Spring_04_ Spring VS JSP 방식 2

hyeong taek jo·2023년 10월 3일
0

Spring

목록 보기
4/34

📌 Spring DI 방식 (객체선언)

package sam07;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloApp {

	public static void main(String[] args) {						//원래는 resources 폴더에 다 가져다 놓는데 만일 다른폴더에 넣는다면 이렇게 하면된다.
		ApplicationContext ac = new ClassPathXmlApplicationContext("/sam07/bean07.xml");
		MessageBean mb = (MessageBean) ac.getBean("mb7");
		mb.sayHello();
	}
}

package sam07;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileOutputter implements Outputter {
	private String fileName;
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void output(String msg) {
		System.out.println("fileName: "+fileName);
		try {
			FileWriter fw = new FileWriter(new File(fileName));
			fw.write(msg);
			fw.close();
		} catch (IOException e) {
			System.out.println("e.getMessage()-->"+e.getMessage());
		}
	}

}

package sam07;

public class MessageBeanImpl implements MessageBean {
	private String    name;
	private String    greet;
	private Outputter outPutter;
	
	public void setName(String name) {
		this.name = name;
	}

	public void setGreet(String greet) {
		this.greet = greet;
	}

	public void setOutPutter(Outputter outPutter) {
		this.outPutter = outPutter;
	}

	public void sayHello() {
		String msg = name + "님! " + greet;
		System.out.println(msg);
		if(outPutter != null) outPutter.output(msg);
	}
}

정송환님! 안녕하세요
fileName: c:/log/msg1.txt

profile
마포구 주민

0개의 댓글