0

I want to convert spring xml to java configuration.

<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>

I have two classes: Car and Tire.

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;
    }
}

I'm not using @Inject nor @Autowired annotations, but spring autowires and it works. How can I change xml to java configuration without modifying Car and Tire classes?

Thanks in advance.

3 Answers3

0

This is psuedo/sample code (may have syntax errors).

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();
  }
}

And here is how to access the context:

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

ref: spring_java_based_configuration

Salman
  • 76
  • 5
  • That's wrong : `@Bean` annotation is not magic enough to break what is coded. You **explicitely** write `car.setTire(tire())`. As tire returns **a new Tire** on each invocation, the one you inject in `car` bean is **not** `tire` bean. – Serge Ballesta Jul 02 '15 at 17:59
  • You may need to look up the spring concepts [prototype and singleton](http://stackoverflow.com/questions/16058365/what-is-difference-between-singleton-and-prototype-bean). You may use Serge's answer if it fits your requirements better. – Salman Jul 02 '15 at 18:59
  • Thanks for your answer, @Salman . Indeed, my specific question is here: [link](http://stackoverflow.com/questions/31192940/converting-spring-xml-to-java-configuration-with-implicit-setter-autowiring-and) – Marcelo Fernandes Jul 02 '15 at 19:30
0

This is mainly a fix to Salman's answer.

You can indeed use a configuration class to achieve your requirements, but you must inject tire bean in car bean and not an arbitrary Tire. You can use the fact that it is legal for a configuration class to have its own beans injected into itself :

@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();
    }
}

That way, you can leave both Car.java and Tire.java untouched (no annotations there) and still have tire bean injected in car bean.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Here, the car() method asks for a Tire as a parameter. When Spring calls car() to create the Car bean, it autowires a Tire into the configuration method. Then the body of the method can use it however it sees fit. With this technique, the car() method can still inject the Tire into the Car setTire method without explicitly referring to the Tire @Bean method.

@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
  • 68
  • 5
  • Thanks for your answer, @ricardo . Indeed, my specific question is here: [link](http://stackoverflow.com/questions/31192940/converting-spring-xml-to-java-configuration-with-implicit-setter-autowiring-and) – Marcelo Fernandes Jul 02 '15 at 19:26