Merge pull request #854 from dkornishev/master
Spring Constructor-Based DI
This commit is contained in:
commit
615ddc19bf
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.constructordi;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.constructordi.domain.Engine;
|
||||
import com.baeldung.constructordi.domain.Transmission;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.constructordi")
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public Engine engine() {
|
||||
return new Engine("v8", 5);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Transmission transmission() {
|
||||
return new Transmission("sliding");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.constructordi;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.baeldung.constructordi.domain.Car;
|
||||
|
||||
public class SpringRunner {
|
||||
public static void main(String[] args) {
|
||||
Car toyota = getCarFromXml();
|
||||
|
||||
System.out.println(toyota);
|
||||
|
||||
toyota = getCarFromJavaConfig();
|
||||
|
||||
System.out.println(toyota);
|
||||
}
|
||||
|
||||
private static Car getCarFromJavaConfig() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||
|
||||
return context.getBean(Car.class);
|
||||
}
|
||||
|
||||
private static Car getCarFromXml() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("baeldung.xml");
|
||||
|
||||
return context.getBean(Car.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.constructordi.domain;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Car {
|
||||
private Engine engine;
|
||||
private Transmission transmission;
|
||||
|
||||
@Autowired
|
||||
public Car(Engine engine, Transmission transmission) {
|
||||
this.engine = engine;
|
||||
this.transmission = transmission;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Engine: %s Transmission: %s", engine, transmission);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.constructordi.domain;
|
||||
|
||||
public class Engine {
|
||||
private String type;
|
||||
private int volume;
|
||||
|
||||
public Engine(String type, int volume) {
|
||||
this.type = type;
|
||||
this.volume = volume;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s %d", type, volume);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.constructordi.domain;
|
||||
|
||||
public class Transmission {
|
||||
private String type;
|
||||
|
||||
public Transmission(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s", type);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<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-3.0.xsd">
|
||||
|
||||
<bean id="toyota" class="com.baeldung.constructordi.domain.Car">
|
||||
<constructor-arg index="0" ref="engine"/>
|
||||
<constructor-arg index="1" ref="transmission"/>
|
||||
</bean>
|
||||
|
||||
<bean id="engine" class="com.baeldung.constructordi.domain.Engine">
|
||||
<constructor-arg index="0" value="v4"/>
|
||||
<constructor-arg index="1" value="2"/>
|
||||
</bean>
|
||||
|
||||
<bean id="transmission" class="com.baeldung.constructordi.domain.Transmission">
|
||||
<constructor-arg value="sliding"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
Loading…
Reference in New Issue