Java Spring不使用XML上声明的注释bean进行自动连接
我正在尝试将一个在XML上定义的bean注入到一个带注释的bean中,它只是带注释的,而不是在XML上声明的,我想这正是我缺少的东西,这里是我的*context.XML:Java Spring不使用XML上声明的注释bean进行自动连接,java,xml,spring,spring-mvc,ejb,Java,Xml,Spring,Spring Mvc,Ejb,我正在尝试将一个在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" 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>
<?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>
当我尝试访问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
按照上下文创建的步骤,我看不到任何bean,而当springs基于xml创建上下文时,注释就看不到,因为我可以看到wre创建的所有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"
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>
<?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>
正如我所说,带注释的bean无法看到XML bean,因此spring无法自动关联它们
编辑3
我没有@Configuration
bean(我没有使用AnnotationConfigApplicationContext),所有配置都在XML中,如果我尝试创建一个,服务器不会启动,它是一个带有spring 3.2和EJB3.0的遗留系统,我现在无法更改这一方面
提前谢谢 我认为您没有为
UserBusiness
类指定@Component
注释
编辑:
你能将
组件扫描
配置为com.app.common
而不是com.app.common.jsf
吗?看起来有效的方法是创建一个@Configuration
导入包含bean声明的xml(使用@ImportResource
),而不是用xml扫描它的包。
出于某种原因,如果我在XML中声明该文件,服务器就不会启动,这很奇怪,因为我在任何地方都没有使用注释配置的定义。可以显示完整的stacktrace吗?Spring通常会说明自动连接失败的原因。在xml中,userBusiness的类型为
LocalStatelessSessionProxyFactoryBean
,但在java中,您使用的是com.app.common.business.userBusiness
。在xml中,您告诉spring将LocalStatelessSessionProxyFactoryBean
注册为名为userBusiness
的bean,在java中,您自动连接不同的类com.app.common.business.userBusiness
Hi@JaySmith,LocalStatelessSessionProxyFactoryBean是InjectEJB专用的bean,它处理类型转换。UserBusiness接收UserBusinessImpl无状态EJB。@JoelSantos您在哪里导入XML文件以让Spring创建其中定义的bean?我相信您缺少bean组件scanHi@anup0513,它在XML上声明,是一个使用Spring创建的EJBLocalStatelessSessionProxyFactoryBean@JoelSantos你能试试编辑吗?我认为组件扫描是为我尝试过的com.app.common.jsf
配置的,但是由于SpringEJB集成,它遇到了同样的问题。UserBusiness有@Autowired
bean,因为它们看不到映射到XML上的内容,所以我收到了与其他字段相同的异常。