java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate

后端 未结 4 1548
[愿得一人]
[愿得一人] 2021-02-10 01:49

I am trying to implement this tutorial about Declarative Transaction in Spring Framework application but don\'t work because when I try to execute the MainApp c

相关标签:
4条回答
  • 2021-02-10 02:25

    I was able to execute without changing the <aop:config>, just by getting bean instance from the StudentDAO interface.

    <aop:config>
          <aop:pointcut id="createOperation" 
          expression="execution(* com.tutorialspoint.StudentJDBCTemplate.create(..))"/>
          <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"/>
    </aop:config>
    
     StudentDAO studentJDBCTemplate = (StudentDAO)context.getBean("studentJDBCTemplate");
    
    0 讨论(0)
  • 2021-02-10 02:25

    You should use the interface type in aop pointcut expression i.e in following line:-

    expression="execution(* org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate.create(..))" />
    

    use the below code:-

    expression="execution(* org.andrea.myexample.myDeclarativeTransactionSpring.StudentDAO.create(..))" />
    

    Spring supports AOP via proxying which is of two type Interface based (Proxy implements all the interfaces implemented by target class) and Class based ( achieved by subclassing the target class).

    0 讨论(0)
  • 2021-02-10 02:25

    In my case it was resolved as below:

    Util.clobToString((Clob)item[index])
    
    0 讨论(0)
  • 2021-02-10 02:37

    Option 1, change your configuration to inject transactions at the interface level:

    <aop:config>
        <aop:pointcut id="createOperation"
            expression="execution(* org.andrea.myexample.myDeclarativeTransactionSpring.StudentDAO.create(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation" />
    </aop:config>
    

    and obtain the bean as an instance of that interface:

    StudentDAO studentDao = (StudentDAO) context.getBean("studentJDBCTemplate");
    

    Option 2, indicate that the proxy should extend the target class using proxy-target-class attribute:

    <aop:config proxy-target-class="true">
        ...
    </aop:config>
    

    The first option is the cleaner one, but frankly I'd prefer to use@Transactional annotations rather than AOP declarations within the Spring bean XML. It's sometimes diffucult to get the latter correct and if you don't have specific transactionality tests on your components, you will not necessarily notice that things are incorrect.

    0 讨论(0)
提交回复
热议问题