java - aop :aspectj-autoproxy and tx:annotation-driven conflict in spring

标签 java spring spring-mvc aspectj spring-aop

这是我使用 Spring AOP 的第一个项目。添加时出现问题

<aop:aspectj-autoproxy proxy-target-class="true" /> 

在我的 spring-servlet.xml 中。如果我从上下文文件中评论这一行,应用程序运行正常并且没有错误。

如果我添加此行,则会引发异常。

SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myItemController' defined in file [C:\Users\mmunaf\spring_boot_MSes_In30Minutes\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\ItemServices\WEB-INF\classes\com\mywmos\itemservices\controller\MyItemController.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot resolve reference to bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' while setting bean property 'transactionAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: List [Xlint:invalidAbsoluteTypeName]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:479)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1230)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1174)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1066)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5409)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5707)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

这是我的方面类。

@Aspect
@Component
public class MyItemDaoLoggingAspect {

    //this is wher we add all aspect logic 

    //@Before advice

    @Before("execution(public List getAllItems())")
    public void beforeVaalidateMyItem() {
        System.out.println(" ========> In MyItemDaoLoggingAspect");
        System.out.println(" ========> Executing beforeAscpect for validateMyItem");
    }
}

这是我的 dao 方法。

public List getAllItems() {
    Session session = sessionFactory.getCurrentSession();
    return session.createCriteria(MyItem.class).list();
}

这是我的应用程序配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


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

    <mvc:annotation-driven />
    <tx:annotation-driven />
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="${oracle.driver-calss}" />
        <property name="username" value="${oracle.userName}" />
        <property name="password" value="${oracle.password}" />
        <property name="url" value="${oracle.Url}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan"
            value="com.mywmos.itemservices.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${oracle.hbm2ddl}</prop>
                <prop key="hibernate.dialect">${oracle.dialect}</prop>
                <prop key="hibernate.show_sql">${oracle.show_sql}</prop>
            </props>
        </property>
    </bean>


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="propertyPlaceHolderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>db.properties</value>
        </property>
    </bean>

    <bean id="appCtxBProvider"
        class="com.mywmos.itemservices.util.ApplicationContextProvider" />


    <bean id="myCollDemoBean"
        class="com.mywmos.itemservices.model.CollectionsDemo">

        <property name="mySet">
            <set>
                <value>1</value>
                <value>2</value>
                <value>${oracle.show_sql}</value>
            </set>
        </property>
    </bean>
</beans>

如果您需要任何其他详细信息,请告诉我。 谢谢。

最佳答案

有两件事可能会导致代码出现问题。

1.您还没有定义完全限定的方法名称。至少需要通配符。

2.您已在切入点中将访问修饰符指定为<​​strong>public,并且对于代理机制,您已使用proxy-target-class="true"强制spring使用CGLIB代理机制。但根据“https://tenmilesquare.com/using-gclib-with-proxy-target-class-true/”,CGLIB不会代理任何公共(public)方法。

所以我建议将切入点更改为以下内容。

“执行(* packageName.*.getAllItems(..))”

或者尝试在切入点中使用完全限定的方法名称以及 proxy-target-class="false"

有关切入点签名的更多详细信息,请参阅第 7.2.3.4 点 https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch07s02.html

关于java - aop :aspectj-autoproxy and tx:annotation-driven conflict in spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54975753/

相关文章:

java - 如何创建集合的数组?

java - Eclipse:如何更改方法声明中返回类型的颜色

java - 在 JFrame.invalidate 中调用 pack() 是一个坏主意吗?

spring - 在 CloudFoundry 上为 Redis 实例配置哨兵节点

java - 配置 org.hibernate.SQL 记录器时,Logback 显示 DEBUG 输出,根目录位于 INFO 级别

java - 如何在 Eclipse 中创建 Spring MVC 项目?

java - 打印出 TreeMap<String, TreeSet<String>> 键和映射值

spring - 如何使用 Spring Boot 1.5.2 注入(inject) Jersey 端点接口(interface)的实现?

spring - Thymeleaf 中的有条件关闭标签

java - 将 Handler 注入(inject)注解 Controller