Abdul
Abdul

Reputation: 1208

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

this is my first project using spring AOP. There is a problem when i add

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

in my spring-servlet.xml. If i comment this line from the context files application runs fine and no errors.

If I add this line then an exception has been thrown.

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)

This is my aspect class.

@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");
    }
}

This is my dao method.

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

This is my application config file.

<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>

please let me know if you need any other details. Thank you.

Upvotes: 0

Views: 1466

Answers (3)

Abdul
Abdul

Reputation: 1208

This helped me. I changed the return type List to java.util.List.

How to intercept the return type List using the spring AOP pointcut expression

@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");
    }
}

Upvotes: 1

Nawnit Sen
Nawnit Sen

Reputation: 1038

There are two thing that can cause issue in your code.

1.You have not defined a fully qualified method name.at least wildcard would be required.

2.You have specified access modifier in your pointcut as public and for proxy mechanism you have used proxy-target-class="true" which forces spring to use CGLIB proxy mechanism .But as per "https://tenmilesquare.com/using-gclib-with-proxy-target-class-true/" CGLIB wont proxy any public method.

So i would suggest to change your pointcut to the following.

"execution(* packageName.*.getAllItems(..))"

or try with fully qualified method name in pointcut along with proxy-target-class="false"

For more detailed info about pointcut signature refer to point 7.2.3.4 on https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch07s02.html

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 1

This line is causing issue: @Before("execution(public List getAllItems())")

You need to remove access modifier and return type of the method used in execution and add fully qualified class name where that method is present. see below

@Before("execution(put fully qualified class name here.getAllItems())")

Upvotes: 0

Related Questions