反正@Autowire一个bean,需要构造函数的参数? [英] Anyway to @Autowire a bean that requires constructor arguments?

查看:2971
本文介绍了反正@Autowire一个bean,需要构造函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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.movi​​eFinder = 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 and MyConstructorClass, 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屋!

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