2012年9月6日 星期四

[記事] Spring AOP

屬於阿宅世界的技術文章,想看的再點開,切莫自誤 !


呃...吃太多, 有點消化不良, 先吐一點出來, 晚點在反芻... orz

1. Create a interface
package com.cht.domain;

public interface IWorker {

  public void work(String thing);
  public void sayHello(String who);

}


2. implement it
package com.cht.domain;

public class Employee implements IWorker{
  @Override
  public void work(String thing) {
    System.out.println("\tDo " + thing);
  }

  @Override
  public void sayHello(String who) {
    System.out.println("\tHi, " + who);
  }
}


3. write the aspect config bean
package com.cht.domain;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectConfigBean {

  @Pointcut("execution(* com.cht.domain.Employee.*(..))")
  public void doIt() {
    System.out.println("wwwwwwwwwhat!");
  }

  @Before("doIt()")
  public void beforeExec(){
    System.out.println("Before All function call...");
  }

  @Before("execution(* com.cht.domain.Employee.sayHello(..))")
  public void beforeAgain(JoinPoint point){
    System.out.println("Before Employee.sayHello");
  }

  @After("doIt()")
  public void afterAll(JoinPoint point) {
    System.out.println("All after "+ point.getSignature().getName());
  }

  @After("execution(* com.cht.domain.Employee.work(..))")
  public void afterExecWork(JoinPoint point){
    System.out.println("after "+ point.getSignature().getName());
  }

  @After("execution(* com.cht.domain.Employee.sayHello(..))")
  public void afterExecSayHello(JoinPoint point ){
    System.out.println("after "+ point.getSignature().getName());
  }

  @Around("execution(* com.cht.domain.Employee.*(..))")
  public void aroundAdvice(ProceedingJoinPoint point){
    System.out.println(point.getSignature().getName() + " proceeding...");
    try {
      point.proceed();
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }
}


4. define them in the application context xml

<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<aop:aspectj -autoproxy="-autoproxy" />

<bean class="com.cht.domain.Employee" id="employee" />
<bean class="com.cht.domain.AspectConfigBean" id="aspectBean" />
</beans>

5. test
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cht.domain.IWorker;

public class TESTAOP {

  public static void main(String[] args) {
    ApplicationContext context =
      new ClassPathXmlApplicationContext("spring-aop.xml");

    IWorker worker = (IWorker) context.getBean("employee");

    worker.sayHello("Samuel");

    worker.work("programming");
  }
}

17 則留言:

  1. It is truly a nice and useful piece of information.

    I am happy that you just shared this useful info with us.

    Please keep us up to date like this. Thanks for sharing.
    My page : lan.helsingkrona.Se

    回覆刪除
  2. For latest news you have to pay a visit internet and on world-wide-web I found this website as a finest website for newest updates.
    Also visit my web-site : Fast Loan 1080

    回覆刪除
  3. magnificent points altogether, you simply gained a new reader.
    What could you recommend in regards to your submit that you made some days in the
    past? Any sure?
    My website : http://phpfoxdev.indexcreativeonline.com/

    回覆刪除
  4. Thanks a bunch for sharing this with all of us you actually know what you are speaking about!
    Bookmarked. Please also seek advice from my website =).
    We can have a hyperlink change contract between us
    Also visit my page http://Wiki.Space-Cam.ch/

    回覆刪除
  5. Your style is so unique compared to other people I've read stuff from. Many thanks for posting when you have the opportunity, Guess I'll just book mark this blog.
    Look at my weblog :: mikelim.info

    回覆刪除
  6. Heya i'm for the first time here. I found this board and I find It really helpful & it helped me out much. I am hoping to give one thing again and aid others such as you aided me.
    Here is my weblog ; businessnews.lu

    回覆刪除
  7. I was curious if you ever thought of changing the layout of your blog?
    Its very well written; I love what youve got to say. But maybe
    you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having one
    or two images. Maybe you could space it out better?
    My web page > Quick Payday Loans

    回覆刪除
  8. Thank you for the good writeup. It in fact was a amusement account it.

    Look advanced to more added agreeable from you!

    However, how can we communicate?
    Here is my website Blog.Phirmaza.Com

    回覆刪除
  9. What's Taking place i am new to this, I stumbled upon this I have discovered It absolutely helpful and it has aided me out loads. I am hoping to give a contribution & aid other users like its helped me. Great job.
    My site > Quick Loan

    回覆刪除
  10. I'm not sure exactly why but this blog is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I'll check back later on and see if the problem
    still exists.
    Look at my blog post :: Flippedeye.net

    回覆刪除
  11. Greetings! Very useful advice within this article! It's the little changes which will make the most important changes. Thanks a lot for sharing!
    Feel free to surf my web-site - fast cash loan 444

    回覆刪除
  12. I don't even know how I stopped up here, however I believed this publish was once great. I don't know who you're but definitely you're going to a famous blogger when you
    are not already. Cheers!
    My web blog http://Mymotorcyclemonster.com/index.php?do=/sondravrcq/info

    回覆刪除
  13. I do not even know how I stopped up here, however I thought this submit was once
    good. I don't recognise who you might be but definitely you are going to a well-known blogger if you happen to are not already. Cheers!
    Here is my weblog ... Http://andenipartners.Com/node/26417

    回覆刪除
  14. Hi there great blog! Does running a blog
    like this require a great deal of work? I've virtually no understanding of computer programming but I was hoping to start my own blog soon. Anyhow, if you have any ideas or techniques for new blog owners please share. I understand this is off subject but I just needed to ask. Many thanks!
    Check out my blog - quick loan

    回覆刪除
  15. Link exchange is nothing else however it is simply placing the other
    person's blog link on your page at suitable place and other person will also do same in support of you.
    Feel free to surf my web page psadirectory.cdconline.com

    回覆刪除
  16. I was curiοuѕ if yоu eνer considered changing the layout of your ωebsite?

    Its veгу wеll ωritten; I love
    what youve got to say. But maybе you cοuld а little morе
    in the wаy of content so people could connect with it better.

    Youve got an awful lot of text for only having one oг two images.
    Maybe you could space it out better?
    My page: prosperloans.webstarts.com

    回覆刪除
  17. Good post! We are linking to this great content on our site.
    Keep up the great writing.

    Here is my page :: buy targeted facebook likes australia

    回覆刪除