From 8c8af4e9bfd8eba66ff524d60b23a3dcbb40cd99 Mon Sep 17 00:00:00 2001 From: gschambial Date: Fri, 10 Nov 2017 17:52:21 +0530 Subject: [PATCH] 1. Constructor and Setter injection explained with example of Computer, Processor and Memory --- spring-boot/.factorypath | 111 ++++++++++-------- .../SpringAutowireConstructorContext.java | 10 ++ .../config/SpringBeanConstructorContext.java | 28 +++++ .../factory/ComputerAutowireFactory.java | 28 +++++ .../factory/ComputerBeanFactory.java | 29 +++++ .../factory/ComputerXmlFactory.java | 31 +++++ .../constructor/model/autowire/Computer.java | 34 ++++++ .../di/constructor/model/autowire/Memory.java | 19 +++ .../constructor/model/autowire/Processor.java | 19 +++ .../di/constructor/model/bean/Computer.java | 29 +++++ .../di/constructor/model/bean/Memory.java | 16 +++ .../di/constructor/model/bean/Processor.java | 16 +++ .../di/constructor/model/xml/Computer.java | 29 +++++ .../di/constructor/model/xml/Memory.java | 20 ++++ .../di/constructor/model/xml/Processor.java | 16 +++ .../config/SpringAutowireSetterContext.java | 10 ++ .../config/SpringBeanSetterContext.java | 31 +++++ .../factory/ComputerAutowireFactory.java | 29 +++++ .../setter/factory/ComputerBeanFactory.java | 30 +++++ .../di/setter/factory/ComputerXmlFactory.java | 30 +++++ .../di/setter/model/autowire/Computer.java | 30 +++++ .../di/setter/model/autowire/Memory.java | 19 +++ .../di/setter/model/autowire/Processor.java | 19 +++ .../di/setter/model/bean/Computer.java | 24 ++++ .../baeldung/di/setter/model/bean/Memory.java | 16 +++ .../di/setter/model/bean/Processor.java | 16 +++ .../di/setter/model/xml/Computer.java | 24 ++++ .../baeldung/di/setter/model/xml/Memory.java | 20 ++++ .../di/setter/model/xml/Processor.java | 16 +++ .../application-di-constructor-context.xml | 23 ++++ .../application-di-setter-context.xml | 22 ++++ .../AutowireAnnotationInjectionTest.java | 28 +++++ .../BeanAnnotationInjectionTest.java | 28 +++++ .../di/constructor/XmlInjectionTest.java | 27 +++++ .../AutowireAnnotationInjectionTest.java | 28 +++++ .../setter/BeanAnnotationInjectionTest.java | 28 +++++ .../baeldung/di/setter/XmlInjectionTest.java | 27 +++++ 37 files changed, 910 insertions(+), 50 deletions(-) create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java create mode 100644 spring-core/src/main/resources/application-di-constructor-context.xml create mode 100644 spring-core/src/main/resources/application-di-setter-context.xml create mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java diff --git a/spring-boot/.factorypath b/spring-boot/.factorypath index aa15485f5c..97062c74a4 100644 --- a/spring-boot/.factorypath +++ b/spring-boot/.factorypath @@ -1,46 +1,30 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -48,81 +32,105 @@ - + + + + + - - + - - - + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + + - + + - @@ -133,7 +141,10 @@ + + + @@ -143,7 +154,7 @@ - + diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java new file mode 100644 index 0000000000..d3c43d843e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java @@ -0,0 +1,10 @@ +package com.baeldung.di.constructor.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages= { "com.baeldung.di.constructor.model.autowire" }) +public class SpringAutowireConstructorContext { + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java new file mode 100644 index 0000000000..0ac1138e78 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.di.constructor.model.bean.Computer; +import com.baeldung.di.constructor.model.bean.Memory; +import com.baeldung.di.constructor.model.bean.Processor; + +@Configuration +public class SpringBeanConstructorContext { + + @Bean + public Computer computer(){ + return new Computer(processor(), memory()); + } + + @Bean + public Processor processor(){ + return new Processor(); + } + + @Bean + public Memory memory(){ + return new Memory(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java new file mode 100644 index 0000000000..a86733c3d1 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; +import com.baeldung.di.constructor.model.autowire.Computer; + +public class ComputerAutowireFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via @Autowire------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Constructor Injection via @Autowire------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java new file mode 100644 index 0000000000..b518438521 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java @@ -0,0 +1,29 @@ +package com.baeldung.di.constructor.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringBeanConstructorContext; +import com.baeldung.di.constructor.model.bean.Computer; + +public class ComputerBeanFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via @Bean ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Constructor Injection via @Bean ------------------"); + + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java new file mode 100644 index 0000000000..2fa12525df --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java @@ -0,0 +1,31 @@ +package com.baeldung.di.constructor.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.constructor.model.xml.Computer; + + + +public class ComputerXmlFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via XML ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Constructor Injection via XML ------------------"); + + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java new file mode 100644 index 0000000000..b470aedeed --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java @@ -0,0 +1,34 @@ +package com.baeldung.di.constructor.model.autowire; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Computer { + + private Processor processor; + private Memory memory; + + @Autowired + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java new file mode 100644 index 0000000000..29633dd5ca --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java @@ -0,0 +1,19 @@ +package com.baeldung.di.constructor.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java new file mode 100644 index 0000000000..0d81e2a005 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java @@ -0,0 +1,19 @@ +package com.baeldung.di.constructor.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java new file mode 100644 index 0000000000..d8856420d0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java @@ -0,0 +1,29 @@ +package com.baeldung.di.constructor.model.bean; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java new file mode 100644 index 0000000000..8e748bb1a0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java @@ -0,0 +1,16 @@ +package com.baeldung.di.constructor.model.bean; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java new file mode 100644 index 0000000000..1566606148 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.constructor.model.bean; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java new file mode 100644 index 0000000000..dbd70cf6a4 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java @@ -0,0 +1,29 @@ +package com.baeldung.di.constructor.model.xml; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java new file mode 100644 index 0000000000..f5ea3b4ec9 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java @@ -0,0 +1,20 @@ +package com.baeldung.di.constructor.model.xml; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Memory() { + + } + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java new file mode 100644 index 0000000000..72733829d8 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.constructor.model.xml; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java new file mode 100644 index 0000000000..4e0ac0df28 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java @@ -0,0 +1,10 @@ +package com.baeldung.di.setter.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages= { "com.baeldung.di.setter.model.autowire" }) +public class SpringAutowireSetterContext { + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java new file mode 100644 index 0000000000..885173f5b4 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java @@ -0,0 +1,31 @@ +package com.baeldung.di.setter.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.di.setter.model.bean.Computer; +import com.baeldung.di.setter.model.bean.Memory; +import com.baeldung.di.setter.model.bean.Processor; + +@Configuration +public class SpringBeanSetterContext { + + @Bean + public Computer computer(){ + Computer computer = new Computer(); + computer.setProcessor(processor()); + computer.setMemory(memory()); + return computer; + } + + @Bean + public Processor processor(){ + return new Processor(); + } + + @Bean + public Memory memory(){ + return new Memory(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java new file mode 100644 index 0000000000..98c3fccb27 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java @@ -0,0 +1,29 @@ +package com.baeldung.di.setter.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringAutowireSetterContext; +import com.baeldung.di.setter.model.autowire.Computer; + + +public class ComputerAutowireFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via @Autowire ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Setter Injection via @Autowire ------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java new file mode 100644 index 0000000000..6341029e95 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java @@ -0,0 +1,30 @@ +package com.baeldung.di.setter.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringBeanSetterContext; +import com.baeldung.di.setter.model.bean.Computer; + + +public class ComputerBeanFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via @Bean ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Setter Injection via @Bean ------------------"); + + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java new file mode 100644 index 0000000000..707725d340 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java @@ -0,0 +1,30 @@ +package com.baeldung.di.setter.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.setter.model.xml.Computer; + + + +public class ComputerXmlFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new ClassPathXmlApplicationContext("application-setter-context.xml"); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via XML ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Setter Injection via XML ------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java new file mode 100644 index 0000000000..4d8a3b1dd3 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java @@ -0,0 +1,30 @@ +package com.baeldung.di.setter.model.autowire; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Computer { + + private Processor processor; + private Memory memory; + + public Processor getProcessor() { + return processor; + } + + @Autowired + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + @Autowired + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java new file mode 100644 index 0000000000..0953a42b35 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java @@ -0,0 +1,19 @@ +package com.baeldung.di.setter.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java new file mode 100644 index 0000000000..ee5ef3b6cd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java @@ -0,0 +1,19 @@ +package com.baeldung.di.setter.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java new file mode 100644 index 0000000000..01c87a6c73 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java @@ -0,0 +1,24 @@ +package com.baeldung.di.setter.model.bean; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java new file mode 100644 index 0000000000..bfe94aa8e7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java @@ -0,0 +1,16 @@ +package com.baeldung.di.setter.model.bean; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java new file mode 100644 index 0000000000..982b56f3d0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.setter.model.bean; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java new file mode 100644 index 0000000000..3e7fa58ea8 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java @@ -0,0 +1,24 @@ +package com.baeldung.di.setter.model.xml; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java new file mode 100644 index 0000000000..cb11c59583 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java @@ -0,0 +1,20 @@ +package com.baeldung.di.setter.model.xml; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Memory() { + + } + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java new file mode 100644 index 0000000000..07405e13ea --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.setter.model.xml; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/resources/application-di-constructor-context.xml b/spring-core/src/main/resources/application-di-constructor-context.xml new file mode 100644 index 0000000000..46d1256988 --- /dev/null +++ b/spring-core/src/main/resources/application-di-constructor-context.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/main/resources/application-di-setter-context.xml b/spring-core/src/main/resources/application-di-setter-context.xml new file mode 100644 index 0000000000..3831041e01 --- /dev/null +++ b/spring-core/src/main/resources/application-di-setter-context.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java new file mode 100644 index 0000000000..87cb549ace --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; +import com.baeldung.di.constructor.model.autowire.Computer; + +public class AutowireAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java new file mode 100644 index 0000000000..61f718d4ba --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringBeanConstructorContext; +import com.baeldung.di.constructor.model.bean.Computer; + +public class BeanAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java new file mode 100644 index 0000000000..4787c19cbf --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java @@ -0,0 +1,27 @@ +package com.baeldung.di.constructor; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.constructor.model.xml.Computer; + +public class XmlInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java new file mode 100644 index 0000000000..036c6826e3 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.setter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringAutowireSetterContext; +import com.baeldung.di.setter.model.autowire.Computer; + +public class AutowireAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java new file mode 100644 index 0000000000..53f1d4e10e --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.setter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringBeanSetterContext; +import com.baeldung.di.setter.model.bean.Computer; + +public class BeanAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java new file mode 100644 index 0000000000..52452be119 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java @@ -0,0 +1,27 @@ +package com.baeldung.di.setter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.setter.model.xml.Computer; + +public class XmlInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new ClassPathXmlApplicationContext("application-di-setter-context.xml"); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +}