【问题标题】:SpringBeanAutowiringInterceptor Not Injecting Spring Beans to Message Driven Bean : NullPointerExceptionSpringBeanAutowiringInterceptor 未将 Spring Bean 注入消息驱动 Bean:NullPointerException
【发布时间】:2014-12-11 05:38:49
【问题描述】:

我已经浏览了大部分资源并尝试使用以下机制将 Spring Bean 获取到我的 MDB。

    @MessageDriven(name = "FileMDB")
    @Interceptors(SpringBeanAutowiringInterceptor.class)
    public class FileMessageBean implements MessageListener {

        @Autowired
        private IContextLoader contextLoader;

        @Override
        public final void onMessage(final Message message) {

        }
    }

我的 beanRefContext.xml 位于 JAR 文件的类路径中,它是 WAR 文件的依赖项。

<?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
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg value="classpath:channel-integration-context.xml" />
    </bean>

</beans>

channel-integration-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Activates various annotations to be detected in bean classes: Spring's 
        @Required and @Autowired, as well as JSR 250's @Resource. -->
    <!-- <context:annotation-config /> -->

    <!-- AOP Annotation -->     
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- File Adapter -->
    <import resource="classpath:channel-dao-beans.xml" />

    <!-- JMS Beans -->
    <import resource="classpath:jms-beans.xml" />

    <!-- Data JNDI Beans -->
    <import resource="classpath:datasource-jndi-beans.xml" />

    <context:component-scan base-package="com.fdc.channelintegration" />

    <bean id="contextLoader" class="com.fdc.channelintegration.util.SpringContextLoader">
    </bean>
</beans>

我正在使用 Websphere 8.5,并且我的 MDB 正在正确触发。仍然没有将 contextLoader 注入到 MDB 中,我得到了 NullPointerException。

请帮帮我。

【问题讨论】:

    标签: java spring jms ejb-3.0


    【解决方案1】:
    1. 您是否忘记在您的 IContextLoader 类中添加 @Cmponent@Service 注释,以便 spring 注释处理器可以找到它?
    2. &lt;context:annotation-config /&gt; 被注释掉。将其返回到您的上下文文件并删除 @Interceptors(SpringBeanAutowiringInterceptor.class)
    3. 从上下文文件中删除 bean 的定义: &lt;bean id="contextLoader" class="com.fdc.channelintegration.util.SpringContextLoader"/&gt;

    【讨论】:

    • 谢谢,小问题。为什么要删除 SpringBeanAutowiringInterceptor?它假设将spring bean注入EJB对吗?有点困惑。
    • 应该启用注解 bean 定义阅读器。看起来您正在使用默认(单例)范围,因此应该在上下文启动时注入自动装配的依赖项。但如果它是 EJB,我可能会误会。
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多