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
63 [Spring3.1.1][4] RestTemplate 한글 문제 황제낙엽 2018.08.08 89
62 [Spring3.1.1][3] RestTemplate 한글 문제 황제낙엽 2018.08.08 237
61 [Spring3.1.1][2] RestTemplate 한글 문제 황제낙엽 2018.08.08 113
60 [Spring3.1.1][1] RestTemplate 한글 문제 황제낙엽 2018.08.08 683
59 [Spring3.1.1] Eclipse 에 Spring Framework 환경 구축하기 file 황제낙엽 2018.08.08 90
58 웹 개발의 변화와 스프링 황제낙엽 2008.03.19 132
» Spring MVC 가 아닌 환경에서 Spring Pojo Bean 사용하기 (Pure Java App 또는 Servlet App) 황제낙엽 2009.10.22 233
56 NamedParameterJdbcDaoSupport 몇가지 장점 황제낙엽 2007.11.27 101
55 프로젝트의 기본이 되는 Logging, Exception 처리 전략 황제낙엽 2007.01.30 85
54 Spring AOP - Pointcut 황제낙엽 2007.10.02 129
53 <spring:checkbox> tip! 황제낙엽 2007.10.01 378
52 SimpleFormController 정리 황제낙엽 2007.09.19 206
51 Spring의 Exception 황제낙엽 2007.09.17 194
50 스프링 2와 JPA 시작하기 (한글) 황제낙엽 2007.08.27 142
49 스프링 개발팁 황제낙엽 2007.08.17 223
48 유효성체크 (org.springframework.validation.Validator) 황제낙엽 2007.08.17 129
47 Spring 2.0의 XML확장기능 (3) 황제낙엽 2007.08.15 32
46 Spring 2.0의 XML확장기능 (2) 황제낙엽 2007.08.15 73
45 Spring 2.0의 XML확장기능 (1) 황제낙엽 2007.08.15 33
44 CSS와 XHTML을 사용한 효율적인 View 개발 전략 황제낙엽 2007.01.30 104