Spring AOP 创建Advice 基于Annotation

 

1

2

3

4

5

6

7

8

9

10

11

12

public interface IHello {

  public void  sayHello(String str);

}

public class Hello implements IHello {

 

    @Override

    public void sayHello(String str) {

        // TODO Auto-generated method stub

       System.out.println("你好"+str);

    }

 

}

  

aspectBean.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

 

//引入Aspect注解,声明切面

@Aspect

public class aspectBean {

    //定义为切入点

    @Pointcut("execution(* hello.*(..))")

    public void log() {

         

    }

    @Before(value="log()"//在切入点之前执行

    public void startLog()

    {

        System.out.println("开始记录");

    }

    @After(value="log()"//在切入点之后执行

    public void endLog()

    {

        System.out.println("结束记录");

    }

}

  applicationContext.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?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"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

     

    <!-- 启动aspectj -->

    <aop:aspectj-autoproxy/>

    <bean id="apbean" class="com.pb.aspectBean"></bean>

    <bean id="he" class="com.pb.Hello"></bean>

</beans>

  Maintest文件:

1

2

3

4

5

6

7

public static void main(String[] args) {

        // TODO Auto-generated method stub

        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

         

        IHello hello=(IHello)context.getBean("he");

        hello.sayHello("访客");

    }

  执行:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些 Spring AOP 常见问题: 1. 什么是 Spring AOPSpring AOPSpring 框架提供的一种基于代理的 AOP(面向切面编程)实现方式。它允许你通过配置来定义横切关注点,并在执行方法时自动将这些关注点织入到目标对象中。 2. Spring AOP 支持哪些 AOP 类型? Spring AOP 支持五种 AOP 类型:前置通知(Before Advice)、后置通知(After Advice)、环绕通知(Around Advice)、异常通知(After Throwing Advice)和最终通知(After Finally Advice)。 3. Spring AOP 和 AspectJ 有什么区别? Spring AOP 是基于代理的 AOP 实现方式,而 AspectJ 是基于编译时或运行时字节码操纵的 AOP 实现方式。Spring AOP 的功能相对比较简单,适用于大多数场景,而 AspectJ 更加强大,但也更加复杂。 4. Spring AOP 的代理是如何实现的? Spring AOP 采用了 JDK 动态代理和 CGLIB 代理两种方式实现代理。当目标对象实现了接口时,Spring AOP 会使用 JDK 动态代理,否则会使用 CGLIB 代理。 5. Spring AOP 的切入点(Pointcut)有哪些表达式? Spring AOP 的切入点表达式支持通配符和正则表达式,包括以下几种: - execution:匹配方法执行的连接点。 - within:匹配指定类型内的方法执行。 - this:匹配当前代理对象类型的执行方法。 - target:匹配当前目标对象类型的执行方法。 - args:匹配参数类型匹配的执行方法。 - @annotation:匹配特定注解标注的方法执行。 以上是一些 Spring AOP 常见问题,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值