SpringBeanAutowiringInterceptor 抛出 NoSuchBeanDefinitionException
SpringBeanAutowiringInterceptor throwing NoSuchBeanDefinitionException
我正在尝试使用 @Interceptors(SpringBeanAutowiringInterceptor.class)
注释将 bean 自动装配到具有 Spring 的 EJB。问题是当 EJB 被实例化时,我一直在为自动装配的 classes 获取 NoSuchBeanDefinitionException
。 (我的示例中的过滤器 class)。
我正在使用 Java EE 6 应用程序 (EJB 3.0),Spring 4.2.2 由 Maven 管理,运行 在 WebSphere 7 AS 中。
我有以下 Spring 依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.4.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
实施:
@Stateless
@Remote(ServiceRemote.class)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class Service implements ServiceRemote {
@Autowired
private Filter filter;
...
}
Class 自动装配
@Component
public class FilterImpl implements Filter { ... }
SpringBeanAutowiringInterceptor
要查找的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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="businessBeanFactory"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:applicaion-context.xml" />
</bean>
</beans>
具有 bean 定义的应用程序-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.FilterImpl" />
</beans>
两个 XML 都在 class 路径中,就像它们在 src/main/resources/.
中一样
我尝试使用以下代码进行一些调试,但找不到有用的东西。
BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
BeanFactoryReference ref = factoryLocator.useBeanFactory("businessBeanFactory");
BeanFactory factory = ref.getFactory();
FilterImpl instance = factory.getBean(FilterImpl.class);
当 EJB 首次实例化时,两个上下文似乎都已加载,因为我得到如下日志。
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7730773; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from URL [file:/C:/project/service-module/target/classes/beanRefContext.xml]
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5580558: root of context hierarchy
但是在那之后Spring找不到我在application-context.xml
中定义的bean。
在 application-context.xml
中,我尝试使用 <context:component-scan base-package="com"/>
而不是 <bean class="com.FilterImpl" />
,但我得到了相同的结果。
我是不是缺少一些配置?
编辑:添加了 Steve C 的建议和上下文加载日志。
将您的 xml 配置文件从 src/main/java
移动到 src/main/resources
。
src/main/java
中的文件仅由 maven-compiler-plugin 处理。
其他资源文件由 maven-resources-plugin 处理,它从 src/main/resources
.
复制文件
您似乎没有在 spring xml 文件中添加 <context:annotation-config/>
我刚刚找到了答案。在 <constructor-arg value="classpath*:applicaion-context.xml" />
中有一个 applicaion-context.xml
而它应该是 application-context.xml
。当我编辑问题并发布有关加载上下文的日志时,我注意到只有 beanRefContext.xml
加载了它的定义。没有关于某些解析错误的日志,我只是忽略了这种可能性。现在一切正常。
我正在尝试使用 @Interceptors(SpringBeanAutowiringInterceptor.class)
注释将 bean 自动装配到具有 Spring 的 EJB。问题是当 EJB 被实例化时,我一直在为自动装配的 classes 获取 NoSuchBeanDefinitionException
。 (我的示例中的过滤器 class)。
我正在使用 Java EE 6 应用程序 (EJB 3.0),Spring 4.2.2 由 Maven 管理,运行 在 WebSphere 7 AS 中。
我有以下 Spring 依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.4.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
实施:
@Stateless
@Remote(ServiceRemote.class)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class Service implements ServiceRemote {
@Autowired
private Filter filter;
...
}
Class 自动装配
@Component
public class FilterImpl implements Filter { ... }
SpringBeanAutowiringInterceptor
要查找的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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="businessBeanFactory"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:applicaion-context.xml" />
</bean>
</beans>
具有 bean 定义的应用程序-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.FilterImpl" />
</beans>
两个 XML 都在 class 路径中,就像它们在 src/main/resources/.
中一样我尝试使用以下代码进行一些调试,但找不到有用的东西。
BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
BeanFactoryReference ref = factoryLocator.useBeanFactory("businessBeanFactory");
BeanFactory factory = ref.getFactory();
FilterImpl instance = factory.getBean(FilterImpl.class);
当 EJB 首次实例化时,两个上下文似乎都已加载,因为我得到如下日志。
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7730773; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from URL [file:/C:/project/service-module/target/classes/beanRefContext.xml]
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5580558: root of context hierarchy
但是在那之后Spring找不到我在application-context.xml
中定义的bean。
在 application-context.xml
中,我尝试使用 <context:component-scan base-package="com"/>
而不是 <bean class="com.FilterImpl" />
,但我得到了相同的结果。
我是不是缺少一些配置?
编辑:添加了 Steve C 的建议和上下文加载日志。
将您的 xml 配置文件从 src/main/java
移动到 src/main/resources
。
src/main/java
中的文件仅由 maven-compiler-plugin 处理。
其他资源文件由 maven-resources-plugin 处理,它从 src/main/resources
.
您似乎没有在 spring xml 文件中添加 <context:annotation-config/>
我刚刚找到了答案。在 <constructor-arg value="classpath*:applicaion-context.xml" />
中有一个 applicaion-context.xml
而它应该是 application-context.xml
。当我编辑问题并发布有关加载上下文的日志时,我注意到只有 beanRefContext.xml
加载了它的定义。没有关于某些解析错误的日志,我只是忽略了这种可能性。现在一切正常。