由於權限緣由要在filter裏使用spring- service所產生的問題

參考:web

加載順序 http://blog.csdn.net/wayfoon322/article/details/2418011   spring

filter 使用注入bean  http://zy116494718.iteye.com/blog/1918131spa


1. 項目spring使用的是註解方式:在filter裏debug看源碼,類型裏.net

allBeanNamesByType ConcurrentHashMap<K,V>  (id=169) ,是有想要的roleService的,可是在以下代碼裏就是取不到。總報異常:debug

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'roleManageService' is definedcode


WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());xml

RoleManageService roleManageService = (RoleManageService) ac.getBean("roleManageService"); blog


2. 試了使用網上的各類接口:接口

實現ApplicationContextAware接口get

extends ContextLoaderListener implements  ServletContextListener

等等,仍是獲取不到bean實例


3. 最後試着將這個RoleManageService改爲了xml配置方式, 終於在filter裏獲取到了,仍是用1。的方法獲取,觀察下面這兩張debug的截圖

<bean id="roleManageService" class="com.ljq.service.RoleManageService" >
    <property name="roleManageDao">
        <ref bean="roleManageDao"/>
    </property>
</bean>

圖1         


圖2


圖1是使用註解的方式,allBeanNamesByType裏有role的實例,構造器沒有role實例,getBean的時候獲取異常。

圖2是使用XML配置的方式,allBeanNamesByType裏有role的實例,構造器有role實例,getBean的時候正常。


4.基於上述緣由,我想應該是在web加載順序的時候,

spring容器的註解方式在filter以後,致使獲取不到報異常。

spring容器的xml配置方式在filter以前,因此能獲取到。

so.將這些要使用在filter的實例改爲了xml配置方式,其它的仍是用註解

但我參考別人的文章,人家爲何能在filter裏獲取到註解的實例呢?哎,才蔬學淺好鬱悶。。

相關文章
相關標籤/搜索