首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

怎么在Listener或servlet中,使用 spring 使用注解定义的bean

2013-03-14 
如何在Listener或servlet中,使用 spring 使用注解定义的bean?请问如何在Listener或servlet中,使用 spring

如何在Listener或servlet中,使用 spring 使用注解定义的bean?
请问如何在Listener或servlet中,使用 spring 使用注解定义的bean?
我是用网上找的方法,通过WebApplicationContext取bean,但是,跟踪发现,WebApplicationContext中只有xml中定义的bean,我的bean都是用注解方式@Component定义的,请问如何在Listener或servlet中,使用 spring 使用注解定义的bean?

谢谢啊

这是定义的listener,用于替代spring的contextloaderListener


public class SpringLoaderListener extends ContextLoaderListener implements ServletContextListener { 
@Override
public void contextInitialized(ServletContextEvent event) { 
        super.contextInitialized(event); 
        ServletContext t1 = event.getServletContext();
        WebApplicationContext t2 = WebApplicationContextUtils.getWebApplicationContext(t1);
        SpringContextUtil.setApplicationContextStaticlly(t2); 
    }

@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub






package com.woods.common.util;


import org.springframework.beans.BeansException;   
import org.springframework.context.ApplicationContext;   
import org.springframework.context.ApplicationContextAware;   
import org.springframework.web.context.WebApplicationContext;

public class SpringContextUtil implements ApplicationContextAware {  
  
    // Spring应用上下文环境  
    private static WebApplicationContext applicationContext;  
  
    public static void setApplicationContextStaticlly(WebApplicationContext vApplicationContext)    
    {  
        SpringContextUtil.applicationContext = vApplicationContext;  
    }  
  
    

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {  
        SpringContextUtil.applicationContext = (WebApplicationContext) applicationContext;  
    }  
  

    public static ApplicationContext getApplicationContext() {  
        return applicationContext;  
    }  
  
    /** 
     * 获取对象 
     */  
    public static Object getBean(String name) throws BeansException {  
        return applicationContext.getBean(name);  
    }  
  



web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/applicationContext.xml,
WEB-INF/applicationContext-dbcp.xml
</param-value>
</context-param>

<!--


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerSSS</listener-class>
</listener>

<listener> 
       <listener-class>com.woods.common.util.SpringLoaderListener</listener-class> 
</listener> 



servlet中这样取bean

    public void init(){  
        String str = null;  
        if (str == null && profileScheThread == null) {  
        profileScheThread = (ProfileScheThread) SpringContextUtil.getBean("profileScheThread");  
        //profileScheThread.start(); // servlet 上下文初始化时启动 socket
        int a = 0;
        }  
    }  


"profileScheThread"是用@Component定义的bean,这个bean在spring mvc的其它功能中,正常装配。

@Component("profileScheThread")
public class ProfileScheThread {
@Autowired
@Qualifier("productProfileManager")
ProductProfileManager productProfileManager;
@Autowired
@Qualifier("itemInManager")
ItemInManager itemInManager;
    
    /*****************************************************************
     * 方法部分
     *****************************************************************/

public void run() {  
             ...
        }

    } 

[解决办法]

ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
ActivityService service = ctx.getBean(ActivityService.class);


@Service
public class ActivityService {

@Autowired
private ActivityDAO activityDAO;

public Long insert(Activity bean) throws DataAccessException {
return activityDAO.insert(bean);
}

}

[解决办法]
WebApplicationContext rwp = WebApplicationContextUtils.getRequiredWebApplicationContext(ServerConstants.getServletContext());
loginContextLoader = (LoginContextLoader)rwp.getBean("loginContextLoader");

listener对象不归spring管理,所以得手工获取对象

热点排行