1、静态代理
角色分析:
- 抽象角色:一般会使用接口或抽象类来解决
public interface Rent {
void rent();
}
- 真实角色:被代理的角色
public class Host implements Rent{
public void rent() {
System.out.println("房东有房子出租");
}
}
- 代理角色:代理真实角色,一般会有些附属操作
public class Proxy implements Rent{
private Host host;
public Proxy(){}
public Proxy(Host host){
this.host = host;
}
public void rent() {
seeHouse();
host.rent();
contract();
fare();
}
public void seeHouse(){
System.out.println("带客户看房");
}
public void contract(){
System.out.println("签合同");
}
public void fare(){
System.out.println("收租金");
}
}
- 客户:访问代理对象的人
public class Client {
public static void main(String[] args) {
Host host = new Host();
Proxy proxy = new Proxy(host);
proxy.rent();
}
}
好处:
- 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
- 公共的业务交给代理角色,实现了业务分工
- 公共业务发生扩展的时候,方便集中管理
坏处:
- 一个真实角色就会产生一个代理角色,代码量会很大,开发效率会变低
2、动态代理
动态代理分为两大类:基于接口的动态代理,基于类的动态代理
- 基于接口:JDK 动态代理(下面我们演示的是这种)
- 基于类:cglib
- Java 字节码实现:javasist
角色分析
- 抽象角色:一般会使用接口或抽象类来解决
public interface Rent {
void rent();
}
- 真实角色:被代理的角色
public class Host implements Rent{
public void rent() {
System.out.println("房东有房子出租");
}
}
- 动态生成代理的处理器
public class ProxyInvocationHandler implements InvocationHandler{
// 真实对象
private Object target;
// 设置要代理的对象
public void setTarget(Object target) {
this.target = target;
}
// 生成得到代理类
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
// 处理代理实例,并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 使用反射机制实现
doSomethingBefore();
Object result = method.invoke(target, args);
doSomethingAfter();
return result;
}
public void doSomethingBefore(){
System.out.println("do Something Before");
}
public void doSomethingAfter(){
System.out.println("do Something After");
}
}
- 客户:访问代理对象的人
public class Client {
public static void main(String[] args) {
// 真实角色
Host host = new Host();
// 生成代理类的一个处理器(动态生成代理)
ProxyInvocationHandler handler = new ProxyInvocationHandler();
handler.setTarget(host);
Rent proxy = (Rent) handler.getProxy();
proxy.rent();
}
}
好处:
- 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
- 公共的业务交给代理角色,实现了业务分工
- 公共业务发生扩展的时候,方便集中管理
- 一个动态代理类代理的是一个接口,一般就是对应的一类业务
- 一个动态代理类可以代理多个类,只要是实现了同一个接口即可
3、什么是 AOP
AOP (Aspect Oriented Programming) 意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术,AOP 是 OOP 的延续,是软件开发中的一个热点,也是 Spring 框架中的一个重要内容,是函数式编程的一种衍生范型。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
4、AOP 在Spring中的作用
提供声明式事务;允许用户自定义切面;
- 横切关注点:跨越应用程序多个模块的方法或功能。即是与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志、安全、缓存、事务等等
- 切面(ASPECT):横切关注点 被模块化 的特殊对象,即它是一个类
- 通知(Advice):切面必须要完成的工作,即它是类中的一个方法
- 目标(Target):被通知的对象,即一个接口
- 代理(Proxy):向目标对象应用通知之后创建的对象,即代理类
- 切入点(PointCut):切面通知 执行的"地方"的定义
- 连接点(jointPoint):与切入点匹配的执行点
5、使用 Spring 实现 AOP
方式一:使用Spring的API接口
- 接口
public interface UserService {
void add();
void update();
void delete();
void query();
}
- 实现类
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("add");
}
public void update() {
System.out.println("update");
}
public void delete() {
System.out.println("delete");
}
public void query() {
System.out.println("query");
}
}
- 通知
public class LogBefore implements MethodBeforeAdvice {
/**
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
}
}
public class LogAfter implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了" + method.getName() + "方法,返回结果为" + returnValue);
}
}
- applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userServiceImpl" class="com.study.service.impl.UserServiceImpl"/>
<bean id="logAfter" class="com.study.log.LogAfter"/>
<bean id="logBefore" class="com.study.log.LogBefore"/>
<!-- 方式一:使用原生Spring API接口实现 -->
<!-- 配置aop:需要引入aop的约束 -->
<aop:config>
<!-- 切入点:要执行的位置 -->
<aop:pointcut id="pointCut" expression="execution(* com.study.service.impl.UserServiceImpl.*(..))"/>
<!-- 通知 -->
<aop:advisor advice-ref="logBefore" pointcut-ref="pointCut"/>
<aop:advisor advice-ref="logAfter" pointcut-ref="pointCut"/>
</aop:config>
</beans>
- 测试
public class MyTests {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 动态代理 代理的是 接口(注意这边一定要用接口类型:UserService.class)
UserService userService = context.getBean("userServiceImpl", UserService.class);
/**
* com.study.service.impl.UserServiceImpl的add被执行了
* add
* 执行了add方法,返回结果为null
*/
userService.add();
userService.query();
userService.delete();
userService.update();
}
}
方式二:自定义来实现 AOP
- 接口和实现方法跟方法一一样
- 定义一个切面类
public class DiyPointCut {
public void before(){
System.out.println("方法执行前~~~~~~");
}
public void after(){
System.out.println("方法执行后~~~~~~");
}
}
- applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userServiceImpl" class="com.study.service.impl.UserServiceImpl"/>
<!-- 方式二:自定义实现AOP -->
<bean id="diyPointCut" class="com.study.diy.DiyPointCut"/>
<aop:config>
<!-- 自定义切面-->
<aop:aspect ref="diyPointCut">
<!-- 切入点 -->
<aop:pointcut id="point" expression="execution(* com.study.service.impl.UserServiceImpl.*(..))"/>
<!-- 通知 -->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
- 测试
public class MyTests {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 动态代理 代理的是 接口(注意这边一定要用接口类型)
UserService userService = context.getBean("userServiceImpl", UserService.class);
/**
方法执行前~~~~~~
add
方法执行后~~~~~~
*/
userService.add();
userService.query();
userService.delete();
userService.update();
}
}
方式三:使用注解实现 AOP
- 接口和实现方法跟方法一一样,额外引入一个依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
- 定义一个切面类,并定义切入点和通知
@Aspect
public class AnnotationPointCut {
@Pointcut("execution(* com.study.service.impl.UserServiceImpl.*(..))")
public void pointCut(){}
@Before("pointCut()")
public void before(){
System.out.println("方法执行前~~~~~~注解方式");
}
@After("pointCut()")
public void after(){
System.out.println("方法执行后~~~~~~注解方式");
}
// 在环绕通知中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("pointCut()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前");
// 获得签名 void com.study.service.UserService.add()
System.out.println(proceedingJoinPoint.getSignature());
// 获取方法参数 []
System.out.println(Arrays.toString(proceedingJoinPoint.getArgs()));
// com.study.service.impl.UserServiceImpl@45b9a632
System.out.println(proceedingJoinPoint.getTarget());
// class org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint
System.out.println(proceedingJoinPoint.getClass());
// com.study.service.impl.UserServiceImpl@45b9a632
System.out.println(proceedingJoinPoint.getThis());
// 执行方法
Object proceed = proceedingJoinPoint.proceed();
// null
System.out.println(proceed);
System.out.println("环绕后");
}
}
- applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userServiceImpl" class="com.study.service.impl.UserServiceImpl"/>
<bean id="annotationPointCut" class="com.study.diy.AnnotationPointCut"/>
<!-- 方式三:注解实现AOP,开启注解驱动 JDK(默认proxy-target-class="false") cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
- 测试
public class MyTests {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 动态代理 代理的是 接口(注意这边一定要用接口类型)
UserService userService = context.getBean("userServiceImpl", UserService.class);
/**
环绕前
void com.study.service.UserService.add()
[]
com.study.service.impl.UserServiceImpl@45b9a632
class org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint
com.study.service.impl.UserServiceImpl@45b9a632
方法执行前~~~~~~注解方式
add
方法执行后~~~~~~注解方式
null
环绕后
*/
userService.add();
}
}
补充:execution 表达式
execution(* com.study.service.impl.UserServiceImpl.*(..))
- 第一个 * 表示返回值的类型是任意类型
- com.study.service.impl AOP所切的服务的包名
- UserServiceImpl 表示类名
- 第二个 * 代表这个类下的所有方法
- 后面括号里面代表方法参数是任何参数类型