봄 외부 주입 빈 (JAX - WS)

더 읽 기
      최근 SpringMvc 에 서 는 JAX - WS 를 통 해 웹 서 비 스 를 구현 하고 있 지만, 웹 서 비 스 는 Spring 용기 에 없 기 때문에 Spring 의 dao 가 필요 합 니 다. 태그 @ Autowired 등 을 통 해 얻 은 것 은 모두 null 입 니 다. 그러면 다음 두 가지 방법 으로 bean 을 얻 을 수 있 습 니 다.
 
 
첫 번 째 방법 (spring 4.0 테스트 통과):
웹. xml 등록
 
    org.springframework.web.context.ContextLoaderListener
  

 호출 방법
   obj=(  )ContextLoaderListener.getCurrentWebApplicationContext().getBean("Bean  ");


 
빈 이름 을 모 르 면 모두 열거 해 보 세 요.
String []strs= ContextLoaderListener.getCurrentWebApplicationContext().getBeanDefinitionNames();

for(int i=0;i 
  

 

, ,

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WebContextSpringBeanFactory extends javax.servlet.http.HttpServlet {

	public static WebApplicationContext webAppContext;

	public void init(ServletConfig config) throws ServletException {

		WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
		this.webAppContext = webAppContext;

	}

	/**
	 *  
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName) {
		System.out.println(webAppContext);
		return webAppContext.getBean(beanName);

	}

}

 
웹. xml 설정

		WebContextSpringBeanFactory
		WebContextSpringBeanFactory
		2
	

 어떻게 사용 합 니까?아주 간단 합 니 다.
인터페이스 obj = (인터페이스) WebContextSpringBeanFactory. getBean ("당신 의 Bean");
 

좋은 웹페이지 즐겨찾기