Announcement Announcement Module
Collapse
No announcement yet.
AOP not working as per point cut Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Since "execution(public * *(..))" didn't work, I think your configuration is incorrect. What does you configuration file look like?

    -Ramnivas

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            Do you have a bean corresponding to the aspect?

            -Ramnivas

            Comment


            • #7
              yah had this


              <bean class="packagename.LogInterceptor" />

              please help

              Comment


              • #8
                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?

                Comment


                • #9
                  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..

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      if the beans are being imported correctly into your aspect configured xml then it should work

                      Comment


                      • #12
                        Webflow Aspectj

                        Hi shivaji,

                        in attempting to use Spring AOP proxy if include both classes
                        as a test..
                        Code:
                        <aop:aspectj-autoproxy>
                               <aop:include name="errorLogger" />
                               <aop:include name="searchCriteria" />
                        </aop:aspectj-autoproxy>
                        ie: both Aspect and target are recognized by the proxy somewhat, it only gets triggered upon rendering the view or someting like that, anyway you wouldn't want to have to hardcode all the includes like that?

                        I am to understand that you must use Load-Time Weaving for Webflow (lucky me!) and there's not much out there concerning this.

                        I found this link for configs but can't see/checkout the source code
                        http://www.springbyexample.org/examp...pectj-ltw.html

                        When you do that you get all kinds of serialization errors, Webflow requires
                        classes to implement serialization.

                        Pretty much unable to use AOP in Weflow (hope I'm wrong)!

                        John.

                        Comment

                        Working...
                        X