spring基于注解的AOP配置

本文内容纲要:spring基于注解的AOP配置

xml文件

<?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:context="http://www.springframework.org/schema/context"

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

<!--spring基于注解的AOP配置-->

<!--配置spring创建容器时要扫描的包-->

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

<!--配置spring开启注解AOP的支持-->

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

切面类

package com.itheima.utils;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;

/**

* @Author: lijiahao

* @Description: 用于记录日志的工具类,里面提供了公共的代码

* @Data: Create in 18:45 2020/2/8

* @Modified By:

*/

@Component("Logger")

@Aspect//表示当前类是一个切面类

public class Logger {

@Pointcut("execution(* com.itheima.service.impl.*.*(..))")//设置切入点

private void pt1(){

}

//前置通知

@Before("pt1()")

public void beforePrintLog(){

System.out.println("Logger类中的beforeprintlog方法执行了。。。");

}

//后置通知

@AfterReturning("pt1()")

public void afterReturningPrintLog(){

System.out.println("Logger类中的afterReturningPrintLog方法执行了。。。");

}

//异常通知

@AfterThrowing("pt1()")

public void afterThrowingPrintLog(){

System.out.println("Logger类中的afterThrowingPrintLog方法执行了。。。");

}

//最终通知

@After("pt1()")

public void afterPrintLog(){

System.out.println("Logger类中的afterPrintLog方法执行了。。。");

}

//环绕通知

@Around("pt1()")

public Object aroundPrintLog(ProceedingJoinPoint proceedingJoinPoint){

Object rtValue = null;

try {

Object []args = proceedingJoinPoint.getArgs();//得到方法执行所需的参数

System.out.println("Logger类中的aroundPrintLog方法执行了。。。前置");

rtValue = proceedingJoinPoint.proceed();//明确调用业务层方法(切入点方法)

System.out.println("Logger类中的aroundPrintLog方法执行了。。。后置");

return rtValue;

} catch (Throwable throwable) {

System.out.println("Logger类中的aroundPrintLog方法执行了。。。异常");

throw new RuntimeException(throwable);

} finally {

System.out.println("Logger类中的aroundPrintLog方法执行了。。。最终");

}

}

}

service接口

package com.itheima.service;

/**

* @Author: lijiahao

* @Description:账户的业务层接口

* @Data: Create in 18:42 2020/2/8

* @Modified By:

*/

public interface IAccountService {

//模拟保存账户

void saveAccount();

//模拟更新账户

void updateAccount(int i);

//删除账户

int deleteAccount();

}

service实现类

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import org.springframework.stereotype.Service;

/**

* @Author: lijiahao

* @Description:

* @Data: Create in 18:44 2020/2/8

* @Modified By:

*/

@Service("accountService")

public class AccountServiceImpl implements IAccountService {

public void saveAccount() {

System.out.println("执行了保存。。。");

//int i=1/0;

}

public void updateAccount(int i) {

System.out.println("执行了更新。。。"+i);

}

public int deleteAccount() {

System.out.println("执行了删除。。。");

return 0;

}

}

测试类

package com.itheima.test;

import com.itheima.service.IAccountService;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* @Author: lijiahao

* @Description: 测试aop的配置

* @Data: Create in 19:17 2020/2/8

* @Modified By:

*/

public class AOPTest {

public static void main(String[] args) {

//1.获取容器

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

//2.获取对象

IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");

//3.执行方法

accountService.saveAccount();

}

}

 

本文内容总结:spring基于注解的AOP配置

原文链接:https://www.cnblogs.com/lijiahaoAA/p/12285489.html

以上是 spring基于注解的AOP配置 的全部内容, 来源链接: utcz.com/z/296217.html

回到顶部