Types of Bean Injection in Spring

This commit is contained in:
Shubhra
2018-04-01 17:23:52 +05:30
parent 1de8475a99
commit e9efcb8e70
9 changed files with 206 additions and 17 deletions

View File

@@ -0,0 +1,22 @@
package com.baeldung.beaninjectiontypes.constructorinjection;
import org.springframework.stereotype.Component;
@Component
public class Address {
private String city;
private String country;
public Address(String city, String country) {
this.city = city;
this.country = country;
}
@Override
public String toString() {
return String.format("%s, %s", city, country);
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.beaninjectiontypes.constructorinjection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.beaninjectiontypes.constructorinjection")
public class Config {
@Bean
public Address address() {
return new Address("New York City", "USA");
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.beaninjectiontypes.constructorinjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class User {
private Address address;
@Autowired
public User(Address address) {
this.address = address;
}
@Override
public String toString() {
return String.format("User Address: %s", address);
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.beaninjectiontypes.setterinjection;
import org.springframework.stereotype.Component;
@Component
public class Address {
private String city;
private String country;
public void setCity(String city) {
this.city = city;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return String.format("%s, %s", city, country);
}
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.beaninjectiontypes.setterinjection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.beaninjectiontypes.setterinjection")
public class Config {
@Bean
public Address address() {
Address address = new Address();
address.setCity("Los Angeles");
address.setCountry("USA");
return address;
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.beaninjectiontypes.setterinjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class User {
private Address address;
@Autowired
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return String.format("User Address : %s", address);
}
}

View File

@@ -1,22 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="exampleDAO" class="com.baeldung.beaninjection.ExampleDAOBean">
<constructor-arg index="0" type="java.lang.String"
value="Mandatory DAO Property X" />
</bean>
<bean id="userWithCI" class="com.baeldung.beaninjectiontypes.constructorinjection.User">
<constructor-arg ref="addressWithCI" />
</bean>
<bean id="anotherSampleDAO" class="com.baeldung.beaninjection.AnotherSampleDAOBean">
<constructor-arg index="0" type="java.lang.String"
value="Mandatory DAO Property Y" />
</bean>
<bean id="userWithSI" class="com.baeldung.beaninjectiontypes.setterinjection.User">
<property name="address" ref="addressWithSI" />
</bean>
<bean id="exampleService" class="com.baeldung.beaninjection.ExampleServiceBean">
<constructor-arg index="0" ref="exampleDAO" />
<property name="propertyX" value="Some Service Property X"></property>
<property name="anotherSampleDAO" ref="anotherSampleDAO"></property>
</bean>
<bean id="addressWithCI" class="com.baeldung.beaninjectiontypes.constructorinjection.Address">
<constructor-arg index="0" value="California"></constructor-arg>
<constructor-arg index="1" value="USA"></constructor-arg>
</bean>
<bean id="addressWithSI" class="com.baeldung.beaninjectiontypes.setterinjection.Address">
<property name="city" value="Washington D.C."></property>
<property name="country" value="USA"></property>
</bean>
</beans>

View File

@@ -0,0 +1,32 @@
package com.baeldung.beaninjectiontypes.constructorinjection;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConstructorInjectionTest {
@Test
public void givenXMLConfiguration_WhenConstructorArgPassed_ThenDependencyValid() {
ApplicationContext context = new ClassPathXmlApplicationContext("beaninjectiontypes-context.xml");
User user = (User) context.getBean("userWithCI");
String expectedOutput = "User Address: California, USA";
assertTrue(expectedOutput.equals(user.toString()));
}
@Test
public void givenPOJOConfiguration_WhenConstructorArgPassed_ThenDependencyValid() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
User user = (User) context.getBean(User.class);
String expectedOutput = "User Address: New York City, USA";
assertTrue(expectedOutput.equals(user.toString()));
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.beaninjectiontypes.setterinjection;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterInjectionTest {
@Test
public void givenXMLConfiguration_WhenSetterArgPassed_ThenDependencyValid() {
ApplicationContext context = new ClassPathXmlApplicationContext("beaninjectiontypes-context.xml");
User user = (User) context.getBean("userWithSI");
String expectedOutput = "User Address : Washington D.C., USA";
assertTrue(expectedOutput.equals(user.toString()));
}
@Test
public void givenPOJOConfiguration_WhenSetterArgPassed_ThenDependencyValid() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
User user = (User) context.getBean(User.class);
String expectedOutput = "User Address : Los Angeles, USA";
assertTrue(expectedOutput.equals(user.toString()));
}
}