본문 바로가기
IT

Spring Framework 2.5 에서 Class Type 으로 Bean을 찾는 예제

by 내꿈은한량 2010. 10. 4.
/**
 *
 */
package com.skt.pmp.serviceportal.home;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.skt.pmp.servicedomain.service.common.pps.PpsServiceUtil;/**
 * @author 김종현
 *
 */
public class SpringTestServlet extends HttpServlet {    /**
     *
     */
    private static final long serialVersionUID = -6114116274083741606L;    private void testAppCtx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
       
        int beanCount = context.getBeanDefinitionCount();
        String[] beanNames = context.getBeanDefinitionNames();
//        String[] beanNames = context.getBeanNamesForType(PpsService.class);
      
        PrintWriter out = response.getWriter();
       
        out.println("---------------------------------<br>");
        out.println("Bean Name (total:"+beanCount+")<br>");
        out.println("---------------------------------<br>");
        out.println("<table border='1'>");
        int idx=1;
        for(String beanName : beanNames) {
            out.println("<tr>");
            out.println("<td>"+ idx + "</td>" );
            out.println("<td>"+beanName + "</td>" );
            try {
                Object bean = context.getBean(beanName);
                out.println("<td>"+bean.toString() + "</td>");
            }
            catch(Exception ex) {
                out.println("<td> error!! " + ex.getMessage() + "</td>");
            }
            out.println("</tr>");
            idx++;
        }
        out.println("</table>");
        out.println("---------------------------------<br>");
    }
   
   
   
    protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());        if( "1".equals( request.getParameter("type") ) ) {
            testAppCtx(request, response);
        }
        else {
            Object bean = BeanFactoryUtils.beanOfType(context, PpsServiceUtil.class);            PrintWriter out = response.getWriter();
           
            out.println( bean );
        }
    }
   
    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        processRequest(request, response);
    }    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        processRequest(request, response);
    }   
}

'IT' 카테고리의 다른 글

이클립스 편집 창 전환 키 추가  (0) 2011.04.07
Timezone 리스트  (1) 2011.03.31
톰켓을 사용하는데 필요한 20가지 Tips  (0) 2010.01.21
Confluence 설치 시 먹통(hang) 될때.  (0) 2009.06.29
Versioning Numbering Concepts  (0) 2009.05.26