春季:与基于注解配置的基本值(属性)的构造函数注入 [英] Spring: constructor injection of primitive values (properties) with annotation based configuration

查看:212
本文介绍了春季:与基于注解配置的基本值(属性)的构造函数注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置在春季3,这需要原始值作为其构造函数的参数与基于注解的配置类:

I'm trying to configure a class with Annotation based configuration in Spring 3, which takes primitive values as its constructor arguments:

@Component
class MyBean {
  MyBean(String arg1, String arg2) {
    // ...
  }
}

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

And an application context like this:

<beans [...]>
  <context:component-scan base-package="com.example" />
  <context:property-override location="/WEB-INF/example.properties" />
</beans>

我试图找到某种方式来指定构造函数的参数应该从属性文件作出。显然,这并与采取定期豆构造的工作(例如 MyClass的(豆bean1,OtherBean bean2)),但只是属性?

我也试图标注与Spring 3的 @Value 标注的构造函数的参数和值,如 @的EL前pression值(#{prop.Prop1})ARG1 ,但这似乎并没有任何工作。

I have also tried annotating the constructor arguments with Spring 3's @Value annotation and an EL expression for the value, like @Value("#{prop.Prop1}") arg1, but that doesn't seem to work either.

推荐答案

以下code正常工作与&LT;背景:财产占位... /&GT;

The following code works fine with <context:property-placeholder .../>:

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

&LT;背景:重写属性... /&GT; 是一个很具体的东西,它不适合这里。

But <context:property-override .../> is a very specific thing, it's not suitable here.

这篇关于春季:与基于注解配置的基本值(属性)的构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆