반응형
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- sample04 -->
<bean id="messageBeanImpl2" class="sample04.MessageBeanImpl">
<constructor-arg value="홍길동"/>
<property name="phone" value="010-123-1234"/>
<property name="outputter" ref="fileOutputter"/>
</bean>
<bean id="fileOutputter" class="sample04.FileOutputter">
<property name="filePath" value="D:/"/>
<property name="fileName" value="result.txt"/>
</bean>
</beans>
HelloSpring
package sample04;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
System.out.println("0. Life Cycle");
System.out.println("---------------------");
System.out.println("ClassPathXmlApplicationContext");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("---------------------");
MessageBean messageBean = (MessageBean) context.getBean("messageBeanImpl2");
System.out.println("context.getBean(");
messageBean.helloCall();
}
}
Outputter
package sample04;
public interface Outputter {
public void output(String message);
}
MessageBeanImpl
package sample04;
public class MessageBeanImpl implements MessageBean {
private String name;
private String phone;
private String aa;
private Outputter outputter; //파일로 출력
public MessageBeanImpl(String name) {
System.out.println("1. MessageBeanImpl(String name) 생성자는 name을 입력한다.");
this.name = name;
}
public void setPhone(String phone) {
System.out.println("5. setPhone(String phone)");
this.phone = phone;
}
public
void setOutputter(Outputter outputter) {
System.out.println("6. setOutputter(Outputter outputter)");
this.outputter = outputter;
}
@Override
public void helloCall() {
System.out.println("helloCall()");
outputter.output("이름="+phone+"\t 핸드폰="+name);
System.out.println("h1");
}
}
FileOutputter
package sample04;
import java.io.FileWriter;
import java.io.IOException;
public class FileOutputter implements Outputter {
private String filePath, fileName;
public FileOutputter() {
System.out.println("2. FileOutputter() 기본생성자 하는게 없는놈");
}
public void setFilePath(String filePath) {
System.out.println("3. setFilePath(String filePath)");
this.filePath = filePath;
}
public void setFileName(String fileName) {
System.out.println("4. setFileName(String fileName)");
this.fileName = fileName;
}
@Override
public void output(String message) {
try {
System.out.println("f1");
FileWriter fileWriter = new FileWriter(filePath+fileName);
System.out.println("f2");
fileWriter.write(message);
System.out.println("f3");
fileWriter.close();
System.out.println("f4");
} catch (IOException e) {
e.printStackTrace();
}
}
}
반응형
'Dev > Spring' 카테고리의 다른 글
스프링 MVC with @Annotation (0) | 2021.01.14 |
---|---|
스프링 MVC + Maven + Tomcat 프로젝트 생성 (0) | 2021.01.12 |
스프링 MVC 기본구조 정리 (0) | 2021.01.10 |
AOP 기능 사용하기 예제 (1) | 2020.12.28 |
STS3 정리 (0) | 2020.12.22 |