spring框架学习(4)AOP(上)

什么是AOP

@Aspect // 声明一个切面
@Component
public class MyAspect {
    // 原业务方法执行前
    @Before("execution(public void com.rudecrab.test.service.*.doService())")
    public void methodBefore() {
        System.out.println("===AspectJ 方法执行前===");
    }

    // 原业务方法执行后
    @AfterReturning("execution(* com.rudecrab.test.service..doService(..))")
    public void methodAddAfterReturning() {
        System.out.println("===AspectJ 方法执行后===");
    }
}
View Code

为什么需要AOP

从Spring的角度看,AOP最大的用途就在于提供了事务管理的能力。

事务管理就是一个关注点,你的正事就是去访问数据库,而你不想管事务(太烦),所以,Spring在你访问数据库之前,自动帮你开启事务,当你访问数据库结束之后,自动帮你提交/回滚事务!

AOP思想介绍 

按照 AOP 框架修改源代码的时机,可以将其分为两类:

  • 静态 AOP 实现, AOP 框架在编译阶段对程序源代码进行修改,生成了静态的 AOP 代理类(生成的 *.class 文件已经被改掉了,需要使用特定的编译器),比如 AspectJ。
  • 动态 AOP 实现, AOP 框架在运行阶段对动态生成代理对象(在内存中以 JDK 动态代理,或 CGlib 动态地生成 AOP 代理类),如 SpringAOP。

aop底层将采用代理机制进行实现。

接口+实现类时 :spring采用jdk的动态代理Proxy。

只有实现类时:spring 采用cglib字节码增强。

静态代理和动态代理

Spring实现AOP的原理

1.jdk动态代理(优先)

  缺点是被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术。

2.cglib代理(没有接口)

  第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.

代码示例

UserService.java

package cn.mf.service;

public interface UserService {
    void save();
    void delete();
    void update();
    void find();
}
View Code

UserServiceImpl.java

package cn.mf.service;

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("保存用户!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("删除用户!");
    }
    @Override
    public void update() {
        System.out.println("更新用户!");
    }
    @Override
    public void find() {
        System.out.println("查找用户!");
    }
}
View Code

动态代理

package cn.mf.c_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import cn.mf.service.UserService;
import cn.mf.service.UserServiceImpl;
//观光代码=>动态代理
public class UserServiceProxyFactory implements InvocationHandler {
    
    public UserServiceProxyFactory(UserService us) {
        super();
        this.us = us;
    }

    private UserService us;
    
    public UserService getUserServiceProxy(){
        //生成动态代理
        UserService usProxy = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),
                                    UserServiceImpl.class.getInterfaces(), 
                                    this);
        //返回
        return usProxy;
    }

    @Override
    public Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable {
        System.out.println("打开事务!");
        Object invoke = method.invoke(us, arg2);
        System.out.println("提交事务!");
        return invoke;
    }

}
View Code

cglib代理

package cn.mf.c_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import cn.mf.service.UserService;
import cn.mf.service.UserServiceImpl;

//观光代码=>cglib代理
public class UserServiceProxyFactory2 implements MethodInterceptor {
    

    public UserService getUserServiceProxy(){
        Enhancer en = new Enhancer();//帮我们生成代理对象
        en.setSuperclass(UserServiceImpl.class);//设置对谁进行代理
        en.setCallback(this);//代理要做什么
        UserService us = (UserService) en.create();//创建代理对象
        return us;
    }

    @Override
    public Object intercept(Object prxoyobj, Method method, Object[] arg, MethodProxy methodProxy) throws Throwable {
        //打开事务
        System.out.println("打开事务!");
        //调用原有方法
        Object returnValue = methodProxy.invokeSuper(prxoyobj, arg);
        //提交事务
        System.out.println("提交事务!");
        return returnValue;
    }
}
View Code

Junit测试

package cn.mf.c_proxy;

import org.junit.Test;

import cn.mf.service.UserService;
import cn.mf.service.UserServiceImpl;

public class Demo {
    
    @Test
    //动态代理
    public void fun1(){
        UserService us = new UserServiceImpl();
        UserServiceProxyFactory factory = new UserServiceProxyFactory(us);
        UserService usProxy = factory.getUserServiceProxy();
        usProxy.save();
        //代理对象与被代理对象实现了相同的接口
        //代理对象 与 被代理对象没有继承关系
        System.out.println(usProxy instanceof UserServiceImpl );//false
    }
    
    @Test
    public void fun2(){
        UserServiceProxyFactory2 factory = new UserServiceProxyFactory2();
        UserService usProxy = factory.getUserServiceProxy();
        usProxy.save();
        //判断代理对象是否属于被代理对象类型
        //代理对象继承了被代理对象=>true
        System.out.println(usProxy instanceof UserServiceImpl );//true
    }
}
View Code

AOP术语

•连接点( join point )

  对应的是具体被拦截的对象,因为 Spring 只能支持方法,所以被拦截的对象往往就是指特定的方法,例如,我们前面提到的HelloServiceimpl的sayHello方法就是一个连接点,AOP将通过动态代理技术把它织入对应的流程中。

•切点(point cut)

  有时候,我们的切面不单单应用于单个方法,也可能是多个类的不同方法,这时,可以通过正则式和指示器的规则去定义,从而适配连接点 。 切点就是提供这样一个功能的概念 。

•通知(advice)  

  就是按照约定的流程下的方法,分为前置通知(before advice)、后置通知(afteradvice)、环绕通知(around advice)、事后返回通知(afterRetuming advice)和异常通知(afterThrowing advice ),它会根据约定织入流程中,需要弄明白它们在流程中的顺序和运行的条件。

•目标对象(target)

  即被代理对象,例如,约定编程中的HelloServicelmpl实例就是一个目标对象,它被代理了。

•引入( introduction )

  是指引入新的类和其方法,增强现有 Bean 的功能。

•织入( weaving )

  它是一个通过动态代理技术,为原有服务对象生成代理对象 , 然后将与切点定义匹配的连接点拦截,并按约定将各类通知织入约定流程的过程。

•切面( aspect)

  是一个可以定义切点、各类通知和引入的内容,SpringAOP 将通过它的信息来增强 Bean 的功能或者将对应的方法织入流程 。

Spring中的AOP代码实战之xml配置

1.导包4+2

1)spring的aop包

spring-aspects-4.2.4.RELEASE.jar

spring-aop-4.2.4.RELEASE.jar

2)spring需要第三方aop包

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

2.准备目标对象

UserService.java

package cn.mf.service;

public interface UserService {
    void save();
    void delete();
    void update();
    void find();
}
View Code

UserServiceImpl.java

package cn.mf.service;

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("保存用户!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("删除用户!");
    }
    @Override
    public void update() {
        System.out.println("更新用户!");
    }
    @Override
    public void find() {
        System.out.println("查找用户!");
    }
}
View Code

3.准备通知

MyAdvice.java

package cn.mf.d_springaop;

import org.aspectj.lang.ProceedingJoinPoint;

//通知类
public class MyAdvice {
    
    //前置通知    
//        |-目标方法运行之前调用
    //后置通知(如果出现异常不会调用)
//        |-在目标方法运行之后调用
    //环绕通知
//        |-在目标方法之前和之后都调用
    //异常拦截通知
//        |-如果出现异常,就会调用
    //后置通知(无论是否出现 异常都会调用)
//        |-在目标方法运行之后调用
//----------------------------------------------------------------
    //前置通知
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}
View Code

4.配置进行织入,将通知织入目标对象中

 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 准备工作: 导入aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
    <bean name="userService" class="cn.mf.service.UserServiceImpl" ></bean>
<!-- 2.配置通知对象 -->
    <bean name="myAdvice" class="cn.mf.d_springaop.MyAdvice" ></bean>
<!-- 3.配置将通知织入目标对象 -->
    <aop:config>
        <!-- 配置切入点 
            public void cn.itcast.service.UserServiceImpl.save() 
            void cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.*()
            
            * cn.itcast.service.*ServiceImpl.*(..)
            * cn.itcast.service..*ServiceImpl.*(..)
        -->
        <aop:pointcut expression="execution(* cn.mf.service.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice" >
            <!-- 指定名为before方法作为前置通知 -->
            <aop:before method="before" pointcut-ref="pc" />
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc" />
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- 异常拦截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
</beans>
View Code

 Spring中的AOP代码实战之注解配置

1.导包4+2

2.准备目标对象

3.准备通知

MyAdvice.java

package cn.mf.e_annotationaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
import org.aspectj.lang.annotation.Pointcut;

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {
    @Pointcut("execution(* cn.mf.service.*ServiceImpl.*(..))")
    public void pc(){}
    //前置通知
    //指定该方法是前置通知,并制定切入点
    @Before("MyAdvice.pc()")
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    @AfterReturning("execution(* cn.mf.service.*ServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }
    //环绕通知
    @Around("execution(* cn.mf.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    @AfterThrowing("execution(* cn.mf.service.*ServiceImpl.*(..))")
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    @After("execution(* cn.mf.service.*ServiceImpl.*(..))")
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}
View Code

4.配置进行织入,将通知织入目标对象中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 准备工作: 导入aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
    <bean name="userService" class="cn.mf.service.UserServiceImpl" ></bean>
<!-- 2.配置通知对象 -->
    <bean name="myAdvice" class="cn.mf.e_annotationaop.MyAdvice" ></bean>
<!-- 3.开启使用注解完成织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
View Code

Junit测试

package cn.mf.e_annotationaop;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.mf.bean.User;
import cn.mf.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/mf/e_annotationaop/applicationContext.xml")
public class Demo {
    @Resource(name="userService")
    private UserService us;
    
    @Test
    public void fun1(){
        us.save();
    }
    
}
View Code

 

JointPoint和ProceedingJoinPoint使用详解

 

@After,@Around,@Before

@Before是在方法执行前执行,@After在方法执行后执行,@Around环绕执行,可以再方法执行前后操作。

 

对象交给spring管理

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    <!-- 将User对象交给spring容器管理 -->
    <!-- Bean元素:使用该元素描述需要spring容器管理的对象
         class属性:被管理对象的完整类名.
         name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.
                    可以重复.可以使用特殊字符.
         id属性: 与name属性一模一样.
                    名称不可重复.不能使用特殊字符.
          结论: 尽量使用name属性.
      -->

    <bean id="pmsVariableDataSourceLoader"
          class="com.baomidou.springmvc.config.database.PMSVariableDataSourceLoader">
        <property name="url" value="1"/>
        <property name="username" value="2"/>
        <property name="password" value="3"/>
    </bean>
</beans>
View Code

PMSVariableDataSourceLoader

import lombok.Data;
@Data
public class PMSVariableDataSourceLoader {
    private String url;
    private String username;
    private String password;
}
View Code

ApplicationTest

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationTest {

    public static void main(String[] args) {
        // 使用ClassPathXmlApplicationContext获取spring容器ApplicationContext
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        // 根据bean id获取bean对象
        PMSVariableDataSourceLoader bean = (PMSVariableDataSourceLoader) applicationContext.getBean("pmsVariableDataSourceLoader");
        System.out.println(bean);
    }
}
View Code

PMSVariableDataSourceLoader(url=1, username=2, password=3)

资料

https://www.cnblogs.com/ziph/p/13339503.html

https://www.cnblogs.com/chenmingjun/p/9413977.html

posted @ 2017-04-17 20:19  ~沐风  阅读(252)  评论(0编辑  收藏  举报