From a32db2581fb69e44fb117349099134497ec43799 Mon Sep 17 00:00:00 2001 From: Danil Kornishev Date: Mon, 21 Nov 2016 11:36:05 -0500 Subject: [PATCH] Added example of spring constructor based dependency injection --- spring-constructor-di/pom.xml | 37 +++++++++++++++++++ .../main/java/com/baeldung/spring/Config.java | 24 ++++++++++++ .../com/baeldung/spring/SpringRunner.java | 30 +++++++++++++++ .../java/com/baeldung/spring/domain/Car.java | 24 ++++++++++++ .../com/baeldung/spring/domain/Engine.java | 16 ++++++++ .../baeldung/spring/domain/Transmission.java | 14 +++++++ .../src/main/resources/baeldung.xml | 20 ++++++++++ 7 files changed, 165 insertions(+) create mode 100644 spring-constructor-di/pom.xml create mode 100644 spring-constructor-di/src/main/java/com/baeldung/spring/Config.java create mode 100644 spring-constructor-di/src/main/java/com/baeldung/spring/SpringRunner.java create mode 100644 spring-constructor-di/src/main/java/com/baeldung/spring/domain/Car.java create mode 100644 spring-constructor-di/src/main/java/com/baeldung/spring/domain/Engine.java create mode 100644 spring-constructor-di/src/main/java/com/baeldung/spring/domain/Transmission.java create mode 100644 spring-constructor-di/src/main/resources/baeldung.xml diff --git a/spring-constructor-di/pom.xml b/spring-constructor-di/pom.xml new file mode 100644 index 0000000000..835a1ba4c8 --- /dev/null +++ b/spring-constructor-di/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + baeldung + baeldung + 1.0-SNAPSHOT + + + + org.springframework + spring-context + 4.2.6.RELEASE + + + javax.inject + javax.inject + 1 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/spring-constructor-di/src/main/java/com/baeldung/spring/Config.java b/spring-constructor-di/src/main/java/com/baeldung/spring/Config.java new file mode 100644 index 0000000000..e7f33c2ad4 --- /dev/null +++ b/spring-constructor-di/src/main/java/com/baeldung/spring/Config.java @@ -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"); + } +} diff --git a/spring-constructor-di/src/main/java/com/baeldung/spring/SpringRunner.java b/spring-constructor-di/src/main/java/com/baeldung/spring/SpringRunner.java new file mode 100644 index 0000000000..e8a74b9482 --- /dev/null +++ b/spring-constructor-di/src/main/java/com/baeldung/spring/SpringRunner.java @@ -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); + } +} diff --git a/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Car.java b/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Car.java new file mode 100644 index 0000000000..73bcc430c1 --- /dev/null +++ b/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Car.java @@ -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); + } +} diff --git a/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Engine.java b/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Engine.java new file mode 100644 index 0000000000..1679692d6d --- /dev/null +++ b/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Engine.java @@ -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); + } +} diff --git a/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Transmission.java b/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Transmission.java new file mode 100644 index 0000000000..47df118020 --- /dev/null +++ b/spring-constructor-di/src/main/java/com/baeldung/spring/domain/Transmission.java @@ -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); + } +} diff --git a/spring-constructor-di/src/main/resources/baeldung.xml b/spring-constructor-di/src/main/resources/baeldung.xml new file mode 100644 index 0000000000..7980460dcc --- /dev/null +++ b/spring-constructor-di/src/main/resources/baeldung.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file