• Spring AOP Schema aop:config、tx:advice


    Spring AOP Schema  aop:config、tx:advice

    一、      利用aop:config标签实现AOP

    首先看个例子,如下

    接口代码:

    package com.lei.demo.aop.schema;
    
    public interface IHello {
        public void sayHello();
    }

    接口实现:

    package com.lei.demo.aop.schema;
    
    public class HelloService implements IHello {
    
        public void sayHello() {
            System.out.println("-----Hello World!-----");
        }
    
    }

    接下来我们要实现AOP,即调用sayHello方法时切入通知。

    1.      第一种方法aop:config中配置aop:pointcut和aop:aspect

    定义一个切面支持类HelloAspect.java

    package com.lei.demo.aop.schema;
    
    /*
     * 切面支持类
     */
    public class HelloAspect {
    
        //前置通知  
        public void beforeAdvice() {  
            System.out.println("===========before advice");  
        }
        
        //后置最终通知  
        public void afterFinallyAdvice() {  
            System.out.println("===========after finally advice");
        }
    }

     

    Xml配置:Spring-AOP-Schema.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  
               http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
               http://www.springframework.org/schema/aop  
               http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
               
    
        <!-- 目标类 -->
        <bean id="helloService" class="com.lei.demo.aop.schema.HelloService" />
        
        <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" />
        
        
        <!-- 配置切面 -->
        <!-- aop:advisor,是有顺序的,必须放在aop:pointcut之后 -->
        <aop:config>
            <aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
            
            <aop:aspect ref="helloAspect">
                <!—以下使用了两种方法定义切入点  pointcut-ref和pointcut-->
                <aop:before pointcut-ref="helloPointcut" method="beforeAdvice" />
                <aop:after pointcut="execution(* com.lei.demo.aop.schema..*.*(..))" method="afterFinallyAdvice" />
            </aop:aspect>
            
            
        </aop:config>
    </beans>

    以上配置中method="beforeAdvice"method="afterFinallyAdvice"对应helloAspect中的方法

    测试,App.java

    package com.lei.demo.aop.schema;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
    
        private static ApplicationContext ctx;
    
        public static void main(String[] args) {
            
            ctx = new ClassPathXmlApplicationContext("Spring-AOP-Schema.xml");
            
            IHello helloService = ctx.getBean("helloService",IHello.class);
            
            helloService.sayHello();
        }
    
    }

    运行App.java,结果如下:

    ===========before advice

    -----Hello World!-----

    ===========after finally advice

    2.      第二种方法aop:config中配置aop:pointcut和aop:advisor

    实现org.aopalliance.intercept.MethodInvocation接口,

    HelloAroundAdvice.java如下:

    package com.lei.demo.aop.schema;
    
    import java.lang.reflect.Method;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class HelloAroundAdvice implements MethodInterceptor {
    
        public Object invoke(MethodInvocation arg0) throws Throwable {
    
            System.out.println("++++++before advice");
            arg0.proceed();
            System.out.println("++++++after advice");
            
            return null;
        }
    
    }

     

    Xml配置:Spring-AOP-Schema.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  
               http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
               http://www.springframework.org/schema/aop  
               http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
               
        <!-- 目标类 -->
        <bean id="helloService" class="com.lei.demo.aop.schema.HelloService" />
        
        <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" />
        
        <bean id="helloArroundAdvice" class="com.lei.demo.aop.schema.HelloAroundAdvice" />
        
        <!-- 配置切面 -->
        <aop:config>
            <aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
            <aop:advisor advice-ref="helloArroundAdvice" pointcut-ref="helloPointcut"/>
            
        </aop:config>
    </beans>

    仍然运行App.java,结果如下:

    ++++++before advice

    -----Hello World!-----

    ++++++after advice

    二、      AOP中加入事务,引入tx:advice

    集成Hibernate时,需要引入事务,以下配置引用Spring 4.0.3和Hibernate 4.3.5

    Spring-hibernate.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            ">
    
        <!-- Hibernate4 -->
        <!-- 加载资源文件  其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
        <context:property-placeholder location="classpath:persistence-mysql.properties" />
        
        <bean id="sessionFactory" 
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan">
                <list>
                    <!-- 可以加多个包 -->
                    <value>com.lei.demo.entity</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                </props>
            </property>
        </bean>
        
        <!-- 数据库映射 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="${jdbc.driverClassName}" />
          <property name="url" value="${jdbc.url}" />
          <property name="username" value="${jdbc.user}" />
          <property name="password" value="${jdbc.pass}" />
       </bean>
       
        <!-- 配置Hibernate事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory" />
       </bean>
       
       <!-- 配置事务异常封装 -->
       <bean id="persistenceExceptionTranslationPostProcessor" 
           class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
       
       <!--  声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="get*" propagation="REQUIRED" />
                <tx:method name="*" read-only="true" />
            </tx:attributes>
        </tx:advice>
        
        <aop:config expose-proxy="true">
            <!-- 只对业务逻辑层实施事务 -->
            <aop:pointcut id="txPointcut" expression="execution(* com.lei.demo.service..*.*(..))" />
            <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
            <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
            
        </aop:config>
        
    </beans>

    重点看一下tx:advice的配置

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    意思是说,这个事务advice的管理者为transactionManager,而这个advice中规定了匹配add*get*方法的传输方式为required,也就是说没有sessionFactory的时候会自动建立,有的时候就不会建立了。当然这只是规定了一个advice,你还需要指定一个aop:pointcut去引用它,例如下边的aop:config配置节。这样,这个advice就被织入到txPointcut这个pointcut上了。

  • 相关阅读:
    前端 HTML CSS
    前端部分1:HTML
    异常处理专区:
    IO模型介绍
    协程专区
    线程专区
    操作系统简介专区
    进程专区
    正则表达式专区:
    题解 P2158 【[SDOI2008]仪仗队】
  • 原文地址:https://www.cnblogs.com/leiOOlei/p/3709607.html
Copyright © 2020-2023  润新知