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 클래스를 좀 더 편리하게 변경 가능하리라 생각한다.

 

 

 

 

 

 

 

번호 제목 글쓴이 날짜 조회 수
공지 (확인전) [2021.03.12] Eclipse에서 Spring Boot로 JSP사용하기(Gradle) 황제낙엽 2023.12.23 0
공지 [작성중/인프런] 스프링부트 시큐리티 & JWT 강의 황제낙엽 2023.12.20 6
83 java.util.MissingResourceException: Can't find bundle for base name xxx, locale ko_KR 황제낙엽 2007.06.21 2362
82 [Spring3.1.1][1] RestTemplate 한글 문제 황제낙엽 2018.08.08 683
81 Spring WebFlow Introduction (웹개발을 직관적으로) 황제낙엽 2006.12.09 555
80 Spring Security OAuth2구현 file 황제낙엽 2019.09.05 462
79 Spring Security OAuth 황제낙엽 2019.09.05 435
78 <spring:checkbox> tip! 황제낙엽 2007.10.01 378
77 [Spring3.1.1][3] RestTemplate 한글 문제 황제낙엽 2018.08.08 237
» Spring MVC 가 아닌 환경에서 Spring Pojo Bean 사용하기 (Pure Java App 또는 Servlet App) 황제낙엽 2009.10.22 233
75 Cugain의 샘플프로젝트 jpetstore 분석기 - (5) applicationContext.xml 분석 황제낙엽 2007.04.21 232
74 스프링 개발팁 황제낙엽 2007.08.17 223
73 Spring Boot에서의 RESTful API 모듈 file 황제낙엽 2020.04.16 216
72 SimpleFormController 정리 황제낙엽 2007.09.19 206
71 Spring 과 Spring Boot의 차이 file 황제낙엽 2020.05.26 202
70 Spring의 Exception 황제낙엽 2007.09.17 194
69 Spring MVC 어플리케이션 개발 <11> 간단한 조회 구현 방안 비교 황제낙엽 2007.05.27 164
68 스프링 2와 JPA 시작하기 (한글) 황제낙엽 2007.08.27 142
67 [Spring3.1.1] ResponseBody 한글깨짐 해결법 황제낙엽 2018.08.08 140
66 웹 개발의 변화와 스프링 황제낙엽 2008.03.19 132
65 Spring AOP - Pointcut 황제낙엽 2007.10.02 129
64 유효성체크 (org.springframework.validation.Validator) 황제낙엽 2007.08.17 129