文章目录
1.代理模式
1.1什么是代理模式
在代理模式(Proxy Pattern)中,一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。
在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口。
主要解决:在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上。在面向对象系统中,有些对象由于某些原因(比如对象创建开销很大,或者某些操作需要安全控制,或者需要进程外的访问),直接访问会给使用者或者系统结构带来很多麻烦,我们可以在访问此对象时加上
优点:
- 1、职责清晰。
- 2、高扩展性。
- 3、智能化。
缺点:
- 1、由于在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请求的处理速度变慢。
- 2、实现代理模式需要额外的工作,有些代理模式的实现非常复杂。
1.2静态代理
创建一个接口
//定义接口 增删改查方法
public interface UserService {
public void insert();
public void delete();
public void update();
public void select();
}
创建实现接口的实现类
public class UserServiceImpl implements UserService {
public void insert() {
System.out.println("实现了增加的功能!");
}
public void delete() {
System.out.println("实现了删除的功能!");
}
public void update() {
System.out.println("实现了修改的功能!");
}
public void select() {
System.out.println("实现了查询的功能!");
}
}
代理类
public class UserServiceProxy implements UserService {
//如果我想要实现扩展的业务功能
private UserServiceImpl userServiceImpl;
public UserServiceProxy() {
}
public void setUserServiceImpl(UserServiceImpl userServiceImpl) {
this.userServiceImpl = userServiceImpl;
}
public void insert() {
log("insert");
System.out.println("实现了增加的功能!");
}
public void delete() {
log("delete");
System.out.println("实现了删除的功能!");
}
public void update() {
log("update");
System.out.println("实现了修改的功能!");
}
public void select() {
log("select");
System.out.println("实现了查询的功能!");
}
//日志方法
public void log(String msg){
System.out.println("[Debug] 使用了"+msg+"方法");
}
}
测试
public class Client {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
UserServiceProxy userProxy = new UserServiceProxy();
userProxy.setUserServiceImpl(userService);
userProxy.insert();
userProxy.delete();
userProxy.update();
userProxy.select();
}
1.3动态代理
创建一个接口
//定义接口 增删改查方法
public interface UserService {
public void insert();
public void delete();
public void update();
public void select();
}
创建实现接口的实现类
public class UserServiceImpl implements UserService {
public void insert() {
System.out.println("实现了增加的功能!");
}
public void delete() {
System.out.println("实现了删除的功能!");
}
public void update() {
System.out.println("实现了修改的功能!");
}
public void select() {
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 {
log(method.getName());
Object result = method.invoke(target, args);
return result;
}
public void log(String msg){
System.out.println("[Debug] 使用了"+msg+"方法");
}
}
测试
public class Client {
public static void main(String[] args) {
//真实角色
UserServiceImpl userService = new UserServiceImpl();
//代理角色
ProxyInvocationHandler pih = new ProxyInvocationHandler();
//设置代理的对象
pih.setTarget(userService);
//动态生成代理类
UserService proxy = (UserService) pih.getProxy();
proxy.select();
}
}
结果
2.面向切面(AOP)
2.1什么是AOP
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
2.2Aop在Spring中的作用
提供声明式事务;允许用户自定义切面
- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
- 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
- 目标(Target):被通知对象。
- 代理(Proxy):向目标对象应用通知之后创建的对象。
- 切入点(PointCut):切面通知 执行的 “地点”的定义。
- 连接点(JointPoint):与切入点匹配的执行点
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
即 Aop 在 不改变原有代码的情况下 , 去增加新的功能 。
2.3Spring实现Aop
使用Aop需要导入依赖
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
2.3.1使用Spring的API 接口
创建接口
public interface UserService {
public void insert();
public void delete();
public void update();
public void select();
}
创建实现接口的实现类
public class UserServiceImpl implements UserService {
public void insert() {
System.out.println("实现了增加的功能!");
}
public void delete() {
System.out.println("实现了删除的功能!");
}
public void update() {
System.out.println("实现了修改的功能!");
}
public void select() {
System.out.println("实现了查询的功能!");
}
}
配置文件
<?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="userService" class="com.li.service.UserServiceImpl"/>
<bean id="log" class="com.li.log.Log"/>
<bean id="afterLog" class="com.li.log.AfterLog"/>
<!-- 配置aop:需要导入aop的约束-->
<aop:config>
<!-- 切入点:expression 表达式,execution(要执行的位置! * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.li.service.UserServiceImpl.*(..))"/>
<!-- 执行环绕增加! -->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.insert();
}
}
2.3.2自定义来实现AOP 【主要是切面定义】
接口与接口实现类一样.
前置、后置类
public class DiyPointCut {
public void before(){
System.out.println("===========方法执行前===========");
}
public void after(){
System.out.println("===========方法执行后===========");
}
}
<!-- 注册bean -->
<bean id="userService" class="com.li.service.UserServiceImpl"/>
<bean id="log" class="com.li.log.Log"/>
<bean id="afterLog" class="com.li.log.AfterLog"/>
方式二:自定义类
<bean id="diy" class="com.li.diy.DiyPointCut"/>
<aop:config>
<!-- 自定义切面 ,ref 要引用的类-->
<aop:aspect ref="diy">
<!-- 切入点 -->
<aop:pointcut id="point" expression="execution(* com.li.service.UserServiceImpl.*(..))"/>
<!-- 通知 -->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
2.3.3使用注解实现
配置文件
<!-- 方式三-->
<bean id="annotationPointCut" class="com.li.diy.AnnotationPointCut"/>
<!-- 开启注解支持 -->
<aop:aspectj-autoproxy/>
<!-- 注册bean -->
<bean id="userService" class="com.li.service.UserServiceImpl"/>
<bean id="log" class="com.li.log.Log"/>
<bean id="afterLog" class="com.li.log.AfterLog"/>
切面类
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.li.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("====方法执行前====");
}
@After("execution(* com.li.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行后====");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.li.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Object proceed = jp.proceed();//执行方法
System.out.println("环绕后");
}