Announcement Announcement Module
Collapse
No announcement yet.
AOP does not work :( Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP does not work :(

    Hi guys:
    I'm new to AOP and i try it with spring's AOP framework. But it does not work. Here are some code following:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    
        <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
        </bean>
    
        <bean id="beforeAdvice" class="com.savagearts.study.aop.spring.HelloWorldBeforeAdvise"/>
    
        <bean id="helloworld" class="com.savagearts.study.aop.spring.HelloWorld">
            <property name="greeting">
                <value>hello world</value>
            </property>
        </bean>
    
        <bean id="pointcut"
            class="org.springframework.aop.support.RegexpMethodPointcut">
            <property name="patterns">
                <list>
                    <value>.*get*</value>
                </list>
            </property>
        </bean>
    
        <bean id="advisor"
            class="org.springframework.aop.support.DefaultPointcutAdvisor"
            singleton="false">
            <property name="pointcut">
                <ref local="pointcut"/>
            </property>
            <property name="advice">
                <ref local="beforeAdvice"/>
            </property>
        </bean>
    
    </beans>

    Code:
    public class HelloWorld &#123;
        private String greeting;
    
        public String getGreeting&#40;&#41;&#123;
            return this.greeting;
        &#125;
    
        public void setGreeting&#40;String greeting&#41; &#123;
            this.greeting = greeting;
        &#125;
    
        public static void main&#40;String&#91;&#93; args&#41;throws Exception&#123;
            InputStream in = Thread.currentThread&#40;&#41;.getContextClassLoader&#40;&#41;.getResourceAsStream&#40;"applicationContext.xml"&#41;;
            ApplicationContext factory = new ClassPathXmlApplicationContext&#40;"applicationContext.xml"&#41;;
            HelloWorld helloWorld=&#40;HelloWorld&#41;factory.getBean&#40;"helloworld"&#41;;
            System.out.print&#40;helloWorld.getGreeting&#40;&#41;&#41;;
        &#125;
    &#125;

    Code:
    public class HelloWorldBeforeAdvise implements MethodBeforeAdvice&#123;
        private int count;
    
        public void before&#40;Method method, Object&#91;&#93; objects, Object o&#41; throws Throwable &#123;
            count++;
            System.out.print&#40;"executing method "+count +" times"&#41;;
        &#125;
    &#125;
    [/code]

  • #2
    Can anybody give me some suggestion?

    Comment


    • #3
      This config and code looks OK. When you say it doesn't work, I assume that you don't get your advice firing, rather than that there's any error?

      So we'll have to try to eliminate possible causes. Please start by making your advice match everything by commenting out the Pointcut reference in the "advisor" bean definition, using the DefaultPointcutAdvisor default (Pointcut.TRUE), to see whether the pointcut is the problem.

      Comment


      • #4
        Thanks, Mr Johnson. I've got your advice and i'll try it.

        Comment


        • #5
          There is an error in your regular expression - you need to use .*get.* not .*get* - notice the additional . before the last *.

          Rob

          Comment


          • #6
            I've fixed this problem. Anyway,Thanks .It's so kind of you spring guys.

            Comment


            • #7
              BTW,

              the org.springframework.aop.support.RegexpMethodPointc ut is deprecated, so I would suggest you switch to JdkRegexpMethodPointcut if you have JDK1.4 or higer or Perl5RegexpMethodPointcut if you have pre-1.4 JDK. Note that Perl5RegexpMethodPointcut would require a dependency on Jakarta ORO regular expression library.

              Comment


              • #8
                >>I've fixed this problem. Anyway,Thanks .It's so kind of you spring guys.

                can u tell us how you got it fixed?What was the change you did?

                Comment


                • #9
                  See my earlier post about the invalid regular expression pattern - that's the fix.

                  Rob

                  Comment


                  • #10
                    Why this does not work ??Advice not getting applied

                    for dynamic pointcut but works for static pointcuts



                    <?xml version="1.0" encoding="UTF-8"?>
                    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                    <beans >

                    <!--CONFIG-->
                    <bean id="businesslogicbean" class="org.springframework.aop.framework.ProxyFact oryBean">
                    <property name="proxyInterfaces">
                    <value>IBusinessLogic</value>
                    </property>

                    <property name="target">
                    <ref local="beanTarget"/>
                    </property>
                    <property name="interceptorNames">
                    <list>
                    <value>theAroundAdvisor</value>
                    </list>
                    </property>
                    </bean>

                    <!--CLASS-->
                    <bean id="beanTarget" class="BusinessLogic"/>

                    <!--ADVISOR-->
                    <!--Note: An advisor assembles pointcuts and advice-->
                    <!-- <bean id="theAroundAdvisor" class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
                    <property name="advice">
                    <ref local="theAroundAdvice"/>
                    </property>
                    <property name="pattern">
                    <value>.*</value>
                    </property>
                    </bean> -->

                    <bean id="mypointcut" class="org.springframework.aop.support.ControlFlow Pointcut">
                    <constructor-arg index="0" >
                    <value>AroundAdvice</value>
                    </constructor-arg>
                    </bean>

                    <bean id="theAroundAdvisor" class="org.springframework.aop.support.DefaultPoin tcutAdvisor">
                    <property name="pointcut">
                    <ref local="mypointcut"/>
                    </property>
                    <property name="advice">
                    <ref local="theAroundAdvice"/>
                    </property>
                    </bean>

                    <bean id="theAroundAdvice" class="AroundAdvice" />
                    </beans>

                    Comment

                    Working...
                    X