Spring不使用注释在XML上声明的bean自动装配

时间:2017-07-03 20:04:25

标签: java xml spring spring-mvc ejb

我试图将在XML上定义的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无法看到声明的那个,但是搜索并且似乎我只需要将注释配置添加到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

按照上下文创建的步骤,我看到注释中没有注册的bean只是当spring创建基于xml的上下文时,我可以看到所有创建的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的遗留系统,我现在无法改变这一方面。

提前致谢!

2 个答案:

答案 0 :(得分:0)

我认为您错过了为my.secret.password=${SUPER_SECRET_ENV_VARIABLE}

指定@Component注释

编辑:

您可以将UserBusiness配置设为component-scan而不是com.app.common

答案 1 :(得分:0)

似乎有用的是创建一个@Configuration导入具有bean声明的xml(带有@ImportResource)并且不用XML扫描它的包。 出于某种原因,如果我在XML中声明文件,服务器无法启动,那很奇怪,因为我没有使用注释配置的任何定义。