Skip to content

Commit 22f0721

Browse files
eralmas7pivovarit
authored andcommitted
BAEL-1247 Spring XML injection. (eugenp#2901)
1 parent d6cbbbe commit 22f0721

10 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.di.spring;
2+
3+
public interface IService {
4+
public String serve();
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.di.spring;
2+
3+
public class IndexApp {
4+
5+
private IService service;
6+
7+
public String getServiceValue() {
8+
return service.serve();
9+
}
10+
11+
public IService getService() {
12+
return service;
13+
}
14+
15+
public void setService(IService service) {
16+
this.service = service;
17+
}
18+
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.di.spring;
2+
3+
public class IndexService implements IService {
4+
5+
@Override
6+
public String serve() {
7+
return "Hello World";
8+
}
9+
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.di.spring;
2+
3+
public class InstanceServiceFactory {
4+
public IService getService(int number) {
5+
switch (number) {
6+
case 1:
7+
return new MessageService("Foo");
8+
case 0:
9+
return new IndexService();
10+
default:
11+
throw new IllegalArgumentException("Unknown parameter " + number);
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.di.spring;
2+
3+
public class MessageApp {
4+
5+
private IService iService;
6+
7+
public MessageApp(IService iService) {
8+
this.iService = iService;
9+
}
10+
11+
public String getServiceValue() {
12+
return iService.serve();
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.di.spring;
2+
3+
public class MessageService implements IService {
4+
5+
private String message;
6+
7+
public MessageService(String message) {
8+
this.message = message;
9+
}
10+
11+
@Override
12+
public String serve() {
13+
return message;
14+
}
15+
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.di.spring;
2+
3+
public class StaticServiceFactory {
4+
public static IService getService(int number) {
5+
switch (number) {
6+
case 1:
7+
return new MessageService("Foo");
8+
case 0:
9+
return new IndexService();
10+
default:
11+
throw new IllegalArgumentException("Unknown parameter " + number);
12+
}
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
message.value=Hello World
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans
4+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
5+
6+
<bean id="indexService" class="com.baeldung.di.spring.IndexService" />
7+
8+
<bean id="messageService" class="com.baeldung.di.spring.MessageService" scope="prototype">
9+
<constructor-arg value="${message.value}" />
10+
</bean>
11+
12+
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
13+
<property name="location" value="com.baeldung.di.spring.properties" />
14+
</bean>
15+
16+
<bean id="messageServiceFromStaticFactory" class="com.baeldung.di.spring.StaticServiceFactory"
17+
factory-method="getService">
18+
<constructor-arg value="1" />
19+
</bean>
20+
21+
<bean id="indexServiceFactory" class="com.baeldung.di.spring.InstanceServiceFactory" />
22+
23+
<bean id="messageServiceFromInstanceFactory" class="com.baeldung.di.spring.InstanceServiceFactory"
24+
factory-method="getService" factory-bean="indexServiceFactory">
25+
<constructor-arg value="1" />
26+
</bean>
27+
28+
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
29+
<property name="service" ref="indexService" />
30+
</bean>
31+
32+
<bean id="indexAppWithStaticFactory" class="com.baeldung.di.spring.IndexApp" scope="prototype">
33+
<property name="service" ref="messageServiceFromStaticFactory" />
34+
</bean>
35+
36+
<bean id="indexAppWithFactoryMethod" class="com.baeldung.di.spring.IndexApp" scope="prototype">
37+
<property name="service" ref="messageServiceFromInstanceFactory" />
38+
</bean>
39+
40+
<bean id="messageWorldApp" class="com.baeldung.di.spring.MessageApp" scope="prototype">
41+
<constructor-arg ref="messageService" />
42+
</bean>
43+
</beans>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.di.spring;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.support.ClassPathXmlApplicationContext;
8+
9+
public class BeanInjectionTest {
10+
11+
private ApplicationContext applicationContext;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
applicationContext = new ClassPathXmlApplicationContext("com.baeldung.di.spring.xml");
16+
}
17+
18+
@Test
19+
public void protoBean_getBean_returnsMultipleInstance() {
20+
final MessageApp messageApp1 = applicationContext.getBean("messageWorldApp", MessageApp.class);
21+
final MessageApp messageApp2 = applicationContext.getBean("messageWorldApp", MessageApp.class);
22+
assertNotEquals(messageApp1, messageApp2);
23+
}
24+
25+
@Test
26+
public void protoFactoryMethod_getBean_returnsMultipleInstance() {
27+
final IndexApp indexApp1 = applicationContext.getBean("indexAppWithFactoryMethod", IndexApp.class);
28+
final IndexApp indexApp2 = applicationContext.getBean("indexAppWithFactoryMethod", IndexApp.class);
29+
assertNotEquals(indexApp1, indexApp2);
30+
}
31+
32+
@Test
33+
public void protoStaticFactory_getBean_returnsMultipleInstance() {
34+
final IndexApp indexApp1 = applicationContext.getBean("indexAppWithStaticFactory", IndexApp.class);
35+
final IndexApp indexApp2 = applicationContext.getBean("indexAppWithStaticFactory", IndexApp.class);
36+
assertNotEquals(indexApp1, indexApp2);
37+
}
38+
39+
@Test
40+
public void singletonBean_getBean_returnsSingleInstance() {
41+
final IndexApp indexApp1 = applicationContext.getBean("indexApp", IndexApp.class);
42+
final IndexApp indexApp2 = applicationContext.getBean("indexApp", IndexApp.class);
43+
assertEquals(indexApp1, indexApp2);
44+
}
45+
}

0 commit comments

Comments
 (0)