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);
}
}