//Ex4_MySome.java
| package exam; /** * 사용자 정보를 저장하는 VO(Value Object) 클래스 * Spring Bean으로 등록되어 다른 객체에 주입(DI)될 수 있다. */ public class Ex4_MySome { // 사용자 아이디 저장 변수 private String id; // 사용자 비밀번호 저장 변수 private String pwd; /** * 아이디 반환 메서드 * * @return 저장된 아이디 */ public String retId() { return id; } /** * 비밀번호 반환 메서드 * * @return 저장된 비밀번호 */ public String retPwd() { return pwd; } /** * 아이디 설정 메서드 * Spring의 property 태그를 통해 호출됨 * * XML 설정: * <property name="id" value="tess2"/> * * @param id 설정할 아이디 */ public void setId(String id) { this.id = id; } /** * 비밀번호 설정 메서드 * Spring의 property 태그를 통해 호출됨 * * XML 설정: * <property name="pwd" value="123"/> * * @param pwd 설정할 비밀번호 */ public void setPwd(String pwd) { this.pwd = pwd; } } |
//Ex4_SomeRef.java
| package exam; /** * Ex4_MySome 객체를 참조하여 사용하는 클래스 * Spring DI(의존성 주입) 예제 */ public class Ex4_SomeRef { // Ex4_MySome 객체를 참조하기 위한 인스턴스 변수 // Spring에서 ref="some" 설정을 통해 주입된다. private Ex4_MySome some; /** * Setter Injection 메서드 * Spring Container가 객체 생성 후 자동으로 호출 * * XML 설정: * <property name="some" ref="some"/> * * Java 코드: * ref.setSome(some); * * @param some 주입받을 Ex4_MySome 객체 */ public void setSome(Ex4_MySome some) { this.some = some; } /** * 주입받은 Ex4_MySome 객체의 정보를 조합하여 반환 * * 내부 동작: * some.retId() -> 아이디 반환 * some.retPwd() -> 비밀번호 반환 * * 예시 결과: * "tess2, 123" * * @return 아이디와 비밀번호를 결합한 문자열 */ public String getUserInfo() { return some.retId() + ", " + some.retPwd(); } } |
//info_di.xml
| <?xml version="1.0" encoding="UTF-8"?> <!-- Spring Bean 설정 파일 --> <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.xsd"> <!-- Ex4_MySome 객체를 Spring 컨테이너에 Bean으로 등록 id="some" : Spring 컨테이너에서 사용할 Bean 이름 class="exam.Ex4_MySome" : 생성할 실제 클래스 경로 --> <bean id="some" class="exam.Ex4_MySome"> <!-- setId("tess2") 호출 --> <property name="id" value="tess2"/> <!-- setPwd("123") 호출 --> <property name="pwd" value="123"/> </bean> <!-- Ex4_SomeRef 객체를 Spring 컨테이너에 Bean으로 등록 id="ref" : Bean 이름 class="exam.Ex4_SomeRef" : 생성할 클래스 --> <bean id="ref" class="exam.Ex4_SomeRef"> <!-- ref="some" Ex4_SomeRef 내부의 some 변수에 위에서 생성한 Bean(id="some") 객체를 주입(DI) Java 코드로 표현하면 Ex4_MySome some = new Ex4_MySome(); some.setId("tess2"); some.setPwd("123"); Ex4_SomeRef ref = new Ex4_SomeRef(); ref.setSome(some); --> <property name="some" ref="some"/> </bean> </beans> |
//Ex4Servlet.java
| package exam; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; /** * Spring DI 예제 Servlet * * URL : * http://localhost:8080/프로젝트명/ex4Servlet */ @WebServlet("/ex4Servlet") public class Ex4Servlet extends HttpServlet { /** * Spring Bean Container 역할을 수행하는 객체 * * XML에 등록된 Bean들을 관리하며 * 필요한 객체를 생성 및 반환한다. */ private ApplicationContext ctx; /** * 서블릿이 최초 실행될 때 단 한번만 호출되는 초기화 메서드 * * info_di.xml 파일을 읽어서 * Spring Bean Container를 생성한다. */ @Override public void init() throws ServletException { // exam/info_di.xml 파일을 읽어 Bean 객체들을 생성 // 생성된 Bean들은 Spring Container 내부에 저장됨 ctx = new GenericXmlApplicationContext("exam/info_di.xml"); } /** * GET 방식 요청 처리 * * URL 예시: * http://localhost:8080/프로젝트명/ex4Servlet */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* * Spring Container에서 * id가 "ref"인 Bean 객체를 가져온다. * * XML: * <bean id="ref" class="exam.Ex4_SomeRef"> * <property name="some" ref="some"/> * </bean> */ Ex4_SomeRef ref = ctx.getBean("ref", Ex4_SomeRef.class); /* * * 결과 예: * tess2, 123 */ String result = /* * Ex4_SomeRef 객체의 메서드 호출 * 내부적으로 * some.retId() + ", " + some.retPwd() * 를 실행하여 반환되는 문자열을 * request 객체에 저장 * * JSP: * ${msg} */ req.setAttribute("msg", ref.getUserInfo();); /* * info.jsp로 요청 전달 * * Forward 방식 * - request 유지 * - response 유지 * - 주소창 변경 없음 */ req.getRequestDispatcher("info.jsp") .forward(req, resp); } } |
//실행화면
다음검색