当前位置: 首页 >Java技术 > springAOP基于XML配置文件方式

springAOP基于XML配置文件方式

<aop:config.../>包含:poincut,advisor,aspect元素,通过XML配置。

springAOP的具体加载步骤:
   1、当spring容器启动的时候,加载了spring的配置文件
   2、为配置文件中所有的bean创建对象
   3、spring容器会解析aop:config的配置 
       1、解析切入点表达式,用切入点表达式和纳入spring容器中的bean做匹配
            如果匹配成功,则会为该bean创建代理对象,代理对象的方法=目标方法+通知
            如果匹配不成功,不会创建代理对象
   4、在客户端利用context.getBean获取对象时,如果该对象有代理对象则返回代理对象,如果代理对象,则返回目标对象


说明:如果目标类没有实现接口,则spring容器会采用cglib的方式产生代理对象,如果实现了接口,会采用jdk的方式


MyAspect.java

package net.csdn.www.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterRetuing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;public class MyAspect {		public void authority(JoinPoint jp) {		System.out.println("被代理方法名字:"+jp.getSignature().getName());		System.out.println("被代理方法参数:"+jp.getArgs());		System.out.println("被代理对象:"+jp.getTarget());		System.out.println("现在调用的是权限验证");  	}		public  void log(JoinPoint jp,Object rvt){		System.out.println("被代理方法名字:"+jp.getSignature().getName());		System.out.println("被代理方法参数:"+jp.getArgs());		System.out.println("被代理对象:"+jp.getTarget());		System.out.println("被代理对象的返回值"+rvt);		System.out.println("现在调用的是日志管理");	}		public Object processTx(ProceedingJoinPoint pjp) throws Throwable{		System.out.println("现在调用的是事务开启");				//得到业务方法的参数		Object[] args=pjp.getArgs();		System.out.println("业务方法的参数:"+args);		//被代理对象的业务方法		Object result=pjp.proceed(args);		System.out.println("现在调用的是事务提交或回滚");	retu result;	}			public void release(){		System.out.println("资源已经被释放!");			}}

UserDao.java

package net.csdn.www.dao;import java.util.Date;import org.springframework.stereotype.Component;@Componentpublic class UserDao {public String save(String name){	System.out.println(name+"被保存");	retu "success";}}

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">	<!-- 切面 -->	<bean id="myAspect" class="net.csdn.www.aop.MyAspect"></bean>	<bean id="userDao" class="net.csdn.www.dao.UserDao"></bean>	<!-- 切入点 -->	<aop:config>		<aop:aspect id="asp1" ref="myAspect">			<aop:before method="authority" pointcut="execution(* net.csdn.www.dao.*.*(..))" />			<aop:after method="release" pointcut="execution(* net.csdn.www.dao.*.*(..))" />			<aop:after-retuing method="log"				pointcut="execution(* net.csdn.www.dao.*.*(..))" retuing="rvt" />			<aop:around method="processTx" pointcut="execution(* net.csdn.www.dao.*.*(..))" />		</aop:aspect>	</aop:config></beans>

测试类Test.java

package net.csdn.www.text;import java.util.Date;import net.csdn.www.dao.UserDao;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");	UserDao  userDao=(UserDao) context.getBean("userDao"); userDao.save("csdnccccccc");}}

springAOP基于XML配置文件方式 _ JavaClub全栈架构师技术笔记


在UserDao类中声明一个带参数的方法

public String eat(Date date,String food){	System.out.println(date+"先准备:"+food);	retu "好吃";}

MyAspect1.java

package net.csdn.www.aop;import java.util.Date;import org.aspectj.lang.annotation.AfterRetuing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;public class MyAspect1 {		//注意:增加参数匹配,参数位置类型要和被代理方法一致	 public void access(Date date, String food,Object rvt){	 System.out.println(date+"吃"+food+"-----");	 System.out.println("返回值为:"+rvt); }	}

在XML文件中配置切面

<bean id="myAspect1" class="net.csdn.www.aop.MyAspect1"></bean>	<!-- 切入点 -->	<aop:config>		<aop:aspect id="asp1" ref="myAspect">			<aop:before method="authority" pointcut="execution(* net.csdn.www.dao.*.*(..))" />			<aop:after method="release" pointcut="execution(* net.csdn.www.dao.*.*(..))" />			<aop:after-retuing method="log"				pointcut="execution(* net.csdn.www.dao.*.*(..))" retuing="rvt" />			<aop:around method="processTx" pointcut="execution(* net.csdn.www.dao.*.*(..))" />		</aop:aspect>		 <aop:aspect id="asp2" ref="myAspect1" order="1">			<aop:after-retuing method="access"				pointcut="execution(* net.csdn.www.dao.*.*(..)) and args(date,food)"				retuing="rvt" />		</aop:aspect>	</aop:config>

调用userDao.eat(new Date(), "xiaochi");

springAOP基于XML配置文件方式 _ JavaClub全栈架构师技术笔记


设置切入点

<aop:pointcut expression="execution(* csdn.dao.*.*(..))" id="mypoint"/>
id 切入点的标示名
expression 切入点表达式
<aop:after method="release" pointcut-ref="mypoint"/>


把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">	<!-- 切面 -->	<bean id="myAspect" class="net.csdn.www.aop.MyAspect"></bean>	<bean id="userDao" class="net.csdn.www.dao.UserDao"></bean>	<bean id="myAspect1" class="net.csdn.www.aop.MyAspect1"></bean>	<!-- 切入点 -->	<aop:config>	<aop:pointcut expression="execution(* net.csdn.www.dao.*.*(..)) and args(date,food)" id="xxx"/> 		<aop:aspect ref="myAspect1"> <aop:after-retuing method="access" 			pointcut-ref="xxx" retuing="rvt"/> </aop:aspect> 	<aop:aspect id="asp1" ref="myAspect" order="2">			<aop:before method="authority" pointcut="execution(* net.csdn.www.dao.*.*(..))" />			<aop:after method="release" pointcut="execution(* net.csdn.www.dao.*.*(..))" />			<aop:after-retuing method="log"				pointcut="execution(* net.csdn.www.dao.*.*(..))" retuing="rvt" />			<aop:around method="processTx" pointcut="execution(* net.csdn.www.dao.*.*(..))" />		</aop:aspect>	</aop:config></beans>

springAOP基于XML配置文件方式 _ JavaClub全栈架构师技术笔记


作者:闫梅
来源链接:https://blog.csdn.net/u011937340/article/details/22426971

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/java/112563.html

标签:SpringAOP
分享给朋友:

“springAOP基于XML配置文件方式” 的相关文章

Redis的搭建(win和linux版) 2022年05月15日 21:58:56
SpringBoot整合Redis缓存 2022年05月15日 21:59:14
SpringBoot 如何统一后端返回格式 2022年05月16日 18:59:41
AIX系统下挂载外置存储 2022年05月16日 19:50:58
SpringCloud简单实例 2022年05月16日 20:38:34
java计数循环及小技巧 2022年05月21日 11:37:19