[英]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.