Spring AOP実現——annotation方法
SpringのAOPに導入する特殊パッケージ(もちろん他のspringのベースパッケージは必須)
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
1.Hello.JAvaインタフェース
2.HelloImpl.java
3.HelloProxy.JAva(いわゆるAspectクラス)
4.ApplicationContext.xml
5.テスト
6.結果
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
1.Hello.JAvaインタフェース
package com.xj.bean.aop;
public interface Hello {
public void addHello(); //
public void deleteHello(); //
public void findHello();
}
2.HelloImpl.java
package com.xj.bean.aop;
public class HelloImpl implements Hello{
@Override
public void addHello() {
System.out.println("add");
}
@Override
public void deleteHello() {
System.out.println("delete");
}
@Override
public void findHello() {
System.out.println("find");
}
}
3.HelloProxy.JAva(いわゆるAspectクラス)
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class HelloProxy {
// (@Before) ( Advice )
//execution
@Before("execution( * com.xj.bean.aop.*.add*(..))")
private void beforeLog(){
System.out.println("before check add");
}
// (@After) ( Advice )
@After("execution(* com.xj.bean.aop.*.delete*(..))")
private void afterLog(){
System.out.println("after check delete");
}
}
4.ApplicationContext.xml
5.テスト
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello)context.getBean("hello");
hello.addHello();
hello.deleteHello();
6.結果
before check add //add
add
delete
after check delete //delete