随笔 - 35  文章 - 0  评论 - 0  阅读 - 9054

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

 

 

 

posted on   ziwang520  阅读(146)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示