简体   繁体   English

如何在不同的类中使用相同的对象及其值

[英]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.accessKeycloud.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)方法获取credObjgetAWSCredential(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.

相关问题 如何在一个班级的不同地方使用同一对象? - How to use same object at different places in a class? 我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇) - How can i use the return value of a private static method (extension of object) in a different method in that same class? (java) 如何在同一类的不同方法中使用显式等待对象? - How to use explicit wait object in different methods from the same class? 是否可以将同一个对象及其值用作其他对象? - Can I get the same object with its vales and use it in a different method? 为什么此类的每个对象对其成员都具有相同的值? - Why does every object of this class have the same value for its members? 如何在同一个类的方法中使用一个类的对象? - How to use an object of a class in a method of the same class? 在返回其对象的相同类型的值的final方法中使用泛型 - Use of generics in a final method that returns a value of the same type of its object 如何将 object 的 class 具有不同 class 成员的 ZCC8D68C551C4A9A6D5313E07DE4DE - How to add an object that its class has a different class member to an CSV? 如何在同一应用程序中使用不同版本的 class? - How to use different versions of a class in the same application? 如何使用不同类别的同一个Webdriver - How to use the same Webdriver from different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM