spring - 使用 TX :annotation-driven prevents Autowiring a bean

标签 spring spring-mvc osgi transactional eclipse-virgo

我正在使用 Spring MVC 和 Virgo Webserver 在 OSGi 应用程序上开发一个模块。

在我的模块上,我有一个 Controller ,它可以访问一个管理器,它有一个负责处理报告生成的处理程序列表。

一切都很好,直到我不得不从外部服务调用事务方法。由于我的所有类(class)都不是事务性的,因此我必须添加对事务管理器和 annotation-driven 的引用。 .然后,我的经理不再收到通知。

我明白在使用 annotation-driven 时我所有的 bean 都必须实现一个公共(public)接口(interface)才能使代理机制工作。据我所知,所有类(class)都是(其中一个不是,但后来我改变了它)。

我的配置文件是:

捆绑上下文.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: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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="reportManager" class="reportmodule.manager.impl.ReportManagerImpl"/>
<bean id="mvpepReportHandler" class="reportmodule.manager.impl.MVPEPReportHandler"/>
<bean id="reportConfigDao" class="reportmodule.repository.impl.ReportConfigurationHibernateDAOImpl"/>
<bean id="oSGIChangeReportHandler" class="reportmodule.osgi.impl.OSGIChangeReportHandlerImpl"/>
<bean id="reportController"
class="reportmodule.controller.impl.ReportControllerImpl"/>
<bean id="reportControllerHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/module/reportController/**=reportController</value>
</property>
<property name="alwaysUseFullPath" value="true"></property>
</bean>
</beans>

我的 bundle-osgi.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:osgi-compendium="http://www.springframework.org/schema/osgi-compendium"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/osgi-compendium
http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium-1.2.xsd">

<osgi:reference id="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager" />
<osgi:reference id="sessionFactory" interface="org.hibernate.SessionFactory" />
<osgi:reference id="smaCoreUtilService" interface="core.util.service.SmaCoreUtilService" />

<osgi:service ref="reportControllerHandlerMapping"
interface="org.springframework.web.servlet.HandlerMapping"
context-class-loader="service-provider"
auto-export="interfaces"/>

<osgi:service interface="reportmodule.api.manager.ReportManager" ref="reportManager" auto-export="interfaces"/>

<osgi:service interface="reportmodule.api.manager.ReportHandler" ref="mvpepReportHandler" auto-export="interfaces"/>

<osgi:service interface="reportmodule.repository.ReportConfigurationDAO" ref="reportConfigDao" auto-export="interfaces"/>

<osgi:service interface="reportmodule.osgi.OSGIChangeReportHandler" ref="oSGIChangeReportHandler" auto-export="interfaces"/>

<osgi:list cardinality="0..N" id="reportHandler" interface="reportmodule.api.manager.ReportHandler" greedy-proxying="true">
<osgi:listener ref="oSGIChangeReportHandler" bind-method="register" unbind-method="unregister"/>    
</osgi:list>

</beans>

因此,在发布所有服务之后,调用 oSGIChangeReportHandler.register(我可以对其进行调试):

@Service(value="oSGIChangeReportHandler")
public class OSGIChangeReportHandlerImpl implements OSGIChangeReportHandler {

    private ReportManager reportManager;

    /**
     * @param reportManager the reportManager to set
     */
    @Autowired
    public void setReportManager(ReportManager reportManager) {
        this.reportManager = reportManager;
    }

    @SuppressWarnings("rawtypes")
    public void register(ReportHandler reportHandler, Map properties) {
        reportManager.addReportHandler(reportHandler);
    }

    @SuppressWarnings("rawtypes")
    public void unregister(ReportHandler reportHandler, Map properties) {
        reportManager.removeReportHandler(reportHandler);
    }


}

尽管调试器显示了 reportManager 的代理和 reportHandlerregister方法,调试器不会在 ReportManagerImpl.addReportHandler 上停止方法:

@Service(value="reportManager")
@Transactional(propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
public class ReportManagerImpl implements ReportManager {

    private ReportConfigurationDAO reportConfigurationDAO;

    private ArrayList<ReportHandler> reportHandlers = new ArrayList<ReportHandler>();

    /**
     * @param reportConfigurationDAO the reportConfigurationDAO to set
     */
    @Autowired
    public void setReportConfigurationDAO(ReportConfigurationDAO reportConfigurationDAO) {
        this.reportConfigurationDAO = reportConfigurationDAO;
    }

    @Override
    @Transactional
    public InputStream gerarRelatorio(ReportRequest repoReq) throws NegocioException {
        // Generates the report...
    }

    /* (non-Javadoc)
     * @see reportmodule.api.manager.ReportManager#addReportHandler(reportmodule.api.manager.ReportHandler)
     */
    @Override
    public void addReportHandler(ReportHandler handler) {
        if (handler != null) {
            this.reportHandlers.add(handler);
        }
    }

    /* (non-Javadoc)
     * @see reportmodule.api.manager.ReportManager#removeReportHandler(reportmodule.api.manager.ReportHandler)
     */
    @Override
    public void removeReportHandler(ReportHandler handler) {
        if (handler != null) {
            this.reportHandlers.remove(handler);
        }
    }

}

我必须强调,当我删除 tx:annotation-driven来自 bundle-context.xml 的标签文件,一切正常(handler 在启动期间被正确添加到列表中)。

那么,我在这里缺少什么?

最佳答案

问题解决了!

正如您在上面的代码中看到的,我通过 XML 定义了 bean。和 Annotation ,因此每个 bean 在运行时都被复制。然后,当我添加 tx:annotation-driven标记应用程序开始拦截错误的 bean。它确实是在通知一个 bean,但是一个孤儿 bean。

关于spring - 使用 TX :annotation-driven prevents Autowiring a bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426406/

相关文章:

java - 如何在 Spring mvc Controller 中设置默认处理程序方法?

java - OSGI 组件已满足,但仍未激活

java - 尝试在Spring MVC Controller 中使用entityManager时出现NullPointerException

spring - 测试 spring 应用程序上下文无法启动的最佳方法是什么?

java - Formatter<LocalDateTime> 未在 REST 应用程序中向 Spring Boot 容器注册

java - 如何像spring mvc一样设置动态变量并将其传递给JSF?

java - 如何捕获 redis.serializer.SerializationException

java - 如何在 SE 应用程序中引导 weld-osgi 版本 2

Java - 覆盖抽象方法

java - 信息 : Additional JARs have been added : 'xalan-2.7.0.jar'