봄 외부 주입 빈 (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");
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.