在程序里面定义简单的AOP切面类
- /**
- * 定义Aspect
- */
- @Aspect
- public class ShopAddCheck {
- /**
- * 定义Pointcut,Pointcut的名称就是allAddMethod,
- * 此方法不能有返回值和参数,该方法只是一个标识
- * Pointcut的内容是一个表达式,描述那些对象的那些方法(订阅Joinpoint)
- */
- @Pointcut("execution(* save*(..))")
- public void allAddMethod(){};
- /**
- * 定义Advice,标识在那个切入点何处织入此方法
- */
- @Before("allAddMethod()")
- public void checkShop(){
- System.out.println("Check Shop");
- }
- }
- //测试代码
- BeanFactory bean = new ClassPathXmlApplicationContext("*.xml");
- ShopManager shopManager = (ShopManager) bean.getBean("shopManagerImp");
- shopManager.saveShop();
/**
* 定义Aspect
*/
@Aspect
public class ShopAddCheck {
/**
* 定义Pointcut,Pointcut的名称就是allAddMethod,
* 此方法不能有返回值和参数,该方法只是一个标识
* Pointcut的内容是一个表达式,描述那些对象的那些方法(订阅Joinpoint)
*/
@Pointcut("execution(* save*(..))")
public void allAddMethod(){};
/**
* 定义Advice,标识在那个切入点何处织入此方法
*/
@Before("allAddMethod()")
public void checkShop(){
System.out.println("Check Shop");
}
}
//测试代码
BeanFactory bean = new ClassPathXmlApplicationContext("*.xml");
ShopManager shopManager = (ShopManager) bean.getBean("shopManagerImp");
shopManager.saveShop();
配置文件中要加入
- <aop:aspectj-autoproxy/>
- <bean id="shopAddCheck" class="com.langhua.spring.ShopAddCheck"/>
- <bean id="shopManagerImp" class="com.langhua.spring.ShopManagerImp"/>
如果不在程序里面定义,也可以在配置文件里面定义
- <bean id="shopAddCheck" class="com.langhua.spring.ShopAddCheck"/>
- <bean id="shopManagerImp" class="com.langhua.spring.ShopManagerImp"/>
- <aop:config>
- <aop:aspect id="check" ref="shopAddCheck">
- <!-- addMethods只是一个标识 -->
- <aop:pointcut id="addMethods" expression="execution(* com.langhua.spring.*.save*(..))"/>
- <!-- checkShop表示具体要调用的方法 -->
- <aop:before method="checkShop" pointcut-ref="addMethods"/>
- </aop:aspect>
- </aop:config>
<bean id="shopAddCheck" class="com.langhua.spring.ShopAddCheck"/>
<bean id="shopManagerImp" class="com.langhua.spring.ShopManagerImp"/>
<aop:config>
<aop:aspect id="check" ref="shopAddCheck">
<!-- addMethods只是一个标识 -->
<aop:pointcut id="addMethods" expression="execution(* com.langhua.spring.*.save*(..))"/>
<!-- checkShop表示具体要调用的方法 -->
<aop:before method="checkShop" pointcut-ref="addMethods"/>
</aop:aspect>
</aop:config>
在AOP的方法中可以使用JoinPoint获得被切入方法的参数等信息
- public void checkShop(JoinPoint joinPoint){
- //获得切入方法传过来的参数,返回Ojbect数组
- joinPoint.getArgs();
- //获得切入方法的方法名
- joinPoint.getSignature().getName();
- }
public void checkShop(JoinPoint joinPoint){
//获得切入方法传过来的参数,返回Ojbect数组
joinPoint.getArgs();
//获得切入方法的方法名
joinPoint.getSignature().getName();
}
JDK代理和CGLIB代理的总结(生成代理对象的前提是有AOP切入)
1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
如果就是单纯的用IOC生成一个对象,也没有AOP的切入不会生成代理的,只会NEW一个实例,给Spring的Bean工厂
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
如何强制使用CGLIB实现AOP
* 添加CGLIB库
* 在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>就能强制使用
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换(没有实现接口的就用CGLIB代理,使用了接口的类就用JDK动态代理)
JDK动态代理和CGLIB字节码生成的区别?
* JDK动态代理只能对实现了接口的类生成代理,而不能针对类
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
因为是继承,所以该类或方法最好不要声明成final
JDK动态代理的例子
- //接口
- public interface ShopManager {
- public void addShop();
- public void delShop();
- public void saveShop();
- public String findShopName();
- }
- //具体实现
- public class ShopManagerImp implements ShopManager {
- public void addShop() {
- System.out.println("调用add方法");
- }
- public void delShop() {
- System.out.println("调用del方法");
- }
- public String findShopName() {
- System.out.println("调用find方法");
- return null;
- }
- public void saveShop() {
- System.out.println("调用save方法");
- }
- }
- //JDK动态代理
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- public class ShopCheck implements InvocationHandler {
- private Object proxyObj;
- public Object newProxy(Object proxyObj){
- this.proxyObj = proxyObj;
- //返回一个代理对象
- return Proxy.newProxyInstance(proxyObj.getClass().getClassLoader(),
- proxyObj.getClass().getInterfaces(),
- this);
- }
- //在使用shopManager类的时候会先调用这个方法(AOP)
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- //插入方法
- checkShop();
- //再调用代理对象的方法
- Object obj = method.invoke(this.proxyObj, args);
- return obj;
- }
- public void checkShop(){
- System.out.println("-------------CheckShop()------------");
- }
- }
- //最后的测试
- public class Click {
- public static void main(String[] args){
- ShopCheck shopcheck = new ShopCheck();
- ShopManager shopManager = (ShopManager) shopcheck.newProxy(new ShopManagerImp());
- shopManager.addShop();
- }
- }