spring 配置文件兑现AOP-学习笔记

spring 配置文件实现AOP-学习笔记

 

Spring 对AOP的支持I:

Aspect默认情况下不用实现接口,但对于目标对象,在默认的情况下必须实现接口 如果没有实现接口必须引入CGLIB库,我们可以通过Advice中添加一个JointPoint参数,这个值会由spring自动传入,从JointPoint中可以取得参数值,方法名等等。

Spring对AOP的支持II:

1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP。

2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP。

3、如果目标对象没有实现接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

在实现接口的情况下, 如果强制使用CGLIB实现AOP?

添加CGLIB库,SPRING_HOME/cglib/*.jar

在spring配置文件中添加<aop:aspectj-autoproxy proxy-target-class="true"/>

JDK动态代理实和CGLIB字节码生成的区别?

JDK动态代理只能对实现了接口的类生成代理,而不能针对普通类。

CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法因为是继承,所以该类或方法最好不要声明成final

 

 

 

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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">
	
	<!-- 
		打开配置项,这个配置项是对@Aspectj这个注解进行支持 注解本身是不能干活的,注解之所以能干活是因为后面有处理器对其进行处理
		这个配置相当于我们将要使用的@Aspectj注解提供了解析的功能
	 -->
	<aop:aspectj-autoproxy />
	<context:component-scan base-package="com.sample.*"/>
	<bean id="apectBean" class="com.sample.service.MyInterceptor2"/>
	<aop:config>
		<aop:aspect id="asp" ref="apectBean">
			<aop:pointcut id="mycut" expression="execution (void com.sample.service.impl.PersonServiceBean*.*(String))"/>
			<aop:before pointcut-ref="mycut" method="doAccessCheck" arg-names="name" />
			<aop:after pointcut-ref="mycut" method="doAfter"/>
			<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
			<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
			<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
		</aop:aspect>
	</aop:config>
	
	<aop:config>
		<aop:aspect id="myAOP" ref="check">
			<aop:pointcut id="target" expression="execution(* com.sample.service.Common.execute2(..))"/>
			<aop:before method="checkValidator" pointcut-ref="target"/>
			<aop:after method="addLog" pointcut-ref="target"/>
		</aop:aspect>
	</aop:config>
</beans>

 

 package com.sample.service;
import org.springframework.stereotype.Service;
@Service("common")
public class Common {
	public void execute(String userName, String password) {
		System.out.println("------------------普通类----------------");
	}
	
	public void execute2(Stock stock) {
		System.out.println("------------------普通类----------------");
	}
}

 

package com.sample.service;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Service;

@Service("check")
public class Check {
	public void checkValidator() {
		System.out.println("------------------验证合法性----------------");
	}
	public void addLog(JoinPoint jp) {
		System.out.println("------------------添加日志----------------");
		Object[] objs = jp.getArgs();
		for (Object o : objs) {
			Stock stock = (Stock)o;
			System.out.println(stock.getLId());
		}
		
		System.out.println("========checkSecurity=="+jp.getSignature().getName());//这个是获得方法名
	}
}