Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want an advisor for many pointcut

 
Kurt Xu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shashank Ag
Ranch Hand
Posts: 88
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kurt Xu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic