Different Types of Bean Injection in Spring

This commit is contained in:
alv21 2018-02-08 19:15:56 +01:00
parent 0743dec07c
commit ae47827879
15 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package com.baeldung.beansinjectiontypes;
public class Cabriolet implements Car{
public void honk(){
System.out.println("I'm a cabriolet!");
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.beansinjectiontypes;
public interface Car {
public void honk();
}

View File

@ -0,0 +1,9 @@
package com.baeldung.beansinjectiontypes;
public class DealershipTraditionalStyle {
private Car car = new Sedan();
public Car getCar() {
return car;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.beansinjectiontypes;
public class DealershipWithDependenciesInjection {
private Car car;
public DealershipWithDependenciesInjection(Car car) {
this.car = car;
}
public Car getCar() {
return car;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.beansinjectiontypes;
import org.springframework.stereotype.Component;
@Component
public class Sedan implements Car {
public void honk(){
System.out.println("I'm a sedan!");
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.beansinjectiontypes.constructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.beansinjectiontypes.Car;
import com.baeldung.beansinjectiontypes.Sedan;
@Configuration(value="constructorConfig")
public class Config {
@Bean
public Car car() {
return new Sedan();
}
@Bean
public Dealership dealership() {
return new Dealership(car());
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.beansinjectiontypes.constructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.beansinjectiontypes.Car;
@Component(value="constructorDealership")
public class Dealership {
private Car car;
@Autowired
public Dealership(Car car) {
this.car = car;
}
public Car getCar() {
return car;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.beansinjectiontypes.constructor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class HornCheckerFile {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class)) {
Dealership dealership = (Dealership) context.getBean("dealership");
dealership.getCar().honk();
}
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.beansinjectiontypes.constructor;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HornCheckerXML {
public static void main(String[] args) {
try (FileSystemXmlApplicationContext context =
new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) {
Dealership dealership = (Dealership) context.getBean("constructorDealership");
dealership.getCar().honk();
}
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.beansinjectiontypes.setter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.beansinjectiontypes.Car;
import com.baeldung.beansinjectiontypes.Sedan;
@Configuration(value="setterConfig")
public class Config {
@Bean
public Car car() {
return new Sedan();
}
@Bean
public Dealership dealership() {
Dealership dealership = new Dealership();
dealership.setCar(car());
return dealership;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.beansinjectiontypes.setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.beansinjectiontypes.Car;
@Component(value="setterDealership")
public class Dealership {
private Car car;
@Autowired
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return car;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.beansinjectiontypes.setter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class HornCheckerFile {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class)) {
Dealership dealership = (Dealership) context.getBean("dealership");
dealership.getCar().honk();
}
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.beansinjectiontypes.setter;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HornCheckerXML {
public static void main(String[] args) {
try (FileSystemXmlApplicationContext context =
new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) {
Dealership dealership = (Dealership) context.getBean("setterDealership");
dealership.getCar().honk();
}
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:component-scan base-package="com.baeldung.beansinjectiontypes" />
</beans>

View File

@ -0,0 +1,56 @@
package com.baeldung.beansinjectiontypes;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.baeldung.beansinjectiontypes.constructor.Config;
import com.baeldung.beansinjectiontypes.constructor.Dealership;
public class BeansInjectionTypesIntegrationTest {
@Test
public void configBean_WhenSetOnConstructor_ThenDependencyValid() {
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class)) {
Dealership dealership = (Dealership) context.getBean("dealership");
assertTrue(dealership.getCar() instanceof Sedan);
}
}
@Test
public void configBean_WhenSetOnSetter_ThenDependencyValid() {
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(com.baeldung.beansinjectiontypes.setter.Config.class)) {
com.baeldung.beansinjectiontypes.setter.Dealership dealership =
(com.baeldung.beansinjectiontypes.setter.Dealership) context.getBean("dealership");
assertTrue(dealership.getCar() instanceof Sedan);
}
}
@Test
public void annotationAndXML_WhenSetOnSetter_ThenDependencyValid() {
try (FileSystemXmlApplicationContext context =
new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) {
Dealership dealership = (Dealership) context.getBean("constructorDealership");
assertTrue(dealership.getCar() instanceof Sedan);
}
}
@Test
public void annotationAndXML_WhenSetOnConstructor_ThenDependencyValid() {
try (FileSystemXmlApplicationContext context =
new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) {
com.baeldung.beansinjectiontypes.setter.Dealership dealership =
(com.baeldung.beansinjectiontypes.setter.Dealership) context.getBean("setterDealership");
assertTrue(dealership.getCar() instanceof Sedan);
}
}
}