比如我有一个filter,继承OncePerRequestFilter或者直接实现http filter
然后我通过下面的方式可以获取到定义在applicationContext.xml中的bean
WebApplicationContext web = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
SystemFacade systemFacade = (SystemFacade)web.getBean("systemFacade");但是实际的情况是SystemFacade是一个注解方式配置的,而不是xml方式配置的。有什么方法可以获取到注解方式定义的bean吗?

解决方案 »

  1.   

    注解方式定义的,依然要配XML文件让Spring去搜索。你可以在applicationContext.xml文件中做修改,参考代码如下:<?xml version="1.0"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  <context:component-scan base-package="beans" />
        
    </beans>
    你需要将base-package改成你自己Bean类所在的包。
      

  2.   

    我是有这句话的。不然不可能注解不可用。
    我的主键现在都是正常。
    我要解决的问题简单的说就是在filter内如何获取到使用annotation方式定义的bean