添加類庫:aspectjrt.jar和aspectjweaver.jar
添加aop schema.
定義xml元素:<aop:aspectj- autoproxy>
編寫java類,並用@Aspect注解成通知
AspectJ 支持 5 種類型的通知注解:
@Before: 前置通知, 在 方法執行之前執行
@After: 後置通知, 在方法執行之後執行
@AfterReturning: 返回通知, 在方法返回結果之後執行
@AfterThrowing: 異常通知, 在方法拋出異常之後
@Around: 環繞通知, 圍繞著方法執行
配置成普通bean元素即可 .
二、
後置通知:@After
@After("execution(* *..WelcomeService.*(..))")
public void applaud(){..}
後置通知在目標方法執行完成之後執行.一個切面aspect包含很多通知.
@After
後置通知表明目標方 法執行完之後,不論是否拋異常,都會織入該通知.
@AfterReturning
方法返回後通知只在目標方法返回以後執行,若拋 異常不執行.
@AfterReturning(pointcut="",returning="res")
public void xxx(Joinput jp,Object res)
在AfterReturning
通知中可接收到返回值.res即是用來接收返回值的對象.
三、
環繞 通知:@Around
@Around("execution(* *..WelcomeService.*(..))")
public void around (ProceedingPointCut jp){..}
注意:可以控制目標方法是否調用,以及返回完全不同的對象,要慎用.
指定優先 級:
@Aspect
@Order(0)
public class xxx{...}
加上@Order注解可以指定加入切面的優先級(先後順序,值越小, 優先級越高)
例子說明:
AdviceImpl類:
package www.csdn.spring.advice.aspetjs;
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;
@Aspect
public class AdviceImpl {
@Before(value = "execution(* UserDaoImpl.*(..))")
public void doTransAction(){
System.out.println("----開啟事務-----");
}
@After(value = "execution(* www.csdn..UserDaoImpl.*(..))")
public void doAfterTransAction(){
System.out.println("-------提交事務-------");
}
@Around(value = "execution(* www.csdn..UserDaoImpl.save(..))")
public void doSec(ProceedingJoinPoint jp){
System.out.println("----------安全處理之前-------------");
try {
Object obj = jp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------安全處理之後-------------");
}
}
package www.csdn.spring.advice.aspetjs;
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;
@Aspect
public class AdviceImpl {
@Before(value = "execution(* UserDaoImpl.*(..))")
public void doTransAction(){
System.out.println("----開啟事務-----");
}
@After(value = "execution(* www.csdn..UserDaoImpl.*(..))")
public void doAfterTransAction(){
System.out.println("-------提交事務-------");
}
@Around(value = "execution(* www.csdn..UserDaoImpl.save(..))")
public void doSec(ProceedingJoinPoint jp){
System.out.println("----------安全處理之前-------------");
try {
Object obj = jp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------安全處理之後-------------");
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 通知 -->
<bean id="adviceImpl" class="www.csdn.spring.advice.aspetjs.AdviceImpl" />
<!-- 配置bean -->
<bean id="userDaoImpl" class="www.csdn.spring.advice.aspetjs.UserDaoImpl" />
<aop:aspectj-autoproxy />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 通知 -->
<bean id="adviceImpl" class="www.csdn.spring.advice.aspetjs.AdviceImpl" />
<!-- 配置bean -->
<bean id="userDaoImpl" class="www.csdn.spring.advice.aspetjs.UserDaoImpl" />
<aop:aspectj-autoproxy />
</beans>
測試類:
package www.csdn.spring.advice.aspetjs;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspetjsTest {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring-asp*.xml");
UserDao uDao=(UserDao) context.getBean("userDaoImpl");
uDao.save(null);
/*uDao.delete(null);
try{
uDao.update(null);
}catch(Exception e){
}
uDao.getObjects();*/
}
}