sitelink1 http://bestbang.tistory.com/8 
sitelink2 http://stackoverflow.com/questions/46723...t-in-jboss 
sitelink3  
extra_vars6  

 

Servlet에서 Spring Pojo Bean 을 사용할 수 있을까?
순수 자바 프로그램에서 Spring Pojo Bean 은 어떻게 사용할까?
잠깐의 구글링으로 알 수 있었다.

Spring MVC를 사용하면 Controller 를 정의하는 "xxx-servlet.xml" 을 작성하게 될 것이다.
그리고 이 Controller에서 사용하게 될 Pojo Bean 들은 "applicationContext.xml" 과 "dataAccessContext.xml" 에 정의하여 사용 할 것이다.
내가 필요한 것은 Servlet 에서 Pojo Bean 에 접근하고 싶은 것이었다.


1. Servlet 에서 Spring Pojo Bean 에 접근하는 법
  web.xml 에는 다음과 같은 태그가 정의되어 있어야 한다.

·미리보기 | 소스복사·
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>/WEB-INF/dataAccessContext.xml /WEB-INF/applicationContext.xml</param-value>  
  4. </context-param>  
  5.   
  6. <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8. </listener>  

  applicationContext.xml 에는 사용하고자하는 Pojo를 입력한다.

·미리보기 | 소스복사·
 
  1. <bean id="userPojo" class="test.UserPojo">  
  2.     <property name="userDao" ref="userDao"/>  
  3. </bean>  

당연히 userDao 는 dataAccessContext.xml 에 정의하여 두었다.

·미리보기 | 소스복사·
 
  1. <bean id="userDao" class="UserDao"></bean>  



  1.1 WebApplicationContextUtils 클래스를 사용하는 방법
    TestServlet.java 를 작성한다고 하면 userPojo 를 취해야 할 위치에 다음과 같은 코드로 객체를 취한다.

·미리보기 | 소스복사·
 
  1. WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());   
  2. UserPojo userPojo = (UserPojo)context.getBean("userPojo");  

  1.2 ServletContextAttributeExporter 클래스를 사용하여 ServletContext 에서 Attribute 로 취하는 방법
    dataAccessContext.xml 이든 applicationContext.xml 이든 어디든 상관없다. 다음과 같은 XML 태그를 설정한다.

·미리보기 | 소스복사·
 
  1. <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">     
  2.   <property name="attributes">       
  3.     <map>         
  4.       <entry key="userPojo" value-ref="userPojo"/>       
  5.     </map>     
  6.   </property>  
  7. </bean>  

  이제 userPojo 를 취할 위치에서 다음과 같은 코드로 객체를 취한다.

·미리보기 | 소스복사·
 
  1. UserPojo userPojo = (UserPojo) getServletContext().getAttribute("userPojo");  

 

 


2. Pure Java Application에서 Spring Pojo Bean 에 접근하는 법
  순수 자바 프로그램은 Servlet과 같이 web.xml에 등록하는 등의 절차가 없으므로 프로그램 내에서 Spring Context를 로드하는 작업을 해주어야 한다.
  다음과 같은 프로그램으로 Spring Context를 로드할 수 있는데 프로그램이 초기화 될 때 Context 를 로드하고 프로그램 전반에서 Spring Pojo Bean 을 취하면 된다.

 

 

 

 

·미리보기 | 소스복사·
 
  1. public class UserSpringBean implements ContextSpringBean {      
  2.      
  3.     private static UserSpringBean userSpringBean = new UserSpringBean();      
  4.     private ApplicationContext ctx;

        private UserSpringBean(){      
  5.         init();      
  6.     }      
  7.     public static UserSpringBean getInstance(){      
  8.         return userSpringBean;      
  9.     }      
  10.      
  11.     private void init() {      
  12.         ctx = new ClassPathXmlApplicationContext(getContextPaths());      
  13.     }      
  14.      
  15.     public Object getBean(String beanName) {      
  16.         return ctx.getBean(beanName);      
  17.     }      
  18.      
  19.     private String[] getContextPaths() {      
  20.         String rootPath = "spring/";      
  21.         String [] paths = {      
  22.                 rootPath+"dataAccessContext.xml",      
  23.                 rootPath+"applicationContext.xml",      
  24.                 ...      
  25.         };      
  26.         return paths;      
  27.     }      
  28. }  

  그리고 프로그램 내에서는 다음과 같은 코드로 Pojo Bean 을 취한다.

 

 

 

·미리보기 | 소스복사·
 
  1. ContextSpringBean contextSpringBean = UserSpringBean.getInstance();      
  2. UserPojo userPojo = (UserPojo)contextSpringBean.getBean("userPojo");  


  물론 위의 UserSpringBean 클래스를 좀 더 편리하게 변경 가능하리라 생각한다.

 

 

 

 

 

 

 

번호 제목 글쓴이 날짜 조회 수
» Spring MVC 가 아닌 환경에서 Spring Pojo Bean 사용하기 (Pure Java App 또는 Servlet App) 황제낙엽 2009.10.22 541
56 NamedParameterJdbcDaoSupport 몇가지 장점 황제낙엽 2007.11.27 304
55 프로젝트의 기본이 되는 Logging, Exception 처리 전략 황제낙엽 2007.01.30 344
54 Spring AOP - Pointcut 황제낙엽 2007.10.02 357
53 <spring:checkbox> tip! 황제낙엽 2007.10.01 656
52 SimpleFormController 정리 황제낙엽 2007.09.19 483
51 Spring의 Exception 황제낙엽 2007.09.17 421
50 스프링 2와 JPA 시작하기 (한글) 황제낙엽 2007.08.27 406
49 스프링 개발팁 황제낙엽 2007.08.17 473
48 유효성체크 (org.springframework.validation.Validator) 황제낙엽 2007.08.17 344
47 Spring 2.0의 XML확장기능 (3) 황제낙엽 2007.08.15 230
46 Spring 2.0의 XML확장기능 (2) 황제낙엽 2007.08.15 263
45 Spring 2.0의 XML확장기능 (1) 황제낙엽 2007.08.15 234
44 CSS와 XHTML을 사용한 효율적인 View 개발 전략 황제낙엽 2007.01.30 336
43 스프링의 구조별 기능 설명 황제낙엽 2007.06.26 285
42 실습 STEP2 - 기초편 (데이터 베이스 접속) file 황제낙엽 2007.06.21 244
41 java.util.MissingResourceException: Can't find bundle for base name xxx, locale ko_KR 황제낙엽 2007.06.21 2784
40 자바지기 스프링 프레임웍 아티클 황제낙엽 2007.06.04 273
39 AOP(Aspect Oriented Programming) 황제낙엽 2007.06.03 262
38 Spring MVC 어플리케이션 개발 <12> 간단한 조회 구현 방안 비교 황제낙엽 2007.05.27 237