PDA

View Full Version : AOP does not work :(



Savagearts
Sep 6th, 2004, 09:34 AM
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:

<?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.Defaul tAdvisorAutoProxyCreator">
</bean>

<bean id="beforeAdvice" class="com.savagearts.study.aop.spring.HelloWorldBeforeAd vise"/>

<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.RegexpMethodPointc ut">
<property name="patterns">
<list>
<value>.*get*</value>
</list>
</property>
</bean>

<bean id="advisor"
class="org.springframework.aop.support.DefaultPointcutAdv isor"
singleton="false">
<property name="pointcut">
<ref local="pointcut"/>
</property>
<property name="advice">
<ref local="beforeAdvice"/>
</property>
</bean>

</beans>




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;.get ResourceAsStream&#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;



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]

Savagearts
Sep 7th, 2004, 12:36 AM
Can anybody give me some suggestion?

Rod Johnson
Sep 7th, 2004, 04:48 AM
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.

Savagearts
Sep 7th, 2004, 08:52 AM
Thanks, Mr Johnson. I've got your advice and i'll try it.

robh
Sep 8th, 2004, 10:37 AM
There is an error in your regular expression - you need to use .*get.* not .*get* - notice the additional . before the last *.

Rob

Savagearts
Sep 10th, 2004, 11:14 AM
I've fixed this problem. Anyway,Thanks .It's so kind of you spring guys.

dima767
Sep 17th, 2004, 10:08 AM
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.

julius_ferns
Feb 19th, 2005, 02:06 AM
>>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?

robh
Feb 19th, 2005, 04:32 AM
See my earlier post about the invalid regular expression pattern - that's the fix.

Rob

julius_ferns
Feb 19th, 2005, 05:36 AM
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.ProxyFactoryBean">
<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.RegexpMethodPointc utAdvisor">
<property name="advice">
<ref local="theAroundAdvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean> -->

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

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

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