Announcement Announcement Module
Collapse
No announcement yet.
Spring, Interceptors and AspectJ Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, Interceptors and AspectJ

    hello all,
    i have written an application that uses Spring and Hibernate to retrieve some data from database...
    I have implemented AOP around advice using spring aop functionality, but now (just for trial), i wanted to replace spring AOP with AspectJ.
    for spring AOP, i had to define proxies and interceptors (also for transaction purposes)...
    my applicationContext.xml is as follows

    <beans>

    <!-- ========================= GENERAL DEFINITIONS ========================= -->



    <!-- Configurer that replaces ${...} placeholders with values from properties files -->
    <!-- (in this case, mail and JDBC related properties) -->

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">

    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/menagerie?autoReconnect=true</value>
    </property>

    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>mypass</value>
    </property>
    </bean>

    <bean id="stockDataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">

    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/informa?autoReconnect=true</value>
    </property>

    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>mypass</value>
    </property>
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>com/myapp/hibernate/HibernateEntry.hbm.xml</value>
    <value>com/myapp/hibernate/HibernateExpenseType.hbm.xml</value>
    <value>com/myapp/hibernate/HibernateUser.hbm.xml</value>
    <value>com/myapp/hibernate/HibernateStock.hbm.xml</value>

    </list>
    </property>
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.M ySQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    </bean>

    <bean id="stockSessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>de/informa/impl/hibernate/Items.hbm.xml</value>

    </list>
    </property>
    <property name="dataSource">
    <ref local="stockDataSource"/>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.M ySQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    </bean>


    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="stockTransactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref bean="stockSessionFactory"/>
    </property>
    </bean>


    <!-- Persistence Layer -->

    <bean id="persistenceManager"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">

    <property name="proxyInterfaces">
    <value>com.myapp.common.PersistenceManager</value>
    </property>
    <property name="target">
    <ref local="persistenceManagerTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="transactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>

    <property name="preInterceptors">
    <list>
    <ref local="beforeAdvisor"/>
    <ref local="afterAdvisor"/>
    <ref local="queryAdvisor"/>
    </list>
    </property>
    </bean>



    <bean id="persistenceManagerTarget" class="com.myapp.common.PersistenceManagerImpl">
    <property name="budgetDAO">
    <ref local="budgetDAO"/>
    </property>

    </bean>


    <bean id="stockPersistenceManager" class="com.myapp.common.StockPersistenceManagerImp l">
    <property name="stockDAO">
    <ref local="stockDAO"/>
    </property>

    </bean>


    <bean id="stockDAO"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="proxyInterfaces">
    <value>com.myapp.common.StockDAO</value>
    </property>
    <property name="target">
    <ref local="stockDAOTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="stockTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    <property name="preInterceptors">
    <list>
    <ref local="stockAroundAdvisor"/>
    </list>
    </property>

    </bean>

    <!-- AOP -->


    <bean id="beforeAdvice" class="com.myapp.aop.TracingBeforeAdvice"/>
    <bean id="afterAdvice" class="com.myapp.aop.TracingAfterAdvice"/>
    <bean id="insertAdvice" class="com.myapp.aop.InsertBeforeAdvice"/>
    <bean id="stockAroundAdvice" class="com.myapp.aop.StockAroundAdvice"/>
    <bean id="tracingInterceptor" class="com.myapp.aop.TracingInterceptor"/>




    <bean id="beforeAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="beforeAdvice"/>
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>

    <bean id="afterAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="afterAdvice"/>
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>

    <bean id="queryAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="insertAdvice"/>
    </property>
    <property name="pattern">
    <value>com.myapp.common.PersistenceManager.insert* </value>
    </property>
    </bean>

    <bean id="stockAroundAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="stockAroundAdvice"/>
    </property>
    <property name="pattern">
    <value>com.myapp.common.StockDAO.query*</value>
    </property>
    </bean>



    <bean id="aroundAspect"
    class="com.myapp.aop.QueryAspect"
    factory-method="aspectOf"/>

    <bean id="tracingAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="tracingInterceptor"/>
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>


    </beans>


    the problem is my HibernateStockDAOImpl./
    i am using an advisor containing an around advice, using spring aop functionality.

    I have written a similar aspect using aspect j, and here is the code

    ---CODE----
    public aspect QueryAspect {


    pointcut query(com.myapp.hibernate.HibernateStockDAOImpl dao) : call(* com.myapp.hibernate.HibernateStockDAOImpl.*(..))
    && target(dao);


    before(com.myapp.hibernate.HibernateStockDAOImpl dao): query(dao) {
    System.err.println("--------- BEFOREAspectJ around advice....." + thisJoinPoint);

    }

    after(com.myapp.hibernate.HibernateStockDAOImpl dao): query(dao) {
    System.err.println("--------- AFter around advice....." + thisJoinPoint);

    }
    }


    I was expecting that my AspectJ before & after advices were going to be called every time HibernateStockDAOImpl method gets called (so, even before the query method defined for the StockAroundAdvice.

    Instead, i found out that they are called ONLY from my StockAroundAdvice.around advice.

    Only guess that i can make is that behaviour is like this because my HibernateStockDAOImpl is wrapped in a Proxy ...
    But if i don't wrap it in a proxy, i will lose AOP transaction functionality...

    anyone can help? hope i was clear enough to explain my problem..

    thanx in advance and regards
    marco
Working...
X