요구사항
* 하나의 interface 기반하에서 "Hello World!" 와 "Hi World!" 메시지를 출력해야 한다.
* 출력하는 메시지를 생성하는 부분과 생성된 메시지를 Rendering 하는 부분이 분리되어야 한다.
Dependency Injection
interface MessageProvider
package net.slipp.dl;
public interface MessageProvider {
String getMessage();
}
class MessageRender
package net.slipp.dl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageRender {
private MessageProvider messageProvider;
public void setMessageProvider(MessageProvider messageProvider) {
this.messageProvider = messageProvider;
}
public void render(){
System.out.println(messageProvider.getMessage());
}
public static void main(String[] args) {
/* MessageRender renderer=new MessageRender();
renderer.setMessageProvider(new HelloWorldMessageProvider());
renderer.render();
renderer.setMessageProvider(new HiWorldMessageProvider());
renderer.render();*/
ApplicationContext ac=new ClassPathXmlApplicationContext("di.xml");
MessageRender renderer =(MessageRender)ac.getBean("messageRender");
renderer.render();
}
}
class HiWorldMessageProvider
package net.slipp.dl;
public class HiWorldMessageProvider implements MessageProvider{
@Override
public String getMessage() {
String str="Hi";
return str;
}
}
class HelloWorldMessageProvide
package net.slipp.dl;
public class HelloWorldMessageProvider implements MessageProvider {
@Override
public String getMessage() {
String str ="Hello";
return str;
}
}
pom.xml
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
class MessageRendererTest
package net.slipp.dl;
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.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/di.xml")
public class MessageRendererTest {
@Autowired
private MessageRender messageRender;
@Test
public void test(){
messageRender.render();
}
}
di.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageProvider" class="net.slipp.dl.HelloWorldMessageProvider" />
<bean id="messageRender" class="net.slipp.dl.MessageRender" >
<property name="messageProvider" ref="messageProvider"></property>
</bean>
</beans>
이클립스 템플릿 설정
${:import(org.junit.Test,org.junit.runner.RunWith,org.springframework.test.context.ContextConfiguration,
org.springframework.test.context.junit4.SpringJUnit4ClassRunner)}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/${cursor}")
댓글 ( 4)
댓글 남기기