国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

spring的aop實現(xiàn)

2019-11-08 03:09:56
字體:
來源:轉載
供稿:網(wǎng)友

來源:http://www.tuicool.com/articles/QbaQru

SPRing默認aspectJ切入點語法

引入:將方法或字段添加到被處理的類中。 目標對象: 包含連接點的對象。也被稱作 被通知或被代理對象。  AOP代理:AOP框架創(chuàng)建的對象,對目標對象的加強。 織入:將增強處理添加到目標對象中,并創(chuàng)建一個被增強的對象的過程。 

掃描加的注釋和啟動@Aspect所需要的jar包: 

為了在Spring配置中使用@AspectJ切面,首先必須啟用Spring對@AspectJ切面配置的支持,并確保 自動代理(autoproxying) 的bean是否能被這些切面通知。自動代理是指Spring會判斷一個bean是否使用了一個或多個切面通知,并據(jù)此自動生成相應的代理以攔截其方法調用,并且確保通知在需要時執(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

增加驗證驗證文檔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.java
package 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 切入點指示符 

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.java 
package 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 {	//定義一個切入點    ,訪問修飾符,包名,類名,方法名(參數(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();運行結果: 信息: 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("獲取目標方法拋出的異常"+rvt);	      System.out.println("記錄日志");	}調用delete()方法后的運行結果:

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)獲取目標方法拋出的異常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的切入點	@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";	}運行結果:

信息: 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 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 晋州市| 襄城县| 平定县| 承德市| 宜兰县| 乐都县| 通江县| 钦州市| 泰顺县| 博爱县| 社会| 蓝田县| 台东市| 香河县| 金阳县| 潼南县| 嵊泗县| 合作市| 罗江县| 通化市| 鄂温| 崇文区| 遵义市| 鹤庆县| 邮箱| 额济纳旗| 武陟县| 灵璧县| 永泰县| 鹤山市| 丹巴县| 兰溪市| 东乡| 榆中县| 临高县| 兴化市| 江门市| 麻城市| 桑植县| 平昌县| 潼关县|