反正@Autowire一个bean,需要构造函数的参数? [英] Anyway to @Autowire a bean that requires constructor arguments?
问题描述
我使用Spring 3.0.5和我使用@Autowire注释为我的类成员尽可能多。我需要autowire的一个bean需要参数的构造函数。
在XML中,我可以使用作为bean定义的一部分。是否有类似的@Autowire注释机制?
例如:
@Component
public class MyConstructorClass {
String var;
public MyConstructorClass(String constrArg){
this.var = var;
}
...
}
@Service
public class MyBeanService {
@Autowired
MyConstructorClass myConstructorClass;
....
}
如何在MyBeanService中使用@Autowire注释指定constrArg的值?有任何方法可以做到吗?
谢谢,
Eric
您需要 @Value
注释。
< >
一个常见的用例是使用
#{systemProperties.myProp}
样式表达式分配默认字段值。
public class SimpleMovieLister {
private MovieFinder movieFinder;
private String defaultLocale;
@Autowired
public void configure(MovieFinder movieFinder,
@Value(#{systemProperties ['user.region']}} String defaultLocale){
this.movieFinder = movieFinder;
this.defaultLocale = defaultLocale;
}
// ...
}
请参阅:表达式语言>注释配置
为了更清楚:在你的场景中,你将连接两个类,
MybeanService
和MyConstructorClass
,像这样:@Component
public class MyBeanService implements BeanService {
@Autowired
public MybeanService(MyConstructorClass foo){
//使用foo
执行操作
}
@Component
public class MyConstructorClass {
public MyConstructorClass(@Value(#{some expression here})String value){
//做某些值为
}
}
更新:如果您需要使用不同值的
MyConstructorClass
的几个不同实例, href =http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers>使用限定词注释I'm using Spring 3.0.5 and am using @Autowire annotation for my class members as much as possible. One of the beans that I need to autowire requires arguments to its constructor. I've looked through the Spring docs, but cannot seem to find any reference to how to annotate constructor arguments.
In XML, I can use as part of the bean definition. Is there a similar mechanism for @Autowire annotation?
Ex:
@Component public class MyConstructorClass{ String var; public MyConstructorClass( String constrArg ){ this.var = var; } ... } @Service public class MyBeanService{ @Autowired MyConstructorClass myConstructorClass; .... }
In this example, how do I specify the value of "constrArg" in MyBeanService with the @Autowire annotation? Is there any way to do this?
Thanks,
Eric
解决方案You need the
@Value
annotation.A common use case is to assign default field values using
"#{systemProperties.myProp}"
style expressions.
public class SimpleMovieLister { private MovieFinder movieFinder; private String defaultLocale; @Autowired public void configure(MovieFinder movieFinder, @Value("#{ systemProperties['user.region'] }"} String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } // ... }
See: Expression Language > Annotation Configuration
To be more clear: in your scenario, you'd wire two classes,
MybeanService
andMyConstructorClass
, something like this:@Component public class MyBeanService implements BeanService{ @Autowired public MybeanService(MyConstructorClass foo){ // do something with foo } } @Component public class MyConstructorClass{ public MyConstructorClass(@Value("#{some expression here}") String value){ // do something with value } }
Update: if you need several different instances of
MyConstructorClass
with different values, you should use Qualifier annotations这篇关于反正@Autowire一个bean,需要构造函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!