在 Spring 的 Java 配置中自动装配 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28747743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:43:47  来源:igfitidea点击:

Autowire a bean within Spring's Java configuration

javaspring

提问by Avanst

Is it possible to use Spring's @Autowiredannotation within a Spring configuration written in Java?

是否可以在用@AutowiredJava 编写的 Spring 配置中使用 Spring 的注释?

For example:

例如:

@Configuration
public class SpringConfiguration{

   @Autowired 
   DataSource datasource;

   @Bean
   public DataSource dataSource(){
       return new dataSource();
   }

   // ...

}

Obviously a DataSource interface cannot be directly instantiated but I directly instantiated it here for simplification. Currently, when I try the above, the datasource object remains null and is not autowired by Spring.

很明显,一个DataSource接口不能直接实例化,为了简化,我这里直接实例化了。目前,当我尝试上述操作时,数据源对象保持为 null,并且不会由 Spring 自动装配。

I got @Autowiredto work successfully with a Hibernate SessionFactoryobject by returning a FactoryBean<SessionFactory>.

@Autowired用一个Hibernate成功运行SessionFactory对象通过返回FactoryBean<SessionFactory>

So my question specifically: is there a way to do that with respect to a DataSource? Or more generally, what is the method to autowire a bean within a Spring Java Configuration?

所以我的问题是:有没有办法对 a 做到这一点DataSource?或者更一般地说,在 Spring Java 配置中自动装配 bean 的方法是什么?

I should note I am using Spring version 3.2.

我应该注意到我使用的是 Spring 3.2 版。

采纳答案by Sotirios Delimanolis

If you need a reference to the DataSourcebean within the same @Configurationfile, just invoke the bean method.

如果您需要DataSource在同一@Configuration文件中引用 bean,只需调用 bean 方法。

@Bean
public OtherBean someOtherBean() {
    return new OtherBean(dataSource());
}

or have it autowired into the @Beanmethod

或者让它自动连接到@Bean方法中

@Bean
public OtherBean someOtherBean(DataSource dataSource) {
    return new OtherBean(dataSource);
}

The lifecycle of a @Configurationclass sometimes prevents autowiring like you are suggesting.

@Configuration类的生命周期有时会像您建议的那样阻止自动装配。

回答by javahuang

maybe you can use constructor to inject some bean from other configuration file to your configuration,while @Autowired may not work correct in the configuration file

也许您可以使用构造函数将其他配置文件中的一些 bean 注入您的配置,而 @Autowired 在配置文件中可能无法正常工作

@Configuration
public class AppConfig {
  private DataSource dataSource;

  public AppConfig(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  @Bean
  public OtherBean otherBean(){
    return new OtherBean(dataSource);
  }
}

回答by Shruthi H R

beans configured with @Configuration class can't be autowired ,

用@Configuration 类配置的 bean 不能自动装配,

So you cannot use @Autowiring and @configuration together

所以你不能同时使用@Autowiring 和@configuration

回答by tkruse

For completeness sake: Yes it is technically possible to use @Autowired annotation in spring @Configuration classes, and it works the same way as for other spring beans. But the accepted answer shows how the original problem should be solved.

为了完整起见:是的,技术上可以在 spring @Configuration 类中使用 @Autowired 注释,它的工作方式与其他 spring bean 相同。但是接受的答案显示了应该如何解决原始问题。