[英]How to use same object with its value in different class
Here i have a method getAWSCredential
which returns cred
with some value in it. 这里我有一个
getAWSCredential
方法,它返回带有一些值的cred
。
@Override
public AWSCredentialDTO getAWSCredential(String user_name) {
AWSCredentialDTO cred= (AWSCredentialDTO) entityManager.createNativeQuery("select * from aws_user_credentials a where a.user_name=:userName",AWSCredentialDTO.class)
.setParameter("userName", user_name).getSingleResult();
return cred;
}
Here I have one more service ec2
where I need to make use of values returned in cred
in previous method. 在这里,我还有一个服务
ec2
,我需要在之前的方法中使用cred
返回的值。
private AmazonEC2 ec2;
public AmazonEC2 ec2() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_access_key_id,
aws_secret_access_key);
return AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.AP_SOUTH_1).build();
}
Both the above methods are separate classes. 上述两种方法都是单独的类。 First I will run the
getAWSCredential
and then I will invoke ec2
where I need to make use of the values of cred
. 首先,我将运行
getAWSCredential
,然后我将调用ec2
,我需要使用cred
的值。 Can anyone suggest me how can I achieve this? 任何人都可以建议我如何实现这一目标? This is a spring boot application.
这是一个spring boot应用程序。
Maybe you'd like to use DependencyInjection for it if the credentials are global or on a context, you can create a bean that either stores the credentials or the EC2 instance as a proxy to access it within the context of the session of the user. 如果凭据是全局的或在上下文中,您可能希望使用DependencyInjection,您可以创建一个bean,该bean可以存储凭据,也可以创建EC2实例作为代理,以便在用户会话的上下文中访问它。
Please specify if you have a single username/password or you've got multiple and the different requests need to be served using a username or another. 请指定您是否有一个用户名/密码,或者您有多个用户名/密码,并且需要使用用户名或其他用户提供不同的请求。
If you've got a single, you can create an API Key and set it in application.properties
. 如果您有一个,您可以创建一个API密钥并在
application.properties
设置它。 You can use cloud.aws.credentials.accessKey
and cloud.aws.credentials.secretKey
. 您可以使用
cloud.aws.credentials.accessKey
和cloud.aws.credentials.secretKey
。
You would have to create a spring bean of the class containing getAWSCredential
and autowire it in the class which needs it. 您必须创建包含
getAWSCredential
的类的spring bean,并在需要它的类中自动装配它。 Check the sample code below. 请查看下面的示例代码。
@Component
public class AmazonUtil {
...
@Override
public AWSCredentialDTO getAWSCredential(String user_name) {
AWSCredentialDTO cred= (AWSCredentialDTO) entityManager.createNativeQuery("select * from aws_user_credentials a where a.user_name=:userName",AWSCredentialDTO.class)
.setParameter("userName", user_name).getSingleResult();
return cred;
}
}
@Component
public class Test {
@Autowired
private AmazonUtil amazonUtil;
public AmazonEC2 ec2(String userName) {
AWSCredentialDTO credsDto = amazonUtil.getAWSCredential(userName);
BasicAWSCredentials awsCreds = ...construct using credsDto
return AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.AP_SOUTH_1).build();
}
}
Create a separate class say ClassCred
and create an object for ClassCred
within the same class ClassCred credObj = new ClassCred()
. 创建一个单独的类,说
ClassCred
并在同一个类ClassCred credObj = new ClassCred()
为ClassCred
创建一个对象。 Get the credObj
from getAWSCredential(String user_name)
method and set the cred
returned by getAWSCredential
method to credObj
. 从
getAWSCredential(String user_name)
方法获取credObj
, getAWSCredential(String user_name)
getAWSCredential
方法返回的cred
设置为credObj
。 Now the object credObj
hass the value cred
returned by getAWSCredential
method. 现在,对象
credObj
具有getAWSCredential
方法返回的值cred
。 Now from your ec2()
method you can invoke the credObj
and get the values stored in it. 现在,从
ec2()
方法中,您可以调用credObj
并获取存储在其中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.