Java aop:aspectj autoproxy和tx:spring中注释驱动的冲突

Java aop:aspectj autoproxy和tx:spring中注释驱动的冲突,java,spring,spring-mvc,aspectj,spring-aop,Java,Spring,Spring Mvc,Aspectj,Spring Aop,这是我第一个使用SpringAOP的项目。当我添加时有一个问题 <aop:aspectj-autoproxy proxy-target-class="true" /> 这是我的课程 @Aspect @Component public class MyItemDaoLoggingAspect { //this is wher we add all aspect logic //@Before advice @Before("execution(pub

这是我第一个使用SpringAOP的项目。当我添加时有一个问题

<aop:aspectj-autoproxy proxy-target-class="true" /> 
这是我的课程

@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");
    }
}
这是我的刀法

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.您已经在切入点中将访问修饰符指定为public,对于代理机制,您使用了proxy target class=true,这迫使spring使用CGLIB代理机制。但是根据CGLIB,它不会代理任何公共方法

因此,我建议您将切入点改为以下内容

执行*packageName.*.getAllItems

或者尝试在切入点中使用完全限定的方法名以及代理目标类=false

有关切入点签名的更多详细信息,请参阅上的第7.2.3.4点

此行导致问题: @执行前公共列表getAllItems

您需要删除执行中使用的方法的访问修饰符和返回类型,并在存在该方法的地方添加完全限定的类名。 见下文


@在执行之前,将完全限定的类名放在这里。getAllItems

这对我很有帮助。我将返回类型列表更改为java.util.List


切入点有什么问题。它与aop tran Management有些冲突。@Abdul类AnnotationTransactionaAttribute资源将初始化为基于代理的aop for public方法,如javaDoc中所述。如果AOP的切入点定义有任何错误,它将失败。这就是我所理解的。我真的非常感谢你的时间兄弟。但我在这件事上犯了一些错误。1.没有给出完整的软件包也能正常工作。2. @Beforeexecutionpublic*getAllItems这也起作用,但当我提到返回类型为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>
@Aspect
@Component
public class MyItemDaoLoggingAspect {

    //this is wher we add all aspect logic 

    //@Before advice

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