Different types of bean injection in Spring
This commit is contained in:
parent
f319718a0f
commit
3a4cddb740
1
pom.xml
1
pom.xml
|
@ -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>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)
|
|
@ -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>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue