Skip to content

Commit e9efcb8

Browse files
committed
Types of Bean Injection in Spring
1 parent 1de8475 commit e9efcb8

9 files changed

Lines changed: 206 additions & 17 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.beaninjectiontypes.constructorinjection;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Address {
7+
8+
private String city;
9+
10+
private String country;
11+
12+
public Address(String city, String country) {
13+
this.city = city;
14+
this.country = country;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return String.format("%s, %s", city, country);
20+
}
21+
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.beaninjectiontypes.constructorinjection;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@ComponentScan("com.baeldung.beaninjectiontypes.constructorinjection")
9+
public class Config {
10+
11+
@Bean
12+
public Address address() {
13+
return new Address("New York City", "USA");
14+
}
15+
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.beaninjectiontypes.constructorinjection;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class User {
8+
9+
private Address address;
10+
11+
@Autowired
12+
public User(Address address) {
13+
this.address = address;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return String.format("User Address: %s", address);
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.beaninjectiontypes.setterinjection;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Address {
7+
8+
private String city;
9+
10+
private String country;
11+
12+
public void setCity(String city) {
13+
this.city = city;
14+
}
15+
16+
public void setCountry(String country) {
17+
this.country = country;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return String.format("%s, %s", city, country);
23+
}
24+
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.beaninjectiontypes.setterinjection;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@ComponentScan("com.baeldung.beaninjectiontypes.setterinjection")
9+
public class Config {
10+
11+
@Bean
12+
public Address address() {
13+
Address address = new Address();
14+
address.setCity("Los Angeles");
15+
address.setCountry("USA");
16+
return address;
17+
}
18+
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.beaninjectiontypes.setterinjection;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class User {
8+
9+
private Address address;
10+
11+
@Autowired
12+
public void setAddress(Address address) {
13+
this.address = address;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return String.format("User Address : %s", address);
19+
}
20+
21+
}
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns="http://www.springframework.org/schema/beans"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
1+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://www.springframework.org/schema/beans
3+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
54

6-
<bean id="exampleDAO" class="com.baeldung.beaninjection.ExampleDAOBean">
7-
<constructor-arg index="0" type="java.lang.String"
8-
value="Mandatory DAO Property X" />
9-
</bean>
5+
<bean id="userWithCI" class="com.baeldung.beaninjectiontypes.constructorinjection.User">
6+
<constructor-arg ref="addressWithCI" />
7+
</bean>
108

11-
<bean id="anotherSampleDAO" class="com.baeldung.beaninjection.AnotherSampleDAOBean">
12-
<constructor-arg index="0" type="java.lang.String"
13-
value="Mandatory DAO Property Y" />
14-
</bean>
9+
<bean id="userWithSI" class="com.baeldung.beaninjectiontypes.setterinjection.User">
10+
<property name="address" ref="addressWithSI" />
11+
</bean>
1512

16-
<bean id="exampleService" class="com.baeldung.beaninjection.ExampleServiceBean">
17-
<constructor-arg index="0" ref="exampleDAO" />
18-
<property name="propertyX" value="Some Service Property X"></property>
19-
<property name="anotherSampleDAO" ref="anotherSampleDAO"></property>
20-
</bean>
13+
<bean id="addressWithCI" class="com.baeldung.beaninjectiontypes.constructorinjection.Address">
14+
<constructor-arg index="0" value="California"></constructor-arg>
15+
<constructor-arg index="1" value="USA"></constructor-arg>
16+
</bean>
17+
18+
<bean id="addressWithSI" class="com.baeldung.beaninjectiontypes.setterinjection.Address">
19+
<property name="city" value="Washington D.C."></property>
20+
<property name="country" value="USA"></property>
21+
</bean>
2122

2223
</beans>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.beaninjectiontypes.constructorinjection;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
public class ConstructorInjectionTest {
11+
12+
@Test
13+
public void givenXMLConfiguration_WhenConstructorArgPassed_ThenDependencyValid() {
14+
ApplicationContext context = new ClassPathXmlApplicationContext("beaninjectiontypes-context.xml");
15+
User user = (User) context.getBean("userWithCI");
16+
17+
String expectedOutput = "User Address: California, USA";
18+
19+
assertTrue(expectedOutput.equals(user.toString()));
20+
}
21+
22+
@Test
23+
public void givenPOJOConfiguration_WhenConstructorArgPassed_ThenDependencyValid() {
24+
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
25+
User user = (User) context.getBean(User.class);
26+
27+
String expectedOutput = "User Address: New York City, USA";
28+
29+
assertTrue(expectedOutput.equals(user.toString()));
30+
}
31+
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.beaninjectiontypes.setterinjection;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
public class SetterInjectionTest {
11+
12+
@Test
13+
public void givenXMLConfiguration_WhenSetterArgPassed_ThenDependencyValid() {
14+
ApplicationContext context = new ClassPathXmlApplicationContext("beaninjectiontypes-context.xml");
15+
User user = (User) context.getBean("userWithSI");
16+
17+
String expectedOutput = "User Address : Washington D.C., USA";
18+
19+
assertTrue(expectedOutput.equals(user.toString()));
20+
}
21+
22+
@Test
23+
public void givenPOJOConfiguration_WhenSetterArgPassed_ThenDependencyValid() {
24+
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
25+
User user = (User) context.getBean(User.class);
26+
27+
String expectedOutput = "User Address : Los Angeles, USA";
28+
29+
assertTrue(expectedOutput.equals(user.toString()));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)