Revert "Different Types of Bean Injection in Spring"
This reverts commit ae47827879d0e9396ad5de5f979f495629a35838.
This commit is contained in:
parent
e792f17f82
commit
af542fce40
@ -1,8 +0,0 @@
|
|||||||
package com.baeldung.beansinjectiontypes;
|
|
||||||
|
|
||||||
public class Cabriolet implements Car{
|
|
||||||
|
|
||||||
public void honk(){
|
|
||||||
System.out.println("I'm a cabriolet!");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
package com.baeldung.beansinjectiontypes;
|
|
||||||
|
|
||||||
public interface Car {
|
|
||||||
|
|
||||||
public void honk();
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.baeldung.beansinjectiontypes;
|
|
||||||
|
|
||||||
public class DealershipTraditionalStyle {
|
|
||||||
private Car car = new Sedan();
|
|
||||||
|
|
||||||
public Car getCar() {
|
|
||||||
return car;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.baeldung.beansinjectiontypes;
|
|
||||||
|
|
||||||
public class DealershipWithDependenciesInjection {
|
|
||||||
private Car car;
|
|
||||||
|
|
||||||
public DealershipWithDependenciesInjection(Car car) {
|
|
||||||
this.car = car;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Car getCar() {
|
|
||||||
return car;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
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!");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
<?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>
|
|
@ -1,56 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user