BAEL-1247 Spring XML injection. (#2901)
This commit is contained in:
parent
d6cbbbedb4
commit
22f07214b1
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public interface IService {
|
||||
public String serve();
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class IndexApp {
|
||||
|
||||
private IService service;
|
||||
|
||||
public String getServiceValue() {
|
||||
return service.serve();
|
||||
}
|
||||
|
||||
public IService getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(IService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class IndexService implements IService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class InstanceServiceFactory {
|
||||
public IService getService(int number) {
|
||||
switch (number) {
|
||||
case 1:
|
||||
return new MessageService("Foo");
|
||||
case 0:
|
||||
return new IndexService();
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parameter " + number);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class MessageApp {
|
||||
|
||||
private IService iService;
|
||||
|
||||
public MessageApp(IService iService) {
|
||||
this.iService = iService;
|
||||
}
|
||||
|
||||
public String getServiceValue() {
|
||||
return iService.serve();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class MessageService implements IService {
|
||||
|
||||
private String message;
|
||||
|
||||
public MessageService(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class StaticServiceFactory {
|
||||
public static IService getService(int number) {
|
||||
switch (number) {
|
||||
case 1:
|
||||
return new MessageService("Foo");
|
||||
case 0:
|
||||
return new IndexService();
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parameter " + number);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
message.value=Hello World
|
|
@ -0,0 +1,43 @@
|
|||
<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="indexService" class="com.baeldung.di.spring.IndexService" />
|
||||
|
||||
<bean id="messageService" class="com.baeldung.di.spring.MessageService" scope="prototype">
|
||||
<constructor-arg value="${message.value}" />
|
||||
</bean>
|
||||
|
||||
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="com.baeldung.di.spring.properties" />
|
||||
</bean>
|
||||
|
||||
<bean id="messageServiceFromStaticFactory" class="com.baeldung.di.spring.StaticServiceFactory"
|
||||
factory-method="getService">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexServiceFactory" class="com.baeldung.di.spring.InstanceServiceFactory" />
|
||||
|
||||
<bean id="messageServiceFromInstanceFactory" class="com.baeldung.di.spring.InstanceServiceFactory"
|
||||
factory-method="getService" factory-bean="indexServiceFactory">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
|
||||
<property name="service" ref="indexService" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexAppWithStaticFactory" class="com.baeldung.di.spring.IndexApp" scope="prototype">
|
||||
<property name="service" ref="messageServiceFromStaticFactory" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexAppWithFactoryMethod" class="com.baeldung.di.spring.IndexApp" scope="prototype">
|
||||
<property name="service" ref="messageServiceFromInstanceFactory" />
|
||||
</bean>
|
||||
|
||||
<bean id="messageWorldApp" class="com.baeldung.di.spring.MessageApp" scope="prototype">
|
||||
<constructor-arg ref="messageService" />
|
||||
</bean>
|
||||
</beans>
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class BeanInjectionTest {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
applicationContext = new ClassPathXmlApplicationContext("com.baeldung.di.spring.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void protoBean_getBean_returnsMultipleInstance() {
|
||||
final MessageApp messageApp1 = applicationContext.getBean("messageWorldApp", MessageApp.class);
|
||||
final MessageApp messageApp2 = applicationContext.getBean("messageWorldApp", MessageApp.class);
|
||||
assertNotEquals(messageApp1, messageApp2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void protoFactoryMethod_getBean_returnsMultipleInstance() {
|
||||
final IndexApp indexApp1 = applicationContext.getBean("indexAppWithFactoryMethod", IndexApp.class);
|
||||
final IndexApp indexApp2 = applicationContext.getBean("indexAppWithFactoryMethod", IndexApp.class);
|
||||
assertNotEquals(indexApp1, indexApp2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void protoStaticFactory_getBean_returnsMultipleInstance() {
|
||||
final IndexApp indexApp1 = applicationContext.getBean("indexAppWithStaticFactory", IndexApp.class);
|
||||
final IndexApp indexApp2 = applicationContext.getBean("indexAppWithStaticFactory", IndexApp.class);
|
||||
assertNotEquals(indexApp1, indexApp2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singletonBean_getBean_returnsSingleInstance() {
|
||||
final IndexApp indexApp1 = applicationContext.getBean("indexApp", IndexApp.class);
|
||||
final IndexApp indexApp2 = applicationContext.getBean("indexApp", IndexApp.class);
|
||||
assertEquals(indexApp1, indexApp2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue