1、首先修改bean.xml的配置資訊
目錄的結構可以看上一篇文章https://blog.csdn.net/weixin_44588495/article/details/93377796
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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">
</beans>
配置Spring建立容器時要掃描的包,配置Spring建立容器時要掃描的包
<context:component-scan base-package="com.spring" ></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2、配置AOP資訊
對實作類添加注解,保證掃描包的時候該對象進入IOC容器
@Service("accountService")
public class AccountService implements IAccountService {
@Component("logger")
public class Logger {
logger類
package com.spring.utils;
import javafx.beans.binding.ObjectExpression;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 提供公共方法
*/
@Component("logger")
@Aspect//表示目前類是一個切面類
public class Logger {
@Pointcut("execution(* com.spring.service.*.*(..))")
private void pt(){}
/**
* 前置通知
*/
@Before("pt()")
public void beforePrintLog(){
System.out.println("前置通知before記錄日志");
}
/**
* 後置通知
*/
@AfterReturning("pt()")
public void afterReturnPrintLog(){
System.out.println("後置通知afterReturn記錄日志");
}
/**
* 異常通知
*/
@AfterThrowing("pt()")
public void afterThrowPrintLog(){
System.out.println("異常通知before記錄日志");
}
/**
* 最終通知
*/
@After("pt()")
public void afterPrintLog(){
System.out.println("最終通知after記錄日志");
}
/**
* 環繞通知
*/
//@Around("pt()")
public void arroundLog(ProceedingJoinPoint pjp){
System.out.println("環繞通知");
}
}
執行:
直接使用這四個注解的方式它的調用順序是有問題的。
- 使用環繞通知:執行四種通知
@Around("pt()")
public Object arroundLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
Object[] args = pjp.getArgs();
System.out.println("前置通知");
rtValue = pjp.proceed(args);
System.out.println("後置通知");
return rtValue;
} catch (Throwable throwable) {
System.out.println("異常通知");
throwable.printStackTrace();
}finally {
System.out.println("最終通知");
}
System.out.println("環繞通知");
return null;
}
執行:這樣的順序就沒問題了
對于注解開發,更建議大家使用環繞通知,而不是四種注解的方式。
同時我們也可以修改為全注解的方式。用注解的配置類代替bean.xml檔案。
bean.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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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">
<context:component-scan base-package="com.spring" ></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
建立一個SpringConfiguration類
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = "com.spring")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
修改test類
package com.spring.test;
import com.spring.service.IAccountService;
import config.SpringConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService accountService = (IAccountService) ac.getBean("accountService");
accountService.saveAccount();
}
}
執行: