0 0

spring 容器外的实例如何得到spring中的bean??5

WebApplicationContextUtils. getWebApplicationContext(servletContext).getBean(beanName);???这个貌似不行,是不是因为实例没有在spring中注册,所以得不到servletContex?
2009年4月14日 22:56

5个答案 按时间排序 按投票排序

0 0

由于用到了WebApplicationContext,你在web.xml里面需要写

<context-param>    
    <param-name>contextConfigLocation</param-name>    
    <param-value>classpath:/applicationContext.xml</param-value>    
</context-param>    
<listener>    
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
</listener>
[color=red]<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>[/color]

红色这段要加上

2009年4月29日 09:18
0 0

在web.xml配置

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:/applicationContext.xml</param-value>  
</context-param>  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener> 

下面是在struts1.x Action中的用法
WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext()).getBean("userService");

2009年4月15日 16:58
0 0

配置见楼上, 但是这个方法要拿到servletContext才有用哦!

2009年4月15日 12:27
0 0

使用监听器加载spring配置文件

  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/applicationContext.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

使用WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("beanName");可以过去到bean实例

2009年4月15日 10:13
0 0

这个是要你在web.xml文件中配置一个listener或servlet启动时加载spring的bean后,才能用WebApplicationContextUtils. getWebApplicationContext(servletContext).getBean(beanName);得到它的bean

2009年4月15日 01:13

相关推荐

Global site tag (gtag.js) - Google Analytics