Different types of bean injection in Spring

This commit is contained in:
Dassi Orleando 2017-08-19 20:41:55 +01:00
parent f319718a0f
commit 3a4cddb740
10 changed files with 264 additions and 0 deletions

View File

@ -170,6 +170,7 @@
<module>spring-social-login</module>
<module>spring-spel</module>
<module>spring-thymeleaf</module>
<module>spring-types-bean-injection</module>
<module>spring-userservice</module>
<module>spring-zuul</module>
<module>spring-reactor</module>

View File

@ -0,0 +1,3 @@
## Relevant articles:
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)

View File

@ -0,0 +1,81 @@
<?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>
<groupId>com.baeldung</groupId>
<artifactId>spring-types-bean-injection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-types-bean-injection</name>
<description>Types of bean injection in spring</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<java.version>1.8</java.version>
<spring.version>4.3.10.RELEASE</spring.version>
<junit.version>4.12</junit.version>
</properties>
</project>

View File

@ -0,0 +1,25 @@
package com.baeldung.config;
import com.baeldung.service.MegaTestService;
import com.baeldung.service.SuperTestService;
import com.baeldung.service.TestService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public TestService TestService() {
return new TestService();
}
@Bean
public SuperTestService SuperTestService() {
return new SuperTestService(new TestService());
}
@Bean
public MegaTestService MegaTestService() {
return new MegaTestService();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MegaTestService {
private TestService testService;
@Autowired
public void setTestService(TestService testService) {
this.testService = testService;
}
public String getMegaTestOne() {
return "Mega" + testService.getTestOne();
}
public String getMegaTestTwo() {
return "Mega" + testService.getTestTwo();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SuperTestService {
private TestService testService;
@Autowired
public SuperTestService(TestService testService) {
this.testService = testService;
}
public String getSuperTestOne() {
return "Super" + testService.getTestOne();
}
public String getSuperTestTwo() {
return "Super" + testService.getTestTwo();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.service;
import org.springframework.stereotype.Service;
@Service
public class TestService {
public String getTestOne() {
return "TestOne";
}
public String getTestTwo() {
return "TestTwo";
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.service;
import com.baeldung.config.AppConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
public class MegaTestServiceIntegrationTest {
@Autowired
MegaTestService megaTestService;
@Test
public void whenCallingGetMegaTestOne_thenWeGetMegaTestOneString() {
String resultOne = megaTestService.getMegaTestOne();
Assert.assertThat(resultOne, is("MegaTestOne"));
}
@Test
public void whenCallingGetMegaTestTwo_thenWeGetMegaTestTwoString() {
String resultTwo = megaTestService.getMegaTestTwo();
Assert.assertThat(resultTwo, is("MegaTestTwo"));
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.service;
import com.baeldung.config.AppConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
public class SuperTestServiceIntegrationTest {
@Autowired
SuperTestService superTestService;
@Test
public void whenCallingGetSuperTestOne_thenWeGetSuperTestOneString() {
String resultOne = superTestService.getSuperTestOne();
Assert.assertThat(resultOne, is("SuperTestOne"));
}
@Test
public void whenCallingGetSuperTestTwo_thenWeGetSuperTestTwoString() {
String resultTwo = superTestService.getSuperTestTwo();
Assert.assertThat(resultTwo, is("SuperTestTwo"));
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.service;
import com.baeldung.config.AppConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { AppConfig.class })
public class TestServiceIntegrationTest {
@Autowired
TestService testService;
@Test
public void whenCallingGetTestOne_thenWeGetTestOneString() {
String resultOne = testService.getTestOne();
Assert.assertThat(resultOne, is("TestOne"));
}
@Test
public void whenCallingGetTestTwo_thenWeGetTestTwoString() {
String resultTwo = testService.getTestTwo();
Assert.assertThat(resultTwo, is("TestTwo"));
}
}