简体   繁体   English

如何通过注释注入对象并在此对象上设置属性值

[英]How to inject an object via annotation and set an attribute value on this object

I am using the @Resource annotation to inject an object which is working fine. 我正在使用@Resource批注注入工作正常的对象。 However I would like to set an attribute on this injected object and I'm not sure whether this is possible using annotations. 但是我想在这个注入的对象上设置一个属性,但是我不确定是否可以使用注释来实现。

For example, I have class Test which has an instance of MyClass injected using the @Resource annotation. 例如,我有Test类,它使用@Resource批注注入了MyClass的实例。 MyClass has an attribute, myAttribute , which I want to set when the MyClass instance is injected. MyClass有一个属性myAttribute ,我想在注入MyClass实例时设置它。

Does anyone know if this is possible? 有人知道这是否可能吗?

You should make use of the @PostConstruct annotation, from javax.annotation : 您应该使用javax.annotation@PostConstruct批注:

public class Test {
    @Resource
    private MyClass myClass;

    @PostConstruct
    public void init() {
       myClass.setMyAttribute("test-class");
    }
}

public class AnotherTest {
    @Resource
    private MyClass myClass;

    @PostConstruct
    public void init() {
       myClass.setMyAttribute("another-test-class");
    }
}

This method will then be called after Spring has initialised your object (ie all dependencies have been injected). 在Spring初始化您的对象之后(即,所有依赖项都已注入),然后将调用此方法。

I'm assuming that MyClass isn't a Singleton. 我假设MyClass不是Singleton。

If you are sure that you won't have more than one dependency or alternative MyClass instances at runtime, you can use the solution by StuPointerException. 如果确定在运行时不会有多个依赖项或替代MyClass实例,则可以通过StuPointerException使用解决方案。 But if both Test and AnotherTest exist in a single application context then due to the singleton default scope of spring beans the AnotherTest initialization will affect the state of Test as well. 但是如果Test和AnotherTest都存在于单个应用程序上下文中,则由于spring bean的单例默认范围,AnotherTest初始化也会影响Test的状态。
This is because @Resource will inject the same bean into both owner beans. 这是因为@Resource会将相同的bean注入两个所有者bean中。

To prevent this you should create different beans by the same class. 为了防止这种情况,您应该通过同一类创建不同的bean。 This requires either xml configuration or JavaConfig. 这需要xml配置或JavaConfig。 Since you favor annotations here it is: 由于您喜欢这里的注释,因此它是:

@Configuration
public class AppConfig { 

    @Bean
    public MyClass myClass1() {
        MyClass myClass = new MyClass();
        myClass.setMyAttribute("attr-value-1");
        return myClass;
    }

    @Bean
    public MyClass myClass2() {
        MyClass myClass = new MyClass();
        myClass.setMyAttribute("attr-value-2");
        return myClass;
    }

}

And then you can autowire with @Resource as before but with different beans in each case 然后,您可以像以前一样使用@Resource自动接线,但在每种情况下都使用不同的bean

public class Test {
    @Resource("myClass1")
    private MyClass myClass;    
}

public class AnotherTest {
    @Resource("myClass2")
    private MyClass myClass;
}

DI with Spring : 带弹簧的DI:

@Autowired
MyClass myClass

With Java-ee : 使用Java-ee:

@Inject
MyClass myClass

Concerning annotated property injection, you can still take a look at @Value , but you'll need a properties file along with it 关于带注释的属性注入,您仍然可以查看@Value ,但是您需要一个属性文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jackson 反序列化:我可以在可反序列化对象的字段上注入带有注释的值吗? - Jackson deserialization: Can I inject a value with an annotation on the field of the to deserializable object? 创建对象时如何处理注释和设置默认值 - How to process annotation and set a default value while creating object 如何使用spring注释将一个POJO对象注入另一个POJO? - How to inject one POJO object to another POJO using spring annotation? Java:如何将默认值设置为注释,并将另一个注释作为其属性? - Java: how to set default value to annotation with another annotation as its attribute? 如何通过脚轮生成的 object 在 XML 中按属性值查找元素 - How to find an element by attribute value in XML via castor generated object 如何根据另一个属性值设置 object 属性值? - How to set an object attribute's value based on another attributes value? 为 JUnit 4/5 创建注释以在测试中初始化和注入对象 - Creating an annotation for JUnit 4/5 to initialize and inject an object in tests Java Spring-如何将@Value注入数据对象? - Java Spring - how to inject @Value to a data object? 如何通过反射将字段值设置为纯对象 - How to set a Field value as a plain Object via Reflection 应该使用@Mock注释设置Object的详细值吗? - Should Object using @Mock annotation be set detailed value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM