Added example of spring constructor based dependency injection

This commit is contained in:
Danil Kornishev 2016-11-21 11:36:05 -05:00
parent 8e92dd7e07
commit a32db2581f
7 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>baeldung</groupId>
<artifactId>baeldung</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,24 @@
package com.baeldung.spring;
import com.baeldung.spring.domain.Car;
import com.baeldung.spring.domain.Engine;
import com.baeldung.spring.domain.Transmission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.spring")
public class Config {
@Bean
public Engine engine() {
return new Engine("v8", 5);
}
@Bean
public Transmission transmission() {
return new Transmission("sliding");
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.spring;
import com.baeldung.spring.domain.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
import javax.inject.Named;
@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);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.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);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.spring.domain;
public class Transmission {
private String type;
public Transmission(String type) {
this.type = type;
}
@Override
public String toString() {
return String.format("%s", type);
}
}

View File

@ -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.spring.domain.Car">
<constructor-arg index="0" ref="engine"/>
<constructor-arg index="1" ref="transmission"/>
</bean>
<bean id="engine" class="com.baeldung.spring.domain.Engine">
<constructor-arg index="0" value="v4"/>
<constructor-arg index="1" value="2"/>
</bean>
<bean id="transmission" class="com.baeldung.spring.domain.Transmission">
<constructor-arg value="sliding"/>
</bean>
</beans>