Merge pull request #11614 from hkhan/JAVA-8290-split-spring-di-module
[JAVA-8290] Split spring-di module
This commit is contained in:
commit
a7e7caaadf
1
pom.xml
1
pom.xml
|
@ -652,6 +652,7 @@
|
|||
<module>spring-data-rest-querydsl</module>
|
||||
<module>spring-di</module>
|
||||
<module>spring-di-2</module>
|
||||
<module>spring-di-3</module>
|
||||
<module>spring-drools</module>
|
||||
|
||||
<module>spring-ejb</module>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
package com.baeldung.constructordi;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
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")
|
|
@ -1,11 +1,10 @@
|
|||
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;
|
||||
|
||||
import com.baeldung.constructordi.domain.Car;
|
||||
|
||||
public class SpringRunner {
|
||||
public static void main(String[] args) {
|
||||
Car toyota = getCarFromXml();
|
|
@ -5,8 +5,9 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
@Component
|
||||
public class Car {
|
||||
private Engine engine;
|
||||
private Transmission transmission;
|
||||
|
||||
private final Engine engine;
|
||||
private final Transmission transmission;
|
||||
|
||||
@Autowired
|
||||
public Car(Engine engine, Transmission transmission) {
|
|
@ -1,8 +1,9 @@
|
|||
package com.baeldung.constructordi.domain;
|
||||
|
||||
public class Engine {
|
||||
private String type;
|
||||
private int volume;
|
||||
|
||||
private final String type;
|
||||
private final int volume;
|
||||
|
||||
public Engine(String type, int volume) {
|
||||
this.type = type;
|
|
@ -1,7 +1,8 @@
|
|||
package com.baeldung.constructordi.domain;
|
||||
|
||||
public class Transmission {
|
||||
private String type;
|
||||
|
||||
private final String type;
|
||||
|
||||
public Transmission(String type) {
|
||||
this.type = type;
|
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.constructordi;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.baeldung.constructordi.domain.Car;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
@ -11,7 +10,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.constructordi.domain.Car;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class)
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
## Spring Dependency Injection
|
||||
|
||||
This module contains articles about dependency injection with Spring
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [@Lookup Annotation in Spring](https://www.baeldung.com/spring-lookup)
|
||||
- More articles: [[<-- prev]](../spring-di-2)
|
|
@ -0,0 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-di-3</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>spring-di-3</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<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.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.6.1</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -5,6 +5,5 @@ import org.springframework.context.annotation.Configuration;
|
|||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.methodinjections")
|
||||
|
||||
public class AppConfig {
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.methodinjections;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@Component
|
||||
public class Grader {
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
package com.baeldung.methodinjections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
@Component("schoolNotification")
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class SchoolNotification {
|
||||
|
@ -19,7 +19,7 @@ public class SchoolNotification {
|
|||
|
||||
public SchoolNotification(String name) {
|
||||
this.name = name;
|
||||
this.marks = new ArrayList<Integer>();
|
||||
this.marks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String addMark(Integer mark) {
|
|
@ -1,15 +1,15 @@
|
|||
package com.baeldung.methodinjections;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Lookup;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component("studentService")
|
||||
public abstract class StudentServices {
|
||||
|
||||
private Map<String, SchoolNotification> notes = new HashMap<>();
|
||||
private final Map<String, SchoolNotification> notes = new HashMap<>();
|
||||
|
||||
@Lookup
|
||||
protected abstract SchoolNotification getNotification(String name);
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.methodinjections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class StudentIntegrationTest {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLookupMethodCalled_thenNewInstanceReturned() {
|
||||
context = new AnnotationConfigApplicationContext(AppConfig.class);
|
||||
|
||||
Student student1 = context.getBean("studentBean", Student.class);
|
||||
Student student2 = context.getBean("studentBean", Student.class);
|
||||
|
||||
assertEquals(student1, student2);
|
||||
assertNotEquals(student1.getNotification("Alex"), student2.getNotification("Bethany"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAbstractGetterMethodInjects_thenNewInstanceReturned() {
|
||||
context = new ClassPathXmlApplicationContext("beans.xml");
|
||||
|
||||
StudentServices services = context.getBean("studentServices", StudentServices.class);
|
||||
|
||||
assertEquals("PASS", services.appendMark("Alex", 76));
|
||||
assertEquals("FAIL", services.appendMark("Bethany", 44));
|
||||
assertEquals("PASS", services.appendMark("Claire", 96));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="15 seconds" debug="false">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -5,13 +5,10 @@ This module contains articles about dependency injection with Spring
|
|||
### Relevant Articles
|
||||
|
||||
- [The Spring @Qualifier Annotation](https://www.baeldung.com/spring-qualifier-annotation)
|
||||
- [Constructor Dependency Injection in Spring](https://www.baeldung.com/constructor-injection-in-spring)
|
||||
- [Spring Autowiring of Generic Types](https://www.baeldung.com/spring-autowire-generics)
|
||||
- [Guice vs Spring – Dependency Injection](https://www.baeldung.com/guice-spring-dependency-injection)
|
||||
- [Injecting Prototype Beans into a Singleton Instance in Spring](https://www.baeldung.com/spring-inject-prototype-bean-into-singleton)
|
||||
- [@Lookup Annotation in Spring](https://www.baeldung.com/spring-lookup)
|
||||
- [Controlling Bean Creation Order with @DependsOn Annotation](https://www.baeldung.com/spring-depends-on)
|
||||
- [Unsatisfied Dependency in Spring](https://www.baeldung.com/spring-unsatisfied-dependency)
|
||||
- [Circular Dependencies in Spring](https://www.baeldung.com/circular-dependencies-in-spring)
|
||||
- [XML-Based Injection in Spring](https://www.baeldung.com/spring-xml-injection)
|
||||
- More articles: [[next -->]](/spring-di-2)
|
||||
- More articles: [[next -->]](../spring-di-2)
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
package com.baeldung.methodinjections;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class StudentIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenLookupMethodCalled_thenNewInstanceReturned() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
|
||||
Student student1 = context.getBean("studentBean", Student.class);
|
||||
Student student2 = context.getBean("studentBean", Student.class);
|
||||
|
||||
Assert.assertEquals(student1, student2);
|
||||
Assert.assertNotEquals(student1.getNotification("Alex"), student2.getNotification("Bethany"));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAbstractGetterMethodInjects_thenNewInstanceReturned() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
|
||||
StudentServices services = context.getBean("studentServices", StudentServices.class);
|
||||
|
||||
Assert.assertEquals("PASS", services.appendMark("Alex", 76));
|
||||
Assert.assertEquals("FAIL", services.appendMark("Bethany", 44));
|
||||
Assert.assertEquals("PASS", services.appendMark("Claire", 96));
|
||||
context.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue