zoukankan      html  css  js  c++  java
  • Spring-day2-AOP(面向切面编程)

    动态代理

    • 特点

      字节码随用随创建,随用随加载

    • 作用

      不用修改源码对方法增强

    • 分类

      基于接口的动态代理

      基于子类的动态代理

    • 创建

      使用Proxy类中的newProxyInstance方法

    • 要求

      被代理类最少实现一个接口,没有则不能使用

    • newProxyInstance方法参数

      • classLoader:类加载器

        用于加载代理对象字节码的,和被代理对象使用相同的类加载器

      • class[ ]:字节码数组

        用于让代理对象和被代理对象有相同方法,固定写法。

      • InvocationHandler:用于提供增强的代码

        是让我们写如何代理。一般都是写一个该接口的实现类,通常情况下都是匿名内部类,不是必须的

        此接口的实现类都是谁用谁写

      IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),
      	producer.getClass().getInterfaces(),
      	new InvocationHandler(){
      	  作用:执行被代理对象的任何接口方法都会经过该方法
      	  * proxy 代理对象的引用
      	  * method 当前执行的方法
      	  * args 执行当前方法所需的参数
      	  * return 和被代理对象有相同的返回值
      		@override
      		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
      			// 提供增强的代码
      			Object returnValue = null
      			1. 获取方法执行的参数
      			Float money = (Float)args[0]
      			2. 判断当前方法是否为指定方法
      			if("saleProduct".equals(method.getName())){
      				returnValue = method.invoke(producer,money*0.8)
      			}
      			return returnValue;
      		}
      	}
      )
      //代理方法调用的是上面invoke中的方法
      proxyProducer.saleProduct(100000)
      
      • 注意 如果代理的类没有接口,则代理不可用。

    AOPxml配置

    • 连接点Joinpoint:指那些被拦截的点,在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。

    • 切入点Pointcut:所谓切入点指的是要对哪些Joinpoint进行拦截的定义。方法会被增强。

      所有的切入点都是连接点,但不是所有的连接点都是切入点。

    • 通知Advice:指拦截到Joinpoint之后所要做的事情

      在invoke方法里的,有前置通知,后置通知,异常通知,最终通知

    • 引入Introduction

    • 目标对象Target :即被代理的对象

    • 织入Weaving:把增强应用到目标对象来创建新的代理对象的过程。Spring采用动态代理织入。

    1. 创建接口类,实现类

    2. 创建aop通知功能函数

    3. 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">
      
              <!--配置spring的IOC,把service对象配置进来-->
              <bean id="accountService" class="hjj.web.service.impl.AccountServiceImpl"></bean>
      
              <!--spring中基于xml的aop配置步骤
                  1. 把通知bean也交给spring来管理
                  2. 使用aop:config标签表明aop的配置
                  3. 使用aop:aspect标签表明配置切面
                      id:给切面提供一个唯一表示
                      ref:指定通知类bean的id
                  4. 在aop:aspect标签的内部使用对应的标签来配置通知的类型
                      现在让pringLog方法在切入点方法执行前执行
                      aop:before表示配置前置通知
                          method:用于指定Logger类中哪个方法是前置通知
                          point属性:用于指定切入点表达式,该表达式指的是对业务层中哪些方法增强
                          切入点表达式:
                              关键字:execution(表达式)
                              访问修饰符 返回值 包名.类名.方法名(参数列表)
                              全通配写法:* *..*.*(..)
                                  访问修饰符可以省略 *可以代表任何返回值 *.*.*可以表示包的关系 *..表示中间任意包 *.* 表示类名和方法
                                  (..)表示任意参数或者可以写返回值类型 int, java.lang.String
      
                              实际开发写法:切到业务层实现类下的所有方法 * 业务层包.*.*(..)
              -->
      
              <!--配置logger类-->
              <bean id="logger" class="hjj.web.utils.Logger"></bean>
      
              <!--配置AOP-->
              <aop:config>
                  <!--配置切面-->
                  <aop:aspect id="logAdvice" ref="logger">
                      <!--配置通知类型,并且建立通知方法和切入点方法的关联-->
                      <aop:before method="printLog" pointcut="execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
                  </aop:aspect>
              </aop:config>
              
              // 通知类型
                          <aop:aspect id="logAdvice" ref="logger">
                      <!--配置通知类型,并且建立通知方法和切入点方法的关联-->
      <!--                <aop:before method="printLog" pointcut="execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:before>-->
                      <aop:before method="beforePrintLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
                      <aop:after-returning method="afterPrintLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:after-returning>
                      <aop:after-throwing method="afterThrowingPringLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:after-throwing>
                      <aop:after method="finalPrintLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:after>
                  </aop:aspect>
          
      </beans>
      
      <!-- 配置切入点表达式,ID属性用于指定表达式的唯一标识,expression属性用于指定表达式内容,此标签也可以放在aspect外面-->
                  <aop:pointcut id="pt1" expression="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"/>
                  
      <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
      
      

    AOPxml注解

    aop注解配置

    /**
     * 记录日志的工具类,提供了公共的代码
     */
    @Component("logger")
    @Aspect // 表示当前类是一个切面
    public class Logger {
    
    		@Pointcut("execution()")
    		private void pt1(){}
        /**
         * 用于打印日志:计划在其切入点方法执行前执行(切入点方法就是业务层方法)
         */
        @Before(pt1())
        public void beforePrintLog() {
            System.out.println("前置");
        }
    
        public void afterPrintLog() {
            System.out.println("后置");
        }
    
        public void afterThrowingPringLog() {
            System.out.println("异常");
        }
    
        public void finalPrintLog() {
            System.out.println("最终");
        }
    
        // 环绕通知为我们提供了ProceedingJoinPoint,有一个方法proceed(),此方法就明确了调用切入点方法
        // 为我们提供了一种可以在代码中手动控制增强方法合适执行的方式
        public Object aroundPrintLog(ProceedingJoinPoint pjp) {
            Object returnValue = null;
            try {
                Object[] args = pjp.getArgs(); // 得到方法执行所需参数
    
                System.out.println("前置");
    
                returnValue = pjp.proceed(args); // 明确调用业务层的方法
    
                System.out.println("后置");
    
            } catch (Throwable throwable) {
    //            throwable.printStackTrace();
                System.out.println("异常");
            } finally {
                System.out.println("最终");
            }
            return returnValue;
    
    //        System.out.println("环绕通知");
        }
    }
    
    xml:
    配置spring创建容器要扫描的包
    <context:component-scan base-package="包路径"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
    • 注意 如果用注解自带的调用顺序会出现问题,用环绕通知顺序正常

    事务控制

    1. 导包

      <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>5.2.4.RELEASE</version>
      </dependency>
      
      

      事务管理器:org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager

    2. 在bean.xml中配置

    1. 配置事物管理器
    <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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            https://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager">
    	<property name="dataSource" ref="dataSource">
    <bean>
    
    2.配置事物的通知
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	5.配置事物的属性
    	<tx:attributes>
    		<tx:method name="*" propagation="required" read-only='false'/>
    		<tx:method name="find*" propagation="support" read-only='true'/>
    		
    		isolation:指定事物的隔离级别,默认值是default,表示使用数据库的默认隔离级别
    		propagation:用于指定事物的传播行为,默认是REQUIRED,表示一定会有事物,增删改的选择,查询可以使用support
    		read-only:用于指定事物是否只读,查询才设置为true
    		timeout:用于指定事物的超市时间,默认值是-1,表示不超时,如果指定了数值,以秒为单位
    		rollback-for:用于指定一个异常,当产生该异常时事物回滚,产生其他异常时,事物不回滚。没有默认值,表示任何异常都回滚
    		no-rollback-for:用于指定一个异常,当产生该异常,事务不会回滚,产生其他异常,事务回滚。没有默认值,表示任何异常都回滚。
    		
    	</tx:attributes>
    </tx:advice>
    
    3.配置aop切入点表达式
    <aop:config>
    	<aop:pointcut id="pt1" expression="execute(* 包.包.*.*(..))">
    	4. 建立切入点表达式喝事物通知的对应关系
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">
    </aop>
    
    <beans>
    
    1. 基于注解的事务控制

      1. 配置事物管理器
      <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"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/tx
              https://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/aop
              https://www.springframework.org/schema/aop/spring-aop.xsd"
              http://www.springframework.org/schema/context
              https://www.springframework.org/schema/context/spring-context.xsd">
      
      3. 配置spring创建容器时要扫描的包
      <context:component-scan base-package="包的地址">
      
      4. 开启spring对注解事物的支持
      <tx:annotation-driven transaction-manager="transactionManager>"
      
      6. 在需要事物支持的地方使用注解@Transactional
      
      2.在实现类中
      @Service(accountService)
      @Transactional
      public class 实现类 implements 接口类{
      	@Autowired
      	// 在持久层也要配置
      	private IaccountDao accountDao
      }
      
      • 基于注解的配置类

        1.创建一个配置总配置类
        @Configuration
        // 用于配置需要扫描的包
        @ComponentScan("hjj.web")
        @Import({HibernateConfig.class, TransactionConfig.class})
        @PropertySource("hibernateConfig.properties")
        @EnableTransactionManagement //开启注解的支持
        public class SpringConfiguration{
        	
        }
        
        2.另一个java类,连接数据库相关的类
        publci class HibernateConfig{
        
        	@Value("${hibernate.username}")
        	private String username;
        	@Value("${hibernate.password}")
        	private String password
        
        	// 注入进容器
        	@Bean(name="HibernateTemplate")
        	public Hibernate crateHibernateTemplate(DataSource datasource){
        		return new HibernateTemplate(dataSource)
        	}
        	
        	@Bean(name="dataSource")
        	public DataSource crateDataSource(){
        		配置数据库的用户名密码 创建数据源对象
        	}
        }
        
        3. 新建一个properties,配置文件类
        hibernate.username = 
        hibernate.password = 
        
        4. 创建和事物相关的配置类
        public class TransactionConfig {
        	创建事务管理器对象
        	@Bean(name="transactionManager")
        	public PlatformTransactionManager createTransactionManager(DataSource dataSource){
        		return new DataSourceTransactionManager(dataSource)
        	}
        }
        
        5. main方法所在的类
        @ContextConfiguration(classes=SpringConfiguration.class)
        public class test{
        	psvm{
        		业务逻辑
        	}
        }
        
  • 相关阅读:
    Redis --> Redis架构设计
    Redis --> 为redis分配新的端口
    大数据 --> CAP原理和最终一致性
    大数据 --> 一致性Hash算法
    大数据 --> 分布式服务框架Zookeeper
    网络通信 --> Linux 五种IO模型
    网络通信 --> 同步、异步、阻塞与非阻塞介绍
    网络通信 --> Socket、TCP/IP、HTTP、FTP及网络编程
    SoC总线专题
    RISC-V评估系列
  • 原文地址:https://www.cnblogs.com/jimmyhe/p/12592213.html
Copyright © 2011-2022 走看看