Spring之AOP

AOP术语

切入点哪些方法拦截后进行安全性检查(类中一部分方法)


通知对切入点拦截之后,要干什么,通知在java中以方法的形式出现。切面要完成的功能就是通知(即切面这个类里的方法)


切面
通知的模块化(通知(方法)要放在一个类中)


连接点当具体要执行某个某个方法的时候,该方法就是连接的点


目标对象代理模式中的真实主题对象


织入把通知应用到目标对象创建代理的过程



如何定义切入点表达式:

<!-- 如何定义且切入点表达式:
                   * execution - 匹配方法执行的连接点,这是你将会用到的Spring的最主要的切入点指示符。
                   * 例如:要对UserManagerImpl类中以save开始方法进行安全性检查
                        execution("public * cn.xxc.aop.xml.before.UserManagerImpl.save*(String,String) ") 
                    
                   * Spring AOP 用户可能会经常使用 execution切入点指示符。执行表达式的格式如下:
                   * execution(
                        1 modifiers-pattern? 修饰符模式?:
                                             :方法使用的修饰符  public
                        2 ret-type-pattern   返回类型模式(必须)*
                                * 返回类型模式决定了方法的返回类型必须依次匹配一个连接点。 
                                * 你会使用的最频繁的返回类型模式是*,它代表了匹配任意的返回类型
                        3 declaring-type-pattern?  声明类型模式?
                                * 方法所在类的全路径  cn.xxc.aop.xml.before.UserManagerImpl
                        4 name-pattern       名字模式(必须)*
                                * 名字模式匹配的是方法名。 你可以使用*通配符作为所有或者部分命名模式
                                 saveuser  方法必须是saveUser方法
                                 save*     方法一save开始的方法
                                 save*del  开始是save 后缀是del的方法
                        5(param-pattern)    参数模式(必须)*
                               * ()匹配了一个不接受任何参数的方法
                               * (..) 匹配了一个接受任意数量参数的方法(零或者更多)
                               * (*)匹配了一个接受一个任何类型的参数的方法
                               * 模式(*,String)匹配了一个接受两个参数的方法,第一个可以是任意类型, 第二个则必须是String类型
                        6 throws-pattern?)   异常模式?
                  
					     任意公共方法的执行:
						execution(public * *(..))
						
						任何一个名字以“set”开始的方法的执行:
						execution(* set*(..))
						
						AccountService接口定义的任意方法的执行:
						execution(* com.xyz.service.AccountService.*(..))
						
						在service包中定义的任意方法的执行:
						execution(* com.xyz.service.*.*(..))
						
						在service包或其子包中定义的任意方法的执行:
						execution(* com.xyz.service..*.*(..))
-->






UserManger接口:

public interface UserManger {
	public void saveUser();
}


UserMangerImpl:

public class UserMangerImpl implements UserManger {

	public void saveUser() {
		System.out.println("保存");
	}
}

切面,通知方法所在的类

public class Secutity {
	/**
	 *  * 任何通知方法可以将第一个参数定义为org.aspectj.lang.JoinPoint类型 
	 *  * (环绕通知需要定义第一个参数为ProceedingJoinPoint类型, 它是 JoinPoint 的一个子类)。
	 *  *    JoinPoint 接口提供了一系列有用的方法,比如 getArgs()(返回方法参数)、 
	 *       getThis()(返回代理对象)、getTarget()(返回目标)、
	 *       getSignature()(返回正在被通知的方法相关信息)
	 * 
	 *  checkSecurity(JoinPoint joinPoint)
	 *   * 该方法的参数是由spring在运行时,传递进来的,不写也行
	 *   
	 *   
	 *   * 怎么阻止程序执行目标对象方法的
	 *       * 抛出异常,阻止目标方法的执行
	 */
	public void checkSecurity(JoinPoint joinPoint){
		//获取参数
		Object[] objs = joinPoint.getArgs();
		if(objs!=null && objs.length>0){
			for(Object obj:objs){
				System.out.println(obj);
			}
		}
		//获取方法的签名
		System.out.println("方法签名--->"+joinPoint.getSignature().getName());
		
		System.out.println("进行安全性检查");
	}
}




基于xml方式配置



1.前置通知:

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
		 <!--
         	  要使用spring进行编程
              * 引入spring的aop的命名空间
                   *xmlns:aop="http://www.springframework.org/schema/aop"       
                   *http://www.springframework.org/schema/aop 
                   *http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                
              * 引入jar包aspectjweaver.jar   aspectjrt.jar
              
              * 如果想使用注解:common-annotations.jar
         -->
         
        <!-- 声明目标对象用于创建代理对象的
              Spring执行的时候,根据切入点所管理的范围,决定是否为目标对象创建代理对象
        -->
		<bean id="userManger" class="com.xxc.aop.xml.after.UserMangerImpl"/>
		
		<!-- 声明Security类的对象 -->
		<bean id="secutity" class="com.xxc.aop.xml.after.Secutity"/>
		
		<!-- 在Spring的配置文件中,所有的切面和通知都必须定义在aop:config元素内部 -->
		<aop:config>
			<!--aop:aspect  声明security对象为切面  -->
			<aop:aspect ref="secutity">
				<!-- 声明切入点 -->
				<aop:pointcut expression="execution( * com.xxc.aop.xml.after.UserManger.save*(..))" id="perform" />
				 <!-- 最终通知无论如何都会在匹配方法后执行。在aop:aspect中使用aop:after元素来声明它。
                      * method="checkSecurity" 切面中的方法(通知)
                      * pointcut-ref="perform":该通知应用的切入点
              	 -->
				<aop:after method="checkSecurity" pointcut-ref="perform"/>
			</aop:aspect>
		</aop:config>
</beans>


测试类:

public class App {
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/aop/xml/after/applicationContext.xml");
		UserManger um = (UserManger) ac.getBean("userManger");
		um.saveUser();
	}
}




2.后置通知:

切面

public class Secutity {
	/*
	 *  <aop:after-returning method="checkSecurity" pointcut-ref="perform" returning="val"/>    
	 *        方法的第二个参数是传递方法的返回值的Object val 
	 *        该参数的名称必须和aop:after-returning配置文件中returning属性的值相同   
	 */
	public void checkSecurity(JoinPoint joinPoint,Object val){
		System.out.println("返回值是——-----》"+val);
		System.out.println("进行安全性检查");
	}
}



applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
         
        <!-- 声明目标对象用于创建代理对象的
              Spring执行的时候,根据切入点所管理的范围,决定是否为目标对象创建代理对象
        -->
		<bean id="userManger" class="com.xxc.aop.xml.after.UserMangerImpl"/>
		
		<!-- 声明Security类的对象 -->
		<bean id="secutity" class="com.xxc.aop.xml.after.Secutity"/>
		
		<!-- 在Spring的配置文件中,所有的切面和通知都必须定义在aop:config元素内部 -->
		<aop:config>
			<!--aop:aspect  声明security对象为切面  -->
			<aop:aspect ref="secutity">
				<!-- 声明切入点 -->
				<aop:pointcut expression="execution( * com.xxc.aop.xml.after.UserManger.*(..))" id="perform" />
				 <!-- 后置通知在匹配的方法完全执行后运行。在aop:aspect中使用aop:after-returning元素来声明它。
                      * method="checkSecurity" 切面中的方法(通知)
                      * pointcut-ref="perform":该通知应用的切入点
                      * returning="val":方法的返回值保存在val这个参数中
               	 -->
				<aop:after-returning method="checkSecurity" pointcut-ref="perform" returning="val"/>
			</aop:aspect>
		</aop:config>
</beans>



3.异常通知:只有当方法抛出异常的时候才会执行的通知。


没异常则不执行。

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
         
        <!-- 声明目标对象用于创建代理对象的
              Spring执行的时候,根据切入点所管理的范围,决定是否为目标对象创建代理对象
        -->
		<bean id="userManger" class="com.xxc.aop.xml.afterThorwing.UserMangerImpl"/>
		
		<!-- 声明Security类的对象 -->
		<bean id="secutity" class="com.xxc.aop.xml.afterThorwing.Secutity"/>
		
		<!-- 在Spring的配置文件中,所有的切面和通知都必须定义在aop:config元素内部 -->
		<aop:config>
			<!--aop:aspect  声明security对象为切面  -->
			<aop:aspect ref="secutity">
				<!-- 声明切入点 -->
				<aop:pointcut expression="execution( * com.xxc.aop.xml.afterThorwing.UserManger.save*(..))" id="perform" />
				 <!-- 异常通知在匹配方法抛出异常退出时执行。在aop:aspect中使用aop:after-throwing元素来声明它。
                      * method="checkSecurity" 切面中的方法(通知)
                      * pointcut-ref="perform":该通知应用的切入点
                      * throwing="ex":方法的抛出的异常保存在ex这个参数中
                 -->
				<aop:after-throwing method="checkSecurity" pointcut-ref="perform" throwing="ex"/>
			</aop:aspect>
		</aop:config>
</beans>

UserMangerImpl

public class UserMangerImpl implements UserManger {

	public void saveUser(String name,String sex) {
		System.out.println(name+"----"+sex+"---保存");
		//抛出异常,否则异常通知不会执行
		if(true)
			throw new RuntimeException("lllllllllllllll");
	}

	@Override
	public String findUser() {
		return "返回值";
	}
}


4.最终通知:不管是否抛出异常,都会执行此通知

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
         
        <!-- 声明目标对象用于创建代理对象的
              Spring执行的时候,根据切入点所管理的范围,决定是否为目标对象创建代理对象
        -->
		<bean id="userManger" class="com.xxc.aop.xml.after.UserMangerImpl"/>
		
		<!-- 声明Security类的对象 -->
		<bean id="secutity" class="com.xxc.aop.xml.after.Secutity"/>
		
		<!-- 在Spring的配置文件中,所有的切面和通知都必须定义在aop:config元素内部 -->
		<aop:config>
			<!--aop:aspect  声明security对象为切面  -->
			<aop:aspect ref="secutity">
				<!-- 声明切入点 -->
				<aop:pointcut expression="execution( * com.xxc.aop.xml.after.UserManger.*(..))" id="perform" />
				 <!-- 最终通知无论如何都会在匹配方法退出后执行。在aop:aspect中使用aop:after元素来声明它。
                      * method="checkSecurity" 切面中的方法(通知)
                      * pointcut-ref="perform":该通知应用的切入点
              	 -->
				<aop:after method="checkSecurity" pointcut-ref="perform"/>
			</aop:aspect>
		</aop:config>
</beans>


5.环绕通知:需要手动调用目标对象的方法

切面:

public class Secutity {
	/**
	 *  * 任何通知方法可以将第一个参数定义为org.aspectj.lang.JoinPoint类型 
	 *  * (环绕通知需要定义第一个参数为ProceedingJoinPoint类型, 它是 JoinPoint 的一个子类)。
	 *  *    JoinPoint 接口提供了一系列有用的方法,比如 getArgs()(返回方法参数)、 
	 *       getThis()(返回代理对象)、getTarget()(返回目标)、
	 *       getSignature()(返回正在被通知的方法相关信息)
	 * 
	 *  checkSecurity(ProceedingJoinPoint joinPoint)
	 *   * 该方法的参数是由spring在运行时,传递进来的
	 * 
	 *   * 该通知要增加方法的返回值
	 *   
	 */
	public Object checkSecurity(ProceedingJoinPoint joinPoint) throws Throwable{
		//调用目标对象的方法,控制底层方法的执行,不会自动调用
		Object obj = joinPoint.proceed();
		System.out.println("进行安全性检查");
		return obj;
	}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
         
        <!-- 声明目标对象用于创建代理对象的
              Spring执行的时候,根据切入点所管理的范围,决定是否为目标对象创建代理对象
        -->
		<bean id="userManger" class="com.xxc.aop.xml.around.UserMangerImpl"/>
		
		<!-- 声明Security类的对象 -->
		<bean id="secutity" class="com.xxc.aop.xml.around.Secutity"/>
		
		<!-- 在Spring的配置文件中,所有的切面和通知都必须定义在aop:config元素内部 -->
		<aop:config>
			<!--aop:aspect  声明security对象为切面  -->
			<aop:aspect ref="secutity">
				<!-- 声明切入点 -->
				<aop:pointcut expression="execution( * com.xxc.aop.xml.around.UserManger.*(..))" id="perform" />
				<!-- Around通知使用aop:around元素来声明
                      * method="checkSecurity" 切面中的方法(通知)
                      * pointcut-ref="perform":该通知应用的切入点
              	-->
				<aop:around method="checkSecurity" pointcut-ref="perform"/>
			</aop:aspect>
		</aop:config>
</beans>




基于注解方式配置:

appliactionContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
		  <!-- 
	          	  通过在你的Spring的配置中引入下列元素来启用Spring对@AspectJ的支持
	             * 注册对注解解析的处理器
	             * 使用自动代理
	      -->
	      <aop:aspectj-autoproxy/>
	      
	        <!-- 声明Security类的对象 -->
	      <bean id="security" class="com.xxc.aop.aspectJ.befroe.Secutity"/>
	      
	      
	      <!-- 声明目标对象用于创建代理对象的
	           Spring执行的时候,根据切入点所管理的范围,决定是否为目标对象创建代理对象
	      -->
	      <bean id="userManager" class="com.xxc.aop.aspectJ.befroe.UserMangerImpl"/>
</beans>


1.前置通知:

//声明切面
@Aspect
public class Secutity {
	/**
	 * 定义切入点
	 *   * 一个切入点签名通过一个普通的方法定义来提供
	 *   * 使用@Pointcut定义
	 *   
	 *   * 方法的要求
	 *      * 方法最好是private修改
	 *      * 方法的返回类型为void
	 *      * 方法的名称自定义(该方法名称+()就是后边要引用的切入点的名称)
	 *      * 方法体为空{}
	 */
	@Pointcut("execution(* com.xxc.aop.aspectJ.befroe.UserMangerImpl.save*(..))")
	private void perfrom(){}
	
	//定义前置通知
	@Before("perfrom()")
	public void checkSecurity(JoinPoint joinPoint){
		//获取参数
		Object[] objs = joinPoint.getArgs();
		if(objs!=null && objs.length>0){
			for(Object obj:objs){
				System.out.println(obj);
			}
		}
		//获取方法的签名
		System.out.println("方法签名--->"+joinPoint.getSignature().getName());
		
		System.out.println("进行安全性检查");
	}
}

2.后置通知:

//声明切面
@Aspect
public class Secutity {
	/**
	 * 定义切入点
	 *   * 一个切入点签名通过一个普通的方法定义来提供
	 *   * 使用@Pointcut定义
	 *   
	 *   * 方法的要求
	 *      * 方法最好是private修改
	 *      * 方法的返回类型为void
	 *      * 方法的名称自定义(该方法名称+()就是后边要引用的切入点的名称)
	 *      * 方法体为空{}
	 */
	@Pointcut("execution(* com.xxc.aop.aspectJ.afterReturning.UserMangerImpl.*(..))")
	private void perfrom(){}
	
	//定义后置通知
	@AfterReturning(value="perfrom()",returning="val")
	public void checkSecurity(JoinPoint joinPoint,Object val){
		System.out.println("val---->"+val);
		System.out.println("进行安全性检查");
	}
}


3.定义异常通知

//声明切面
@Aspect
public class Secutity {
	/**
	 * 定义切入点
	 *   * 一个切入点签名通过一个普通的方法定义来提供
	 *   * 使用@Pointcut定义
	 *   
	 *   * 方法的要求
	 *      * 方法最好是private修改
	 *      * 方法的返回类型为void
	 *      * 方法的名称自定义(该方法名称+()就是后边要引用的切入点的名称)
	 *      * 方法体为空{}
	 */
	@Pointcut("execution(* com.xxc.aop.aspectJ.afterThrowing.UserMangerImpl.*(..))")
	private void perfrom(){}
	
	//定义异常通知
	@AfterThrowing(value="perfrom()",throwing="ex")
	public void checkSecurity(JoinPoint joinPoint,Throwable ex){
		System.out.println("异常----------->"+ex);
		System.out.println("进行安全性检查");
	}
}



4.定义最终通知

//声明切面
@Aspect
public class Secutity {
	/**
	 * 定义切入点
	 *   * 一个切入点签名通过一个普通的方法定义来提供
	 *   * 使用@Pointcut定义
	 *   
	 *   * 方法的要求
	 *      * 方法最好是private修改
	 *      * 方法的返回类型为void
	 *      * 方法的名称自定义(该方法名称+()就是后边要引用的切入点的名称)
	 *      * 方法体为空{}
	 */
	@Pointcut("execution(* com.xxc.aop.aspectJ.after.UserMangerImpl.*(..))")
	private void perfrom(){}
	
	//定义最终通知
	@After("perfrom()")
	public void checkSecurity(JoinPoint joinPoint){
		System.out.println("进行安全性检查");
	}
}

5.定义环绕通知

//声明切面
@Aspect
public class Secutity {
	/**
	 * 定义切入点
	 *   * 一个切入点签名通过一个普通的方法定义来提供
	 *   * 使用@Pointcut定义
	 *   
	 *   * 方法的要求
	 *      * 方法最好是private修改
	 *      * 方法的返回类型为void
	 *      * 方法的名称自定义(该方法名称+()就是后边要引用的切入点的名称)
	 *      * 方法体为空{}
	 */
	@Pointcut("execution(* com.xxc.aop.aspectJ.befroe.UserMangerImpl.save*(..))")
	private void perfrom1(){}
	
	@Pointcut("execution(* com.xxc.aop.aspectJ.befroe.UserMangerImpl.find*(..))")
	private void perfrom2(){}
	
	@Pointcut("perfrom1() || perfrom2()")  
	private void perfrom3(){}
	
	//定义前置通知
	@Around("perfrom3()")  //或者  @Around("perfrom1() || perfrom2()")
	public Object checkSecurity(ProceedingJoinPoint joinPoint) throws Throwable{
		Object obj = joinPoint.proceed();
		System.out.println("进行安全性检查");
		return obj;
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值