SpringAOP配置

来源:互联网 发布:淘宝卖家怎么看运费险 编辑:程序博客网 时间:2024/06/03 15:58
<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
    
    <!-- 配置客户的dao -->
    <bean id="customerDao" class="cn.feicui.demo3.CustomerDaoImpl"/>
    
    <!-- 编写切面类配置好 -->
    <bean id="myAspectXml" class="cn.feicui.demo3.MyAspectXml"/>
    
    <!-- 配置AOP -->
    <aop:config>
        <aop:aspect ref="myAspectXml">
            <!-- <aop:before method="log" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/> -->
            
            <!-- 配置最终通知
            <aop:after method="after" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/>
            -->
            
            <!-- <aop:after-returning method="afterReturn" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/> -->
            
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/>
        </aop:aspect>
    </aop:config>
    
</beans>


<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
    
    <!-- 配置客户的dao -->
    <bean id="customerDao" class="cn.feicui.demo3.CustomerDaoImpl"/>
    
    <!-- 编写切面类配置好 -->
    <bean id="myAspectXml" class="cn.feicui.demo3.MyAspectXml"/>
    
    <!-- 配置AOP -->
    <aop:config>
        <aop:aspect ref="myAspectXml">
            <!-- 切入点的表达式
                1. execution()    固定的,不能不写
                2. public 可以省略不写
                3. void,返回值可以出现 * 表示任意的返回值,返回值类型不能不写
                4. 可以使用 * 代替的,不能不编写的,简写方式:*..*方法
                5. *DaoImpl
                6. 方法 save*
                7. 方法的参数:
            -->
            <!-- <aop:before method="log" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/> -->
            <!-- public 可以省略不写 -->
            <!-- <aop:before method="log" pointcut="execution(void cn.feicui.demo3.CustomerDaoImpl.save())"/> -->
            
            <!-- void,返回值可以出现 * 表示任意的返回值,返回值类型不能不写 -->
            <!-- <aop:before method="log" pointcut="execution(* cn.feicui.demo3.CustomerDaoImpl.save())"/> -->
            
            <!-- 包名可以使用 * 代替,不能不写 -->
            <!-- <aop:before method="log" pointcut="execution(* cn.feicui.*.CustomerDaoImpl.save())"/> -->
            
            <!-- 包的简写的方式,任意的包的结构 -->
            <!-- <aop:before method="log" pointcut="execution(* *..*.CustomerDaoImpl.save())"/> -->
            
            <!-- 编写类的写法 -->
            <!-- <aop:before method="log" pointcut="execution(* *..*.*DaoImpl.save())"/> -->
            
            <!-- 方法编写 -->
            <!-- <aop:before method="log" pointcut="execution(* *..*.*DaoImpl.save*())"/> -->
            
            <!-- 参数列表:出现一个*,表示一个参数,任意参数使用 .. -->
            <aop:before method="log" pointcut="execution(* *..*.*DaoImpl.save*(..))"/>
        </aop:aspect>
    </aop:config>
    
</beans>


<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
    
    <!-- 把custom类交给spring管理 -->
    <bean id="customerDao"
       class="cn.feicui.demo3.CustomerDaoImpl"/>
    
    <!-- 编写切面类配置好 -->
    <bean id="myAspectXml"
          class="cn.feicui.demo3.MyAspectXml"/>
    
    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置切面类:切入点 + 通知(类型) -->
        <aop:aspect ref="myAspectXml">
            <!-- 配置的前置通知,save方法执行之前,增强的方法会执行 -->
            <!-- 切入点的表达式:execution(public void cn.feicui.demo3.CustomerDaoImpl.save()) -->
            <!--  <aop:after-returning method="afterReturn" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/>  -->
            
            <aop:around method="around" pointcut="execution(public void cn.feicui.demo3.CustomerDaoImpl.save())"/>
        </aop:aspect>
    </aop:config>
    
</beans>

















原创粉丝点击