spring框架-AOP_基于注解完全开发_基于xml实现_demo07/08
基本概念
底层原理
底层原理JDK动态代理实现
AOP操作术语
AOP准备工作
AOP-aspect注解1
AOP-aspect注解2
AOP-aspect配置文件
1 基本概念
1 什么是AOP
(1)面向切面编程(方面),利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
(2) 同属描述:不通过修改源代码的方式,在主干功能里面添加新的功能
(3) 使用登录案例说明
2 底层原理
2.1 AOP底层使用动态代理实现(两种)
第一种:JDK动态代理—有接口的情况
第二种:CGLIB动态代理-没有借口的情况
3 底层原理JDK动态代理实现
(1)JDK动态代理,使用Proxy类里面的方法创建代理对象
(2)JDK代理的方法 newProxyInstance
JDK8-Proxy
方法有三个参数
第一个参数:类加载器
第二个参数:增强方法所在的类,这个类实现了借口
第三个参数:实现了这个借口InvocationHandler,创建代理对象,写增强的方法
(3) JDK动态代理代码实现
床架接口,定义方法
package com.zzy;
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}
创建接口实现类,实现方法
package com.zzy;
public class UserDaoImpl implements UserDao{
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public String update(String id) {
return id;
}
}
使用Proxy类创建接口的代理对象
package com.zzy;
import javax.sound.midi.Soundbank;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class JDKProxy {
public static void main(String[] args) {
//创建借口实现类代理对象
Class[] interfaces = {UserDao.class};
/*匿名内部类实现InvocationHandler接口*/
// Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
// @Override
// public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// return null;
// }
// });
UserDaoImpl userDaoImpl = new UserDaoImpl();
//创建UserDao接口的实现类的代理对象
// 类加载器 被代理的接口
UserDao userDao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDaoImpl));
int total = userDao.add(1, 2);
System.out.println("total:"+total);
}
}
//创建代理对象代码,做功能增强
class UserDaoProxy implements InvocationHandler{
//创建的是谁的额代理对象,就需要把谁传递过来
//通过有参构造进行传递
private UserDaoImpl userDaoImpl;
public UserDaoProxy(UserDaoImpl userDaoImpl) {
this.userDaoImpl = userDaoImpl;
}
//增强逻辑
@Override //proxy代理对象 //被增强的方法 //执行增强方法传递的参数
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//在方法执行之前执行
System.out.println("在方法执行之前执行......"+"被增强的方法的名称:"+method.getName()+"......传递的参数"+ Arrays.toString(args));
//被增强的方法
Object result =method.invoke(userDaoImpl, args);
System.out.println("result: "+result);
//在方法执行之后执行
System.out.println("方法之后执行。。。"+userDaoImpl);
return result;
}
}
执行输出结果
4 AOP操作术语
4.1术语名称
(1)连接点:类里面哪些方法可以被增强,这些方法就被称之为连接点
(2)切入点:实际被增强的方法,称之为切入点
(3)通知(增强):实际增强的逻辑部分,就叫做通知,通知类型:前置,后置,环绕,异常,最终
(4)切面:是操作动作,就是把通知应用到切入点的过程
5 AOP准备工作
5.1 Spring框架一般都是基于AspectJ实现AOP操作的
AspectJ 不是Spring的组成部分,是独立的AOP框架。一般将AspectJ和Spring框架一起使用,进行AOP操作
5.2 基于AspectJ 实现AOP操作
(1)基于xml配置文件实现
(2)基于注解方式实现
5.3 3.在项目工程中引入AOP相关依赖
5.4 切入点表达式
(1)切入点表达式的作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构 execution(【权限修饰符】【返回值类型】【类全路径】【方法名称】【参数列表】)
举例1:对com.zzy.dao.UserDao类里面的方法add进行增强
execution( com.zzy.dao.UserDao.add(…))*
举例2:对com.zzy.dao.UserDao类里面的所有方法进行增强
execution( com.zzy.dao.UserDao.(…))*
举例2:对com.zzy.dao包里面所有类,类里面的所有方法进行增强
*execution( com.zzy.dao..*.{…})**
6 AOP-aspectJ注解1
6.1创建目标类User
package com.zzy.aopannotation;
public class User {
public void add(){
System.out.println("add..............");
}
}
6.2 创建增强类(编写增强的逻辑)
在增强了里面,创建方法,让不同的方法代表不同的通知类型
package com.zzy.aopannotation;
/*增强的类*/
public class UserProxy {
/*前置通知*/
public void before(){
System.out.println("before.......");
}
}
6.3进行通知的配置
(1)在spring配置文件中开启注解的扫描
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.开启注解扫描-->
<context:component-scan base-package="com.zzy.aopannotation"></context:component-scan>
<!-- 4.开启AspectJ生成代理对象
意思就是:会去类里面寻找@Aspect注解,就生成代理对象
-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
(2)使用注解创建User和UserProxy对象
(3)在增强类上添加@Aspect注解
(4)在spring配置文件中开启生成代理对象
<!-- 4.开启AspectJ生成代理对象
意思就是:会去类里面寻找@Aspect注解,就生成代理对象
-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
6.4 配置不同类型的通知
在增强类里面,为通知方法添加通知类型注解,使用切入点表达式配置
package com.zzy.aopannotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/*增强的类*/
@Component//交给spring容器创建对象
@Aspect//表示切面配置类,创建代理对象
public class UserProxy {
/*前置通知,在被增强类的add方法之前执行*/
@Before(value = "execution(* com.zzy.aopannotation.User.add(..))")
public void before(){
System.out.println("before.......");
}
/*后置最终通知,在被增强类的add方法之后执行,不安官方发有没有异常都会执行*/
@After(value = "execution(* com.zzy.aopannotation.User.add(..))")
public void after(){
System.out.println("After.......");
}
/*返回通知 :在方法返回结果之后执行,如果方法出现异常,就不执行*/
@AfterReturning(value = "execution(* com.zzy.aopannotation.User.add(..))")
public void afterReturn(){
System.out.println("AfterReturning.......");
}
/*异常通知:在方法出现异常之后执行*/
@AfterThrowing(value = "execution(* com.zzy.aopannotation.User.add(..))")
public void afterThrow(){
System.out.println("AfterThrowing.......");
}
/*环绕通知:在方法执行之前执行,在方法执行之后执行*/
@Around(value = "execution(* com.zzy.aopannotation.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前执行。。。。。");
//倍增墙的方法执行,也就是add方法被封装到jp中执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后执行。。。。。");
}
}
测试并输出结果
package com.zzy.aopannotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beananno.xml");
User user = context.getBean("user", User.class);
user.add();
}
}
6.5 相同的切入点抽取
/*相同切入点的提取*/
@Pointcut(value = "execution(* com.zzy.aopannotation.User.add(..))")
public void pointdemo(){
}
/*前置通知,在被增强类的add方法之前执行*/
@Before(value = "pointdemo()")
public void before(){
System.out.println("before.......");
}
6.6 有多个增强类对用一个方法进行增强,这是增强类的优先级
在增强类上添加注解@Order,其中的值越小,优先级越高
6.7 基于注解完全开发
创建配置类ConfigAOP,代替beananno中的配置
package com.zzy.aopannotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
/*完全注解开发,不需要xml配置文件*/
@Component//交给spring容器管理
@ComponentScan(basePackages = "com.zzy")//开启扫描
@EnableAspectJAutoProxy(proxyTargetClass = true)//开启AspectJ生成代理对象
public class ConfigAop {
}
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.开启注解扫描-->
<context:component-scan base-package="com.zzy.aopannotation"></context:component-scan>
<!-- 4.开启AspectJ生成代理对象
意思就是:会去类里面寻找@Aspect注解,就生成代理对象
-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
7 AOP-aspectJ基于配置文件实现
7.1 创建两个类(增强类和被增强类), 创建方法
package com.zzy.aopxml;
/*被增强类*/
public class Book {
public void buy(){
System.out.println("buy ......");
}
}
package com.zzy.aopxml;
public class BookProxy {
public void before(){
System.out.println("before..........");
}
}
7.2 在spring文件中创建两个类对象
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--创建被增强类的对象-->
<bean id="book" class="com.zzy.aopxml.Book"></bean>
<!--创建增强类的对象-->
<bean id="bookProxy" class="com.zzy.aopxml.BookProxy"></bean>
</beans>
7.3 在spring配置文件中配置切入点
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--创建被增强类的对象-->
<bean id="book" class="com.zzy.aopxml.Book"></bean>
<!--创建增强类的对象-->
<bean id="bookProxy" class="com.zzy.aopxml.BookProxy"></bean>
<!--配置AOP的增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.zzy.aopxml.Book.buy(..))"/>
<!--配置切面 ref引用增强类-->
<aop:aspect ref="bookProxy">
<!--增强作用在具体的方法上
通知类型是before ,增强方法是before 增强方法作用在buy方法切入点上-->
<aop:before method="before" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
7.4 测试并输出结果
package com.zzy.aopxml;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAopXML {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanxml.xml");
Book book = context.getBean("book", Book.class);
book.buy();
}
}