Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: AOP not working as per point cut

  1. #1

    Default AOP not working as per point cut

    Hi there,

    I had created a logger with AOP as shown below

    Code:
    @Aspect
    public class LogInterceptor {
    
    	// @Pointcut("execution(* com.abc.xyz..*.*(..))")
    	@Pointcut("within(com.abc.xyz..*)")
    //	@Pointcut("execution(public * *(..))")
    	public void allMethod() {
    	}
    
    	@Before("allMethod()")
    	public void before(JoinPoint jp) {
    		Logger.logMethodStart(jp.getTarget(), jp.getSignature().getName());
    	}
    
    	@AfterReturning("allMethod()")
    	public void afterReturning(JoinPoint jp) {
    		Logger.logMethodEnd(jp.getTarget(), jp.getSignature().getName());
    	}
    
    }

    i had tried all the point cuts shown

    I had a set fo packages with in com.abc.xyz
    my intention for the point cut was to call logger for all the public methods with in the sub packages of com.abc.xyz

    for example it should be called for all the classes methods with public acess modifier for teh below packages

    com.abc.xyz.pqr
    com.abc.xyz.mno
    etc


    Could any body please correct me if i had anything wrong in my point cut for making my wish to work ?

    Many Thanks in advance.

  2. #2
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Since "execution(public * *(..))" didn't work, I think your configuration is incorrect. What does you configuration file look like?

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  3. #3

    Default

    I had that class as my aspect and nothing other than this in my config xml

    <aop:aspectj-autoproxy />

    offcourse i had my aspect bean declared.

    If i use public i was getting all the org.spring.* as well but not all the packages in my path which i expected

    i just want only for the public methods in my packages.

    To my strange it was showing only for few packages but not for all

    Please help

    Many Thanks in advance

  4. #4
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    With Spring AOP, aspects advise only Spring beans. It looks like if that is all you have in your configuration, you don't have any beans defined. Please refer to Spring documentation for more details on how Spring AOP works.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  5. #5

    Default

    i had used all the packages contents with beans.

    i.e i had configured all the beans in my web.xml like this
    Code:
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/entity-objects-context.xml
    			/WEB-INF/builder-objects-context.xml
    			/WEB-INF/dao-objects-context.xml
    			/WEB-INF/delegator-objects-context.xml
    			/WEB-INF/validator-objects-context.xml
    		</param-value>
    	</context-param>
    i had even introduced that above mentioned aop tag in these xmls as well.

  6. #6
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Do you have a bean corresponding to the aspect?

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  7. #7

    Default

    yah had this


    <bean class="packagename.LogInterceptor" />

    please help

  8. #8

    Question Webflow PointCut

    I'm in a similar situation..

    I'm trying @AspectJ with java 5 annot. and
    just trying to get it working.

    I've added the configs:
    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"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    
        <aop:aspectj-autoproxy>
           <aop:include name="errorLogger" />
        </aop:aspectj-autoproxy>
    
        <bean id="errorLogger" class="org.springframework.webflow.samples.util.ErrorLogger" />
    </beans>
    ErrorLogger has the following
    Code:
    @Aspect
    public class ErrorLogger implements Serializable {
    
        @Pointcut("execution(* org.springframework.webflow.samples.util.SearchCriteria.*(..))")
        public void say(){}
    
        @Before("say()")
        public void sayHi(){
            System.out.println("sayHi()");
        }
    }
    org.springframework.webflow.samples.util.SearchCri teria has method
    Code:
    public void throwExcept(){
            System.out.println("throwExcept()");
            try{
                //int error = 2/0;
                System.out.println("Annie are u OK?");
            }
            catch(Exception e){
               System.out.println("throwExcept()"+e.getMessage());
               e.printStackTrace();
              //throw new Exception(e);
            }
         }
    to trigger the aspect in the flow...
    Code:
    <var name="searchCriteria" class="org.springframework.webflow.samples.util.SearchCriteria" />
    
            <on-start>
                <evaluate expression="searchCriteria.throwExcept()" />
            </on-start>

    when this runs I get
    Code:
    2009-11-28 17:14:38,031 DEBUG [org.springframework.webflow.execution.AnnotatedAction] - <Putting action execution attributes map[[empty]]>
    throwExcept()
    Annie are u OK?
    so the aspect doesn't get triggered.

    However if I change the pointcut to..
    Code:
    @Before("execution(public * *(..))")
    the aspect does get triggered and I get "sayHi" everywhere
    eventually crashing the API.

    So what gives? why doesn't
    Code:
    @Pointcut("execution(* org.springframework.webflow.samples.util.SearchCriteria.*(..))")
    This work? I've tried many variations but no way of selecting my method ( as a test run).

    Is Webflow complicating the way Spring AOP normally works?

  9. #9

    Default

    Where is the SearchCriteria Bean defined in configs ?

    I had solved my problem like this ---


    Key -- > AOP will only understands the Aspects defined within the same context.


    I have many classes formed as layers and i defined each layer in separate xml and had included them in my web.xml. So the AOP container was unable to identify the beans and so the points cuts have not executed.

    I had imported all my config xmls in my main MVC beans config xml (name_that_you_gave_in_Web.xml_For_Spring_Dispatch er_Servlet-servlet.xml)using import tag and everything worked.

    From the configuration you wrote i see the same problem please include your class which contains the method for which you are anticipating the Point cut to execute (SearchCriteria class) to the config xml where you are defing aop proxy.


    you need to import the other beans to your config like this

    Code:
    <import resource="/config/builder-objects-context.xml" />
    and your AOP configuration should be

    Code:
    <aop:aspectj-autoproxy proxy-target-class="true" />
    proxy target class should be set to true if you are not implementing the business interface.


    Hope this helps you..

  10. #10

    Thumbs up

    Hi shivaji,

    Thanks for your help!

    This seems to make sense but how could I:

    "import the other beans to your config like this..
    Code:
    <import resource="/config/builder-objects-context.xml" />
    if I understand right I need a reference to the ${build.classes.dir} or some
    builder-objects-context.xml that represents this?

    With webflow isn't everything autowired up?

    My main web-application-config.xml imports the layer configs
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<!-- Scans for application @Components to deploy -->
            <context:component-scan base-package="org.springframework.webflow.samples" />
    	<!-- Imports the configurations of the different infrastructure systems of the application -->
    	<import resource="webmvc-config.xml" />
    	<import resource="webflow-config.xml" />
    	<import resource="data-access-config.xml" />
    	<import resource="security-config.xml" />
    	<import resource="i18n-config.xml" />
            <import resource="aspect-config.xml" />
    </beans>
    So their all tied together and I thought the classes were taken care of,.. if not how could I do
    Code:
    <import resource="/config/builder-objects-context.xml" />
    what do I look for?

    Best regards,

    John.

Tags for this Thread

Posting Permissions

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