Spring复习:AOP(二)HelloWorld

Spring复习:AOP(二)HelloWorld

基本步骤

1.新建maven项目
2.加入依赖
    1)spring依赖
    2)AspectJ依赖
3.创建目标类:接口和它的实现类
4.创建切面类
    1)在类的上面加入@Aspect
    2)在类中定义方法,方法就是切面要执行的功能代码
    3)在方法上加入Advice注解
    4)切入点表达式
5.创建Spring的配置文件,声明对象,把对象交给容器同一管理
    可以使用注解或者<bean>标签
    1)目标对象
    2)切面类对象
    3)声明AspectJ框架中的自动代理生成器标签
        用来完成蒂埃里对象的自动生成
6.创建测试类,从spring容器中获取目标对象(实际是代理对象)。
    通过代理执行方法,实现aop的功能增强。

新建Maven项目

加入依赖

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--Spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

    <!--AspectJ依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

创建目标类(接口和实现类)

在这里插入图片描述

public class SomeServiceImpl implements SomeService {

    @Override
    public void doSome(String message) {
        System.out.println("doSome message:"+message);
    }

}

创建切面类

在这里插入图片描述

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyAspect {
    @Before(value = "execution(public void com.suffle.service.impl.SomeServiceImpl.doSome(String))")
    public void myBefore(){
        System.out.println("@Before 当前时间:XXXX年XX月XX日 XX时XX分");
    }
}
@Aspect
    是AspectJ框架的注解,表示当前类是切面类
@Before
    前置通知(Advice)
    属性value:切入点表达式
    定义的方法必需是public void修饰的

Spring配置文件

<?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
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="someService" class="com.suffle.service.impl.SomeServiceImpl"/>

    <bean id="myAspect" class="com.suffle.aspect.MyAspect"/>

    <aop:aspectj-autoproxy/>

</beans>
 <aop:aspectj-autoproxy/>
    1.通过扫描找到@Aspect定义的切面类
    2.根据切入点表达式找到需要增强的切入点

测试

public class AspectTest {
    @Test
    public void testAspect01(){
        String configPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configPath);
        SomeService proxy = (SomeService)applicationContext.getBean("someService");
        proxy.doSome("Hello,World!");
    }
}

结果

在这里插入图片描述

JoinPoint参数

切入点方法,可以包含一个JoinPoint类型参数

通过该参数,可获取切入点表达式、方法签名、目标对象等。

所有的通知方法均可以包含该参数。

    @Before(value = "execution(public void com.suffle.service.impl.SomeServiceImpl.doSome(String))")
    public void myBefore(JoinPoint jp){
        //JoinPoint能够获取到 方法的定义,方法的参数等信息
        System.out.println("连接点的方法定义:"+jp.getSignature());
        System.out.println("连接点方法的参数个数:"+jp.getArgs().length);
        //方法参数信息
        Object[] args = jp.getArgs();
        for(Object arg:args){
            System.out.println(arg);
        }
        System.out.println("@Before 当前时间:XXXX年XX月XX日 XX时XX分");
    }

测试结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值