Spring 基于注解配置的AOP框架详细讲解

作用:Spring 基于XML配置的AOP框架详细讲解_代码人生的博客-CSDN博客

可以看小编基于XML配置的AOP框架详细讲解

下面说的是具体注解的使用,方便简单,内容很少!

最重要的在xml文件中写入

<!-- 配置 Spring 开启注解 AOP 的支持 -->
< aop:aspectj-autoproxy ></ aop:aspectj-autoproxy >

1.配置环境(注解配置可以省略本步骤但是要加上下面这三行代码

@Configuration
@ComponentScan(basePackages="com.bookmanagesystem")

类似于xml配置里面的

<context:component-scan base-package="com.bookmanagesystem">

</context:component-scan>
@EnableAspectJAutoProxy!)

外加一个SpringConfiguration配置类

最后在main当中把获取核心容器对象 当中

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

改为

ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration. class );
@Configuration
@ComponentScan(basePackages="com.bookmanagesystem")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
<?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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.bookmanagesystem"></context:component-scan>
<!-- 配置Spring开启注解AOP的支持 --> 
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

2.注解的main函数和xml配置的区别

  public static void main(String[] args) {
        // 1. 获取核心容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 2. 根据id获取bean对象
        IUserService service = (IUserService) context.getBean("userServiceImpl");
        service.update();

        // 3.关闭容器(如果不记得关闭容器,最典型的问题就是数据库连接不能释放)
        ((ClassPathXmlApplicationContext) context).close();
    }

3.注解的MyLogger织入文件和xml配置区别

@Component//表明bean对象进入容器
@Aspect//表明是一个切面,作用和<aop:aspect></aop:aspect>一样
@Configuration
@ComponentScan(basePackages="com.bookmanagesystem")
@EnableAspectJAutoProxy
public class MyLogger {
        // 注解配置切入点表达式
	@Pointcut("execution(* com.bookmanagesystem.service.impl.*.*(..))")
	private void pc1() {
		
	}
	// 前置通知
        // 这里的value要填写绑定到通知的切入点表达式,所以这里要填上切入点表达式。
        // 注意这里面的切入点表达式引用时必须带上小括号,如果不带会导致解析失败
	@Before("pc1() ")
	public void beforePrintLog() {
		System.out.println("前置通知MyLogger类中的beforePrintLog方法开始记录日志了。。。");
	}

	// 后置通知
	@AfterReturning("pc1()")
	public void afterReturningPrintLog() {
		System.out.println("后置通知MyLogger类中的afterReturningPrintLog方法开始记录日志了。。。");
	}

	// 异常通知
	@AfterThrowing("pc1()")
	public void afterThrowingPrintLog() {
		System.out.println("异常通知MyLogger类中的afterThrowingPrintLog方法开始记录日志了。。。");
	}

	// 最终通知
	@After("pc1()")
	public void afterPrintLog() {
		System.out.println("最终通知MyLogger类中的afterPrintLog方法开始记录日志了。。。");
	}
	//环绕通知
	@Around("pc1()")
	public Object aroundPrintLog(ProceedingJoinPoint pjp) {
	Object result = null;
	try {
	Object[] args = pjp.getArgs(); // 得到方法执行所需的参数
	System.out.println("记录日志前置");
	result = pjp.proceed(args); // 明确调用业务层方法(切入点方法)
	System.out.println("记录日志后置");
	return result;
	} catch (Throwable e) { // proceed方法抛出了Throwable,这里用Exception拦不住
	System.out.println("记录日志异常");
	e.printStackTrace();
	throw new RuntimeException(e);
	} finally {
	System.out.println("记录日志最终");
	} }
}

4.扩展@Pointcut,@Component ,@Aspect 理解

@Component //表明bean对象进入容器, 相当于在xml文件里面配置了一个bean文件

详解见spring 创建对象的注解@Component@Controller@service@Repository_代码人生的博客-CSDN博客
@Aspect
表明是一个切面,作,用和<aop:aspect></aop:aspect>一样,aop:aspect 标签表明配置切面 ,在 aop:aspect 标签内部使用对应标签来配置通知的类型,前置后置等等具体详解见

Spring 基于XML配置的AOP框架详细讲解_代码人生的博客-CSDN博客  第9点

@Pointcut

@Pointcut("execution(* com.bookmanagesystem.service.impl.*.*(..))")
    private void pc1() {}

    @Before("pc1() ")
    public void beforePrintLog() {
        System.out.println("前置通知MyLogger类中的beforePrintLog方法开始记录日志了");
    }

等同于xml当中的

<aop:pointcut expression="execution(* com.bookmanagesystem.service.impl.*.*(..) )"
            id="pc1" />

作用是配置前置,后置,最终,异常,环绕时候更方便的使用

例如
 

<!-- 1. 配置前置通知 -->

<!-- <aop:before method="beforePrintLog" pointcut-ref="pc1" /> -->

缺的文件和上面链接里面一致!!!

猜你喜欢

转载自blog.csdn.net/qq_53463161/article/details/121235616