Spring--代理模式---AOP(执行方式的全部代码)

在了解AOP之前,需要了解代理模式

静态代理

角色分析:

  • 抽象角色:一般会使用接口或者抽象类来解决
//租房
public interface Rent {
    public void rent();
}

  • 真实角色:被代理的角色
//房东
public class Host implements Rent{
    @Override
    public void rent() {

    }
}

  • 代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Proxy {
    private Host host;
    public void rent(){
        seeHourse();
        host.rent();
        fare();
    }

    //看房
    public void seeHourse(){
        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();

    }
}

好处:

  • 可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
  • 公共也就交给代理角色!实现了业务的分工!
  • 公共业务发生扩展的时候,方便集中管理!

缺点

  • 一个真实角色就会产生一个代理角色;代码量会翻倍~开发效率会变低

一般我们的系统都是纵向开发的,Aop式横向切入开发
说白了就是:有些功能我已经开发完成了,想要在此基础上再加些东西,那么,aop可以让我们不需要改动之前的代码及逻辑,切一刀,想要加在此基础前还是后都可以

动态代理

  • 动态代理和静态代理角色一样
  • 动态代理的代理类是动态生成的,不是我们直接写好的!
  • 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
       - 基于接口—JDK动态代理【我们在这里使用】
       - 基于类:cglib
       - Java字节码实现:javasist

需要了解两个类:Proxy代理,Invocationhandler嗲用处理程序

动态代理类的好处:

  • 可以使真实角色的操作更加纯粹!不用区关注一些公共的业务
  • 公共也就交给代理角色!实现了业务的分工!
  • 公共业务发生扩展的时候,方便集中管理!
  • 一个动态代理类代理的是一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要实现类同一个接口即可

SpringAOP种,通过Advice定义横切逻辑,Spring中支持5种类型的Advice

  • 前置类型 方法前 org.springframework.aop.MethodBeforeAdvice
  • 后置通知 方法后 org.springframework.aop.AfterReturningAdvice
  • 环绕通知 方法前后 org.aopalliance.intercept.MethodInterceptor
  • 异常抛出通知 方法抛出异常 org.springframework.aop.ThrowsAdvice
  • 引介通知 类中增加新的方法属性 org.springframework.aop.IntroductionInterceptor

即Aop不变的情况下,去增加新的功能,所谓前置,即在执行该方法前使用,其他亦如此

执行方式之一:

增加新的依赖

   <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.7version>
        dependency>
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}

public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户");
    }
}
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"返回结果为:"+returnValue);
    }
}

public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");

    }
}

applicationContext.xml

    
<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 id="userService" class="aopSpring.demo04.service.UserServiceImpl"/>

    <bean id="log" class="aopSpring.demo04.log.Log"/>

    <bean id="afterLog" class="aopSpring.demo04.log.AfterLog"/>

    
    <aop:config>
        
        <aop:pointcut id="pointcut" expression="execution(* aopSpring.demo04.service.UserServiceImpl.*(..))"/>
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    aop:config>
beans>
public class SpringAopTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = context.getBean("userService",UserService.class);
        userService.add();
    }
}

excution表达式解释参考

执行结果

aopSpring.demo04.service.UserServiceImpl的add被执行了
增加了一个用户
执行了add返回结果为:null

执行方式二:自定义

public class DiyPointCut {

    public void before(){
        System.out.println("方法执行前");
    }

    public void after(){
        System.out.println("方法执行后");
    }

}

配置文件种需要改变的匹配制

    
    <bean id="diy" class="aopSpring.demo04.diy.DiyPointCut"/>
    
    <aop:config>
        
        <aop:aspect ref="diy">
            
            <aop:pointcut id="point" expression="execution(* aopSpring.demo04.service.UserServiceImpl.*(..)) )"/>
            
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after"   pointcut-ref="point"/>
        aop:aspect>
    aop:config>

测试类也不变

执行结果

方法执行前
增加了一个用户
方法执行后

此方法直接使用aop配置进行

方式三:使用注解实现AOP

//    使用注解的方式实现AOP
//
//    标注这个类是一个切面
 @Aspect
public class AnnotationPointCut {
       @Before("execution(* aopSpring.demo04.service.UserServiceImpl.*(..))")
     public void before(){
         System.out.println("注解:方法执行之前");
     }
    @After("execution(* aopSpring.demo04.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("注解:方法执行之后");
    }

    // 在环绕增强中,我们可以给定一个参数,代表我们要获取的处理切入的点
    @Around("execution(* aopSpring.demo04.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前:" );

        Signature signature = jp.getSignature();//获得签名
        //执行方法
        Object proceed = jp.proceed();

        System.out.println("环绕后");
    }
}

配置文件中:


    
    <bean id="annotationPointCut" class="aopSpring.demo04.diy.AnnotationPointCut"/>
    
    <aop:aspectj-autoproxy/>

执行结果

环绕前:
注解:方法执行之前
增加了一个用户
注解:方法执行之后
环绕后

你可能感兴趣的:(Java,大话设计模式,代理模式,spring,java)