简体   繁体   English

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

[英]Spring not autowiring using annotation a bean declared on the XML

I'm trying to inject a bean that was defined on a XML into an annotated, It is only annotated and not declared on XML, I think that is just something that I'm missing here is my *context.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>

Here's the component: 这是组件:

@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;
...

When I try to access the AppAuthorization Spring says that: 当我尝试访问AppAuthorization时,Spring表示:

Could not autowire field: private com.app.common.business.UserBusiness com.app.common.jsf.AppAuthorization.userBusiness"

Seems that the annotated bean can't see the declared one, but search and seems that I only needed to add the annotation-config to the XML, is this right? 似乎带注释的Bean无法看到已声明的Bean,但可以搜索,而且似乎只需要将注释配置添加到XML中,对吗? Hope some can help me. 希望有人能帮助我。

EDIT 编辑

I think that this is the most important part of the stack trace: 我认为这是堆栈跟踪中最重要的部分:

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

Following the steps on the context creation I see no bean registered tha can be seen by annotations just when springs creates the context based on the xml is that I can see all the beans that wre created. 按照上下文创建的步骤进行操作,我发现在springs基于xml创建上下文时,注解看不到任何已注册的bean,因为我可以看到所有创建的bean。

EDIT 2 编辑2

This is the beanRefContext.xml: 这是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>

This is the class that loads the XML context files: 这是加载XML上下文文件的类:

public class ClassPathXmlApplicationContext extends org.springframework.context.support.ClassPathXmlApplicationContext {

    public ClassPathXmlApplicationContext() {       
        super((System.getProperty("module.context.file").split(";")));
    }

}

As I said, the annotated beans cannot see the XML ones so, spring cannot autowire them. 就像我说的那样,带注释的bean无法看到XML,因此spring无法自动装配它们。

EDIT 3 编辑3

I don't have a @Configuration bean (I'm not using AnnotationConfigApplicationContext), all the config is in the XML and if I try to create one the server doesn't start, it's a legacy system with spring 3.2 and EJB 3.0 and I can't change this aspect now. 我没有@Configuration bean(我没有使用AnnotationConfigApplicationContext),所有配置都在XML中,如果我尝试创建一个配置,则服务器无法启动,这是一个具有spring 3.2和EJB 3.0的旧系统。我现在无法更改此方面。

Thanks in advance! 提前致谢!

I think you missed to specify @Component annotation for the UserBusiness class 我认为您错过了为UserBusiness类指定@Component批注

EDIT: 编辑:

Can you make the component-scan config to com.app.common instead of com.app.common.jsf 您能否将component-scan配置设为com.app.common而不是com.app.common.jsf

What seems to work was create a @Configuration import the xml that have the bean declaration (with @ImportResource ) and don't scan it's package in XML. 似乎有效的方法是创建一个@Configuration导入具有bean声明的xml(带有@ImportResource ),而不扫描它的XML包。 For some reason if I declare the file in the XML the server don't start, it's strange because I have no definition anywhere that I'm using an annotation configuration. 由于某种原因,如果我在XML中声明文件无法启动服务器,这很奇怪,因为在使用注释配置的任何地方都没有定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM