【问题标题】:Spring Autowiring when using Configuration Class使用配置类时的 Spring 自动装配
【发布时间】:2017-07-06 00:35:48
【问题描述】:

我有一个 xml bean 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>
   <bean id="helloWorld" class="com.a.b.HelloWorld"> 
         <property name="attr1" value="Attr1 from XML"></property>
   </bean>
    <bean id="helloWorld2" class="com.a.b.HelloWorld2">
        <property name="attr2" value="Attr2 from XML"></property>
    </bean>
</beans>

我已经像这样使用构造函数自动装配

public class HelloWorld2{
       private String attr2;
       public void setAttr2(String message){
          this.attr2  = message;
       }

       public void getAttr2(){
          System.out.println("getAttr2 == " + attr2);
       }


    }

public class HelloWorld{
       private String attr1;
       private HelloWorld2 helloWorld2;    
       public HelloWorld(){

       }
       @Autowired
       public HelloWorld(HelloWorld2 helloWorld2){
           System.out.println("hhh");
           this.helloWorld2 = helloWorld2;
       }


    public void setAttr1(String message){
          this.attr1  = message;
       }

       public void getAttr1(){
          System.out.println("getAttr1 == " + attr1);
       }
       public void getH(){
           helloWorld2.getAttr2();
       }
    }

自动装配工作正常。

现在我想将我的 bean 移动到 Configuration 类。 但是如何移动代码以便自动装配工作呢?

我试过这样,但它不起作用

@Configuration
public class Config {
    @Bean
    public HelloWorld helloWorld(){
        HelloWorld a = new HelloWorld();
        a.setAttr1("Demo Attr1");
        return a;

    }

    @Bean
    public HelloWorld2 helloWorld2(){
        HelloWorld2 a = new HelloWorld2();
        a.setAttr2("Demo Attr2");
        return a;               
    }
}

【问题讨论】:

标签: java spring autowired


【解决方案1】:

我认为您想要实现的是将HelloWorld2 实例注入到创建HelloWorld @Bean 的方法中?

应该这样做:

@Configuration
public class Config {
    @Bean
    public HelloWorld helloWorld(HelloWorld2 helloWorld2){
        HelloWorld a = new HelloWorld(helloWorld2);
        a.setAttr1("Demo Attr1");
        return a;

    }

    @Bean
    public HelloWorld2 helloWorld2(){
        HelloWorld2 a = new HelloWorld2();
        a.setAttr2("Demo Attr2");
        return a;               
    }
}

这可能是这些问题的重复:

【讨论】:

  • 稍作修改即可使用。我还必须更改 HelloWorld a = new HelloWorld();到 HelloWorld a = new HelloWorld(helloWorld2);方法对吗?
  • 但是现在即使我从 HelloWorld 构造函数中删除了@Autowired 并且 obj1.getH() 的 O/p 给了我演示 Attr2,这段代码仍然可以工作。情况如何?我的意思是 HelloWorld 如何在没有自动装配的情况下获取 HelloWorld2 的实例?
  • 如果显式调用构造函数,则不需要 Autowired 注解。
猜你喜欢
  • 1970-01-01
  • 2019-05-03
  • 2011-07-23
  • 2017-09-07
  • 1970-01-01
  • 2020-09-09
  • 2018-06-15
  • 2021-12-23
  • 2014-06-26
相关资源
最近更新 更多