[英]Spring not autowiring using annotation a bean declared on the XML
我試圖將在XML上定義的bean注入到帶注釋的bean中,它僅是帶注釋的,而不是在XML上聲明的,我認為這只是我的* context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
...
<bean id="userBusiness" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName" value="java:global/app-common/app-common-core/UserBusinessImpl" />
<property name="businessInterface" value="com.app.common.business.UserBusiness" />
</bean>
...
<context:annotation-config/>
<context:component-scan base-package="com.app.common.jsf" />
</beans>
這是組件:
@Component
public class AppAuthorization {
@Autowired
private UserBusiness userBusiness;
@Autowired
private AppLogin sabiusLogin;
...
@Local
public interface UserBusiness {
User listUserByFilter(User user) throws UserBusinessException;
...
@Stateless
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
@Interceptors({SpringBeanAutowiringInterceptor.class})
public class UserBusinessImpl implements UserBusiness {
@Autowired
private ProgramasUsuariosDao programasUsuariosDao;
...
當我嘗試訪問AppAuthorization時,Spring表示:
Could not autowire field: private com.app.common.business.UserBusiness com.app.common.jsf.AppAuthorization.userBusiness"
似乎帶注釋的Bean無法看到已聲明的Bean,但可以搜索,而且似乎只需要將注釋配置添加到XML中,對嗎? 希望有人能幫助我。
編輯
我認為這是堆棧跟蹤中最重要的部分:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.app.common.business.UserBusiness] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503)
... 201 more
按照上下文創建的步驟進行操作,我發現在springs基於xml創建上下文時,注解看不到任何已注冊的bean,因為我可以看到所有創建的bean。
編輯2
這是beanRefContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="contexts" class="com.app.common.spring.ClassPathXmlApplicationContext" />
</beans>
這是加載XML上下文文件的類:
public class ClassPathXmlApplicationContext extends org.springframework.context.support.ClassPathXmlApplicationContext {
public ClassPathXmlApplicationContext() {
super((System.getProperty("module.context.file").split(";")));
}
}
就像我說的那樣,帶注釋的bean無法看到XML,因此spring無法自動裝配它們。
編輯3
我沒有@Configuration
bean(我沒有使用AnnotationConfigApplicationContext),所有配置都在XML中,如果我嘗試創建一個配置,則服務器無法啟動,這是一個具有spring 3.2和EJB 3.0的舊系統。我現在無法更改此方面。
提前致謝!
我認為您錯過了為UserBusiness
類指定@Component
批注
編輯:
您能否將component-scan
配置設為com.app.common
而不是com.app.common.jsf
似乎有效的方法是創建一個@Configuration
導入具有bean聲明的xml(帶有@ImportResource
),而不掃描它的XML包。 由於某種原因,如果我在XML中聲明文件無法啟動服務器,這很奇怪,因為在使用注釋配置的任何地方都沒有定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.