• spring aop切面切点


    aop开发

    1导入坐标

    <!--导入spring的context坐标,context依赖aop--> 
    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version>
    </dependency>
    <!-- aspectj的织入 -->
     <dependency> 
    <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId>
     <version>1.8.13</version>
    </dependency>

    2创建目标接口和目标类 就是内部有切点(需要被增强的方法的类)

    package com.jc;
    
    import com.jc.impl.TargetInterface;
    
    public class Target implements TargetInterface {
    //    method(切点)
        @Override
        public void method() {
            System.out.println("target is runing。。。");
        }
    }

    3创建切面类 (写增强方法的类)

    package com.jc;
    //切面类
    public class MyAspect {
    
    //    这里面写增强方法
    
        public void before(){
            System.out.println("before is runing...");
        }
    
        public void after(){
            System.out.println("before is runing...");
        }
    
    
    }

    4springapplication 配置

    <?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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <bean id="target" class="com.jc.Target"></bean>
        <bean id="myAspect" class="com.jc.MyAspect"></bean>
        <aop:config>
    <!--        增强方法类 切面对象-->
            <aop:aspect ref="myAspect">
    <!--            配置增强方法以及需要增强方法的类  -->
                <aop:before method="before" pointcut="execution(public void com.jc.Target.method())"/>
            </aop:aspect>
        </aop:config>
        <context:component-scan base-package="com.jc"></context:component-scan>
    </beans>

    5测试

    import com.jc.Target;
    import com.jc.impl.TargetInterface;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
    public class test { @Autowired
    private TargetInterface target;
        @Test
        public void test1(){
            target.method();
        } }

    扩展

     扩展2

     

  • 相关阅读:
    rman备份,恢复
    异步事件回调机制原理探索 (转)
    stock
    将知识变成你的技能点
    Tomcat的URL中文乱码解决以及传输优化
    李洪强iOS开发之-入门指南
    WebSocket 和 Socket 的区别
    李洪强iOS开发之-修改状态栏的字体的颜色
    关于UDID和UUID的区别
    李洪强iOS开发之
  • 原文地址:https://www.cnblogs.com/ziwang520/p/15835316.html
Copyright © 2020-2023  润新知