[英]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.