jQuery in Action, 2nd edition*
The moose likes Spring and the fly likes  want an advisor for many pointcut Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark " want an advisor for many pointcut" Watch " want an advisor for many pointcut" New topic
Author

want an advisor for many pointcut

Kurt Xu
Ranch Hand

Joined: Sep 29, 2009
Posts: 32
Hi all:
In my new project, we need to program many existing projects to intercept some action method in order to add some extra work, mainly synchronize the modified data into another db. We just need an advice but tens of pointcut, maybe over 100, from different existing projects. I want to configure the action and method info in a xml file, and then apply them as pointcut through some way. The configuration info is like below, just temporary thought.

My question is that besides a customized advisor extending StaticMethodMatcherPointcutAdvisor and implementing the method match throung the way of , such as storing the action and method names in a List and determining by List.contains(), is there an other easy and efficient way ? Such as @aspectj and aspectj schema. At least I haven't found a good way, cause it's horrible to write a very long pointcut expression in the applicationContext.xml, there're too many methods and actions to write, and it's hard to read and maintain all these pointcut expression. It's better not to import other jar file beside spring library.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Here is your best solution.

1) Create your own Annotations and then annotate all the methods you want advised.
then write your pointcut expression to match the annotation.

for instance

"execution(@com.myapp.annotations.MyAnnotation * *(..))"

Now any method that is annotated with @MyAnnotation will match that pointcut expression. No more need of 100 pointcut expressions.

Here is my definition of that annotation




That's it.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

You can also use <aop:pointcut/> tags in spring xml, but i'll advice to use them only if you can define some pattern over the join points.
say com.abc.*.Db*.u*(..) then it will work well.


SCJP 91%, SCWCD 97%
Kurt Xu
Ranch Hand

Joined: Sep 29, 2009
Posts: 32
Mark Spritzler wrote:Here is your best solution.

1) Create your own Annotations and then annotate all the methods you want advised.
then write your pointcut expression to match the annotation.

for instance

"execution(@com.myapp.annotations.MyAnnotation * *(..))"

Now any method that is annotated with @MyAnnotation will match that pointcut expression. No more need of 100 pointcut expressions.

Here is my definition of that annotation




That's it.

Mark

It works. If it doesn't need cglib and asm jar file it would be perfect. Our company' system is a little rigid, which means we can't easily add jar file to the existing web project. I'm very curious with that how webwork implements interceptor without cglib and asm. After all interceptor is same of aop.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

If your classes have interfaces and implement them. Them Spring will create DynamicProxies. No need for cglib in that scenario.

Mark
Kurt Xu
Ranch Hand

Joined: Sep 29, 2009
Posts: 32
Mark Spritzler wrote:If your classes have interfaces and implement them. Them Spring will create DynamicProxies. No need for cglib in that scenario.

Mark


Yes, I know that , unfortunately, some BO classes have interface, some don't. Looks like all I can do is to persuade my boss to add jar file.
 
 
subject: want an advisor for many pointcut
 
Similar Threads
Any good examples of using @Before instead of setup
Can spring make a class implement an interface?
Hibernate error with Spring MVC
Is there another option for Spring AOP Annotations?
AspectJ and AOP in general