如何在派生类

时间:2015-11-10 07:03:39

标签: java spring spring-transactions

我有一个抽象类,它作为我项目中所有Bean的基础。

public abstract class BaseBean
{
    /**
     * This method is the template method which executes the life cycle of the
     * Bean. Following is the life cycle for the Bean:- <br>
     * <h1>preExecute<br> <h1>executeImpl <br> <h1>postExecute<br>
     */
    public void exec()
    {
        log.debug("Entering exec method");
        try
        {
            log.debug("Executing preExecute");
            preExecute();
            log.debug("Done executing preExecute");

            log.debug("Executing Service");
            execImpl();
            log.debug("Done executing Service");

            log.debug("Executing postExecute");
            postExecute();
            log.debug("Done executing postExecute");

        }
        catch (Exception e)
        {
            log.error("Exception thrown " + e.getMessage());
            throw new ApplicationException(e.getMessage(), e);
        }
        finally
        {

        }
        log.debug("Exit exec method");
    }

    /**
     * This method needs to be overridden by extending classes and should
     * contain the Business logic for the bean.
     */
    protected abstract void execImpl();

}

我有几个bean可以处理Transactional的东西,即需要与Db交互,少数几个不需要与Db进行任何交互。

我需要我的派生Beans为Transactional 。当我在Derived Class上放置@Transactional注释时,它会给出“无法获取当前线程的事务同步会话”异常

但是当我在Base Bean上放置@Transactional注释时,它运行正常。我可以在Stack Trace中看到,当我将@Transactional放在Base Bean Class中时,它会将Transactional Code添加到我的exec方法中。

我想避免在我的基类中使用@Transactional,因为这意味着我的项目中从该类扩展的所有bean都将具有事务代码。 我怎样才能做到这一点? 下面是我的Bean的例子,它给出了一个错误。 如果我将此注释放在BaseBean上,它可以正常工作。 我的&#34;组件扫描base-package =&#34;已正确设置为包含所有派生bean。

@org.springframework.transaction.annotation.Transactional
public class LoadData extends FrameworkBean
{

    Class tempClass;


    @Override
    public void execImpl()
    {
            List retList = commonDao.loadDatabaseEntity(tempClass);

        }

    }
}

0 个答案:

没有答案