Results 1 to 9 of 9

Thread: problem with pointcut="@annotation(..."

  1. #1
    Join Date
    Feb 2008
    Posts
    9

    Default problem with pointcut="@annotation(..."

    Hi,

    I am fairly new to spring

    My dao interface
    Code:
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    
    public interface VehicleDao {
    	
    	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = VehicleException.class, readOnly = false)
    	public Vehicle addVehicle(String name, String subType,
    			NumberOfPassengers num) throws VehicleException;
    //other methods
    }
    I am having a exception translator to translate DataAccessException to my AppException
    Code:
    @Aspect
    public class ExceptionTranslator implements AfterAdvice{
        @AfterThrowing(throwing="e",pointcut="@annotation(org.springframework.transaction.annotation.Transactional)")
        public void rethrow( DataAccessException e ) throws VehicleException {
        	System.out.println("DAE:"+e.getRootCause());
    //my code
            throw new VehicleException();
        }
    But the rethrow method is not called.

    If I use my @AfterThrowing like:
    Code:
    @AfterThrowing(throwing="e",pointcut="execution(* *.add*(..))")
    instead of
    Code:
     @AfterThrowing(throwing="e",pointcut="@annotation(org.springframework.transaction.annotation.Transactional)")
    everythings working fine.

    Can anyone please explain me the reason behind this behaviour and how to solve this?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Instead of @annotation try @target...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2008
    Posts
    9

    Default

    Thanks for the response.. but that doesnt work either...

    Code:
    @AfterThrowing(throwing="e",pointcut="@target(org.springframework.transaction.annotation.Transactional)")

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Why do you implement AfterAdvice you are using AspectJ so no need for that interface... Next to that please post your aop configuration...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2008
    Posts
    9

    Default

    This is my application context:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    						http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    						http://www.springframework.org/schema/aop
    						http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    						http://www.springframework.org/schema/tx
    						http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    	<bean id="vehicleDao"
    		class="vehicle.dao.VehicleDaoJPAImpl">
    		<property name="entityManagerFactory"
    			ref="entityManagerFactory" />
    	</bean>
    
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="vehicle" />
    	</bean>
    
    	<bean
    		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    	<bean
    		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<bean id="jpaDialect"
    		class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"
    		autowire="no" />
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory"
    			ref="entityManagerFactory" />
    		<property name="jpaDialect" ref="jpaDialect" />
    	</bean>
    
    	<bean class="vehicle.exception.util.ExceptionTranslator" />
    
    	<aop:aspectj-autoproxy />
    
    </beans>
    Am I missing something here?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You have annotated your interfaces, you must annotate your implementing class. Annotations on the interfaces aren't inherited to the implementation. So I also expect you have transactional issues or will run into them soon...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Feb 2008
    Posts
    9

    Default

    Thanks a ton, for the reply.

    Well, I had some transactional problems and thats why I have the annotations to the interface.

    I was giving this a thought. Transactions should be applied on my service layer. In case I want my DataAccessException to be translated to my AppException in the DAO layer, there is no point in where I can specify the pointcut for @transactional.

    And what is the best procedure to be followed? Translate the exceptions at dao layer or service layer?

    And also if you suggest to use it in my service layer, I t would be great if you can provide the way to solve the forthcoming transaction issues...

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You have to have the annotations on the implementing class, on the interface is not going to work (as you noticed with your after throws advice). I think the translation should happen between your data and service layer so after the call to your dao, that way your service remains free of dataacessexception handling.

    Also you are using annotations, if you where to use Spring 2.5 you could annotate your dao's with the @Repository and use that for your ExceptionTranslation filter.

    But the first thing to do is to move your annotations to your implementing classes.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Feb 2008
    Posts
    9

    Default

    Thanks for the help..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •