來源:http://www.tuicool.com/articles/QbaQru
SPRing默認(rèn)aspectJ切入點(diǎn)語法
引入:將方法或字段添加到被處理的類中。 目標(biāo)對象: 包含連接點(diǎn)的對象。也被稱作 被通知或被代理對象。 AOP代理:AOP框架創(chuàng)建的對象,對目標(biāo)對象的加強(qiáng)。 織入:將增強(qiáng)處理添加到目標(biāo)對象中,并創(chuàng)建一個被增強(qiáng)的對象的過程。
掃描加的注釋和啟動@Aspect所需要的jar包:
為了在Spring配置中使用@AspectJ切面,首先必須啟用Spring對@AspectJ切面配置的支持,并確保 自動代理(autoproxying) 的bean是否能被這些切面通知。自動代理是指Spring會判斷一個bean是否使用了一個或多個切面通知,并據(jù)此自動生成相應(yīng)的代理以攔截其方法調(diào)用,并且確保通知在需要時執(zhí)行。
通過在Spring的配置中引入下列元素來啟用Spring對@AspectJ的支持:
<aop:aspectj-autoproxy/>配置文件
xmlns:context="http://www.springframework.org/schema/context"http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd配置aop增加驗(yàn)證驗(yàn)證文檔xmlns:aop="http://www.springframework.org/schema/aop"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop /spring-aop-4.0.xsdUserDao.javapackage net.csdn.www.dao;import org.springframework.stereotype.Component;@Componentpublic class UserDao { public void save(){ System.out.println("用戶保存成功!"); }}applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" > <!-- 語言寫入值xmlns:p --> <!-- xmlns:context掃描加的注釋 --> <context:component-scan base-package="net.csdn.www.dao,net.csdn.www.aop"> <!-- 啟動@Aspect支持 --> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan> <aop:aspectj-autoproxy/></beans>使用execution
切入點(diǎn)指示符execution(public * *(..)) 所有的公共方法 execution(* set*(..)) 以set開頭的任意方法 execution(* com.xyz.service.AccountService.*(..)) com.xyz.service.AccountService類中的所有的方法 execution(* com.xyz.service.*.*(..)) com.xyz.service包中的所有的類的所有的方法 execution(* com.xyz.service..*.*(..)) com.xyz.service包及子包中所有的類的所有的方法 execution(* cn.itcast.spring.sh..*.*(String,?,Integer)) cn.itcast.spring.sh包及子包中所有的類的有三個參數(shù) 第一個參數(shù)為String,第二個參數(shù)為任意類型, 第三個參數(shù)為Integer類型的方法
定義成一個切面 LogAspect.javapackage net.csdn.www.aop;import java.util.logging.Level;import java.util.logging.Logger;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;//定義成一個切面@Aspectpublic class LogAspect { //定義一個切入點(diǎn) ,訪問修飾符,包名,類名,方法名(參數(shù),異常) @Before("execution(* net.csdn.www.dao.*.save*(..))") public void saveLog() { Logger log = Logger.getLogger(LogAspect.class.getName()); log.log(Level.INFO, "信息被保存"); } }測試代碼:ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao=(UserDao) context.getBean("userDao"); userDao.save();運(yùn)行結(jié)果: 信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2014-3-27 21:35:00 net.csdn.www.aop.LogAspect saveLog 信息: 信息被保存 用戶保存成功!在UserDao.java中聲明一個方法:
public void delete(){ int i= 5/0; }@AfterThrowing(throwing="rvt",pointcut="execution(* net.csdn.www.dao.*.delete*(..))") public void throwLog(Throwable rvt){ System.out.println("獲取目標(biāo)方法拋出的異常"+rvt); System.out.println("記錄日志"); }調(diào)用delete()方法后的運(yùn)行結(jié)果:Exception in thread "main" java.lang.ArithmeticException: / by zero at net.csdn.www.dao.UserDao.delete(UserDao.java:15) at net.csdn.www.dao.UserDao$$FastClassBySpringCGLIB$$c34aca31.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)獲取目標(biāo)方法拋出的異常java.lang.ArithmeticException: / by zero 記錄日志 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) at net.csdn.www.dao.UserDao$$EnhancerBySpringCGLIB$$d3fba046.delete(<generated>) at net.csdn.www.test.Test.main(Test.java:14)
添加UserDao.java帶返回值的方法
public String selectUser(String name){ System.out.println("用戶信息查詢成功"); return "success"; }//定義一個Around的切入點(diǎn) @Around("execution(* net.csdn.www.dao.*.select*(..))") public Object selectLog(ProceedingJoinPoint pj) throws Throwable{ Logger log = Logger.getLogger(LogAspect.class.getName()); log.log(Level.INFO, "信息被查詢"); //System.out.println(pj.getArgs()+"--"); Object result= pj.proceed(new String[]{"peitihuande zhi"}); return "peitihuande zhi"; }運(yùn)行結(jié)果:信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2014-3-27 22:28:00 net.csdn.www.aop.LogAspect selectLog 信息: 信息被查詢 用戶信息查詢成功 peitihuande zhi
新聞熱點(diǎn)
疑難解答