【问题标题】:Converting spring xml to java configuration with implicit setter autowiring使用隐式 setter 自动装配将 spring xml 转换为 java 配置
【发布时间】:2015-07-02 17:17:27
【问题描述】:

我想将 spring xml 转换为 java 配置。

<beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
   default-autowire="byName">

  <bean name="car" type="Car" />
  <bean name="tire" type="Tire" />

</beans>

我有两个课程:汽车和轮胎。

public class Tire {
    private String age;
}

public class Car {
    private Tire tire;

    // Spring calls this setter because default-autowire="byName" of xml configuration
    public void setTire(Tire newTire) {
        this.tire = newTire;
    }
}

使用@Inject 或@Autowired 注释,但是spring autowires 并且它可以工作。 如何将 xml 更改为 java 配置而不修改 Car 和 Tire 类?

提前致谢。

【问题讨论】:

    标签: spring autowired spring-java-config


    【解决方案1】:

    这主要是对萨尔曼答案的修复。

    您确实可以使用配置类来满足您的要求,但是您必须car bean 中注入tire bean,而不是任意的Tire。您可以利用配置类将自己的 bean 注入自身是合法的这一事实:

    @Configuration
    public class MyAppConfig {
        @Autowired
        Tire tire;
    
        @Bean 
        public Car car() {
            Car car =new Car();
            car.setTire(tire);
            return car;
        }
        @Bean
        public Tire tire() {
            return new Tire();
        }
    }
    

    这样,您可以将Car.javaTire.java 都保持不变(那里没有注释)并且仍然在car bean 中注入tire bean。

    【讨论】:

      【解决方案2】:

      这是伪代码/示例代码(可能有语法错误)。

      import org.springframework.context.annotation.*;
      import Car;
      import Tire;
      
      @Configuration
      public class MyAppConfig {
        @Bean 
        public Car car() {
          Car car =new Car();
          car.setTire(tire());
          return car;
        }
        @Bean
        public Tire tire() {
          return new Tire();
        }
      }
      

      这是访问上下文的方法:

      public static void main(String[] args) {
         ApplicationContext ctx = 
         new AnnotationConfigApplicationContext(MyAppConfig.class);
         Car car = ctx.getBean(Car.class);
      }
      

      参考:spring_java_based_configuration

      【讨论】:

      • 错了:@Bean 注释不足以破坏编码的内容。你明确car.setTire(tire())。由于轮胎在每次调用时都返回一个新轮胎,因此您在car bean 中注入的那个不是 tire bean。
      • 您可能需要查找 spring 概念 prototype and singleton。如果它更符合您的要求,您可以使用 Serge 的答案。
      • 感谢您的回答,@Salman。确实,我的具体问题在这里:link
      【解决方案3】:

      这里,car() 方法要求一个轮胎作为参数。当春天 调用 car() 来创建 Car bean,它将轮胎自动连接到 配置方法。然后方法的主体可以使用它,但它认为合适。使用这种技术,car() 方法仍然可以将轮胎注入到 Car setTire 方法没有显式引用轮胎 @Bean 方法。

      @Configuration
      public class MyAppConfig {
      
      @Bean
      public Car car(Tire tire) {
          Car car=new Car();
          car.setTire(tire);
          return car;
      }
      @Bean
      public Tire tire() {
          return new Tire();
      }
      

      }

      【讨论】:

      • 感谢您的回答,@ricardo。确实,我的具体问题在这里:link
      猜你喜欢
      • 2015-09-20
      • 2015-05-30
      • 2011-01-15
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多