繁体   English   中英

在运行时仅使用注释填充spring bean属性

[英]Populate spring bean property using annotations only, at run-time

我有一个只有一个属性attr的bean Parent。

class Parent
{
   String attr;
   public void doStuff(){
      //stuff
   }
   public String getAttr() {
       return attr;
   }
   public void setAttr(String attr) {
       this.attr=attr;
   }
}

我有三个扩展此Parent Bean的bean。 我的spring.xml看起来像这样-

<bean id="parent" class="Parent"/>

<bean id="child1" parent="parent">
    <property name="attr" value="Sample value 1"/>
</bean>

<bean id="child2" parent="parent">
    <property name="attr" value="Sample value 2"/>
</bean>

<bean id="child3" parent="parent">
    <property name="attr" value="Sample value 3"/>
</bean>

我想使用注释做同样的事情。 问题是我必须在店面中进行此操作,并且控制器中的所有bean都声明为-

@Resource
@Qualifier("child1")
Parent child1;

有没有一种方法可以使用注释或控制器中的任何其他方法(即不使用spring.xml)将属性添加到子bean?

有没有一种使用@Value注释的方法。 问题是我没有来自属性文件的静态值。 我对3种不同的豆有3种不同的价值。

如果您的属性文件中有三个不同的值,则不应该这样做

在这里看到帖子

@Value(“${child1.attr}”) @Value(“${child2.attr}”) @Value(“${child3.attr}”)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM