相关疑难解决方法(0)

如何使用注释将值注入bean构造函数

我的spring bean有一个带有唯一强制参数的构造函数,我设法用xml配置初始化它:

<bean name="interfaceParameters#ota" class="com.company.core.DefaultInterfaceParameters">
  <constructor-arg>
    <value>OTA</value>
  </constructor-arg>
 </bean>
Run Code Online (Sandbox Code Playgroud)

然后我像这样使用这个bean,效果很好.

 @Resource(name = "interfaceParameters#ota")
 private InterfaceParameters interfaceParameters;
Run Code Online (Sandbox Code Playgroud)

但我想用annocations指定构造函数arg值,类似于

 @Resource(name = "interfaceParameters#ota")
 @contructorArg("ota") // I know it doesn't exists!
 private InterfaceParameters interfaceParameters;
Run Code Online (Sandbox Code Playgroud)

这可能吗 ?

提前致谢

spring annotations

27
推荐指数
1
解决办法
6万
查看次数

Spring:构造函数注入具有基于注释的配置的原始值(属性)

我正在尝试在Spring 3中配置一个基于Annotation配置的类,它将原始值作为构造函数参数:

@Component
class MyBean {
  MyBean(String arg1, String arg2) {
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

和这样的应用程序上下文:

<beans [...]>
  <context:component-scan base-package="com.example" />
  <context:property-override location="/WEB-INF/example.properties" />
</beans>
Run Code Online (Sandbox Code Playgroud)

我正在尝试找到一些方法来指定构造函数参数应该从属性文件中获取.显然,这适用于采用常规bean(例如MyClass(Bean bean1, OtherBean bean2))但只是属性的构造函数?

我也尝试使用Spring 3的@Value注释和值的EL表达式来注释构造函数参数@Value("#{prop.Prop1}") arg1,但是,这似乎也不起作用.

java spring annotations dependency-injection

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

annotations ×2

spring ×2

dependency-injection ×1

java ×1