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

Mar*_*bst 8 java spring annotations dependency-injection

我正在尝试在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,但是,这似乎也不起作用.

axt*_*avt 16

以下代码适用于<context:property-placeholder .../>:

@Component 
public class MyBean { 
    @Autowired
    public MyBean(@Value("${prop1}") String arg1, @Value("${prop2}") String arg2) { 
        // ... 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

但这<context:property-override .../>是一个非常具体的事情,它不适合这里.