forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Different Types of Bean Injection in Spring
- Loading branch information
alv21
committed
Feb 8, 2018
1 parent
0743dec
commit ae47827
Showing
15 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
spring-core/src/main/java/com/baeldung/beansinjectiontypes/Cabriolet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.baeldung.beansinjectiontypes; | ||
|
||
public class Cabriolet implements Car{ | ||
|
||
public void honk(){ | ||
System.out.println("I'm a cabriolet!"); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
spring-core/src/main/java/com/baeldung/beansinjectiontypes/Car.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.baeldung.beansinjectiontypes; | ||
|
||
public interface Car { | ||
|
||
public void honk(); | ||
} |
9 changes: 9 additions & 0 deletions
9
spring-core/src/main/java/com/baeldung/beansinjectiontypes/DealershipTraditionalStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.beansinjectiontypes; | ||
|
||
public class DealershipTraditionalStyle { | ||
private Car car = new Sedan(); | ||
|
||
public Car getCar() { | ||
return car; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...e/src/main/java/com/baeldung/beansinjectiontypes/DealershipWithDependenciesInjection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
spring-core/src/main/java/com/baeldung/beansinjectiontypes/Sedan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/Dealership.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-core/src/main/java/com/baeldung/beansinjectiontypes/constructor/HornCheckerXML.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/Dealership.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-core/src/main/java/com/baeldung/beansinjectiontypes/setter/HornCheckerXML.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
spring-core/src/main/resources/beansinjectiontypes-ctx.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
56 changes: 56 additions & 0 deletions
56
...re/src/test/java/com/baeldung/beansinjectiontypes/BeansInjectionTypesIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |