[JAVA-8290] Split spring-di module
This commit is contained in:
@@ -9,4 +9,6 @@ This module contains articles about dependency injection with Spring
|
||||
- [Spring – Injecting Collections](https://www.baeldung.com/spring-injecting-collections)
|
||||
- [Wiring in Spring: @Autowired, @Resource and @Inject](https://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
||||
- [Injecting Spring Beans into Unmanaged Objects](https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects)
|
||||
- More articles: [[<-- prev]](/spring-di)
|
||||
- [Constructor Dependency Injection in Spring](https://www.baeldung.com/constructor-injection-in-spring)
|
||||
- [Circular Dependencies in Spring](https://www.baeldung.com/circular-dependencies-in-spring)
|
||||
- More articles: [[<-- prev]](../spring-di)[[more -->]](../spring-di-3)
|
||||
|
||||
@@ -14,37 +14,41 @@
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.inject</groupId>
|
||||
<artifactId>javax.inject</artifactId>
|
||||
<version>${javax.inject.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -74,7 +78,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
|
||||
<spring-boot.version>2.6.1</spring-boot.version>
|
||||
<aspectj-plugin.version>1.11</aspectj-plugin.version>
|
||||
<javax.inject.version>1</javax.inject.version>
|
||||
</properties>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CircularDependencyA implements ApplicationContextAware, InitializingBean {
|
||||
|
||||
private CircularDependencyB circB;
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
public CircularDependencyB getCircB() {
|
||||
return circB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
circB = context.getBean(CircularDependencyB.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
|
||||
context = ctx;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CircularDependencyB {
|
||||
|
||||
private CircularDependencyA circA;
|
||||
|
||||
private String message = "Hi!";
|
||||
|
||||
@Autowired
|
||||
public void setCircA(final CircularDependencyA circA) {
|
||||
this.circA = circA;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.constructordi;
|
||||
|
||||
import com.baeldung.constructordi.domain.Engine;
|
||||
import com.baeldung.constructordi.domain.Transmission;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.constructordi")
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public Engine engine() {
|
||||
return new Engine("v8", 5);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Transmission transmission() {
|
||||
return new Transmission("sliding");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.constructordi;
|
||||
|
||||
import com.baeldung.constructordi.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("constructordi.xml");
|
||||
|
||||
return context.getBean(Car.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.constructordi.domain;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Car {
|
||||
|
||||
private final Engine engine;
|
||||
private final 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.constructordi.domain;
|
||||
|
||||
public class Engine {
|
||||
|
||||
private final String type;
|
||||
private final 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.constructordi.domain;
|
||||
|
||||
public class Transmission {
|
||||
|
||||
private final String type;
|
||||
|
||||
public Transmission(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s", type);
|
||||
}
|
||||
}
|
||||
22
spring-di-2/src/main/resources/constructordi.xml
Normal file
22
spring-di-2/src/main/resources/constructordi.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
https://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="toyota" class="com.baeldung.constructordi.domain.Car">
|
||||
<constructor-arg index="0" ref="engine" />
|
||||
<constructor-arg index="1" ref="transmission" />
|
||||
</bean>
|
||||
|
||||
<bean id="engine"
|
||||
class="com.baeldung.constructordi.domain.Engine">
|
||||
<constructor-arg index="0" value="v4" />
|
||||
<constructor-arg index="1" value="2" />
|
||||
</bean>
|
||||
|
||||
<bean id="transmission"
|
||||
class="com.baeldung.constructordi.domain.Transmission">
|
||||
<constructor-arg value="sliding" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { TestConfig.class })
|
||||
public class CircularDependencyIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Bean
|
||||
public CircularDependencyA getCircularDependencyA() {
|
||||
return new CircularDependencyA();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CircularDependencyB getCircularDependencyB() {
|
||||
return new CircularDependencyB();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCircularDependency_whenSetterInjection_thenItWorks() {
|
||||
final CircularDependencyA circA = context.getBean(CircularDependencyA.class);
|
||||
|
||||
Assert.assertEquals("Hi!", circA.getCircB().getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.circulardependency" })
|
||||
public class TestConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.constructordi;
|
||||
|
||||
import com.baeldung.constructordi.domain.Car;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class)
|
||||
public class ConstructorDependencyInjectionIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenPrototypeInjection_WhenObjectFactory_ThenNewInstanceReturn() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||
Car firstContextCar = context.getBean(Car.class);
|
||||
|
||||
ApplicationContext xmlContext = new ClassPathXmlApplicationContext("constructordi.xml");
|
||||
Car secondContextCar = xmlContext.getBean(Car.class);
|
||||
|
||||
assertThat(firstContextCar).isNotSameAs(secondContextCar);
|
||||
assertThat(firstContextCar).hasToString("Engine: v8 5 Transmission: sliding");
|
||||
assertThat(secondContextCar).hasToString("Engine: v4 2 Transmission: sliding");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,10 +10,12 @@ import static org.junit.Assert.assertEquals;
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = AspectJConfig.class)
|
||||
public class PersonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnmanagedObjects_whenInjectingIdService_thenIdValueIsCorrectlySet() {
|
||||
PersonObject personObject = new PersonObject("Baeldung");
|
||||
personObject.generateId();
|
||||
|
||||
assertEquals(1, personObject.getId());
|
||||
assertEquals("Baeldung", personObject.getName());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user