基本配置
- 最近在写一些东西,但是日常开发配置接触的太少了很多都忘记了
- 记录一些常用的东西的最简单用用法方便自己快速的使用
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.*"></context:component-scan>
<!-- 配置spring开启注解AOP的支持,使用注解写aop必须要开启 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.test.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.test.service.impl.*.*(..))")
private void pt1(){}
public void beforePrintLog(){
System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
}
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
}
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
}
public void afterPrintLog(){
System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
}
@Around("pt1()")
public Object aroundPringLog(ProceedingJoinPoint p){
Object rtValue = null;
try{
String name = p.getSignature().getName();
Object[] args = p.getArgs();
log.info("########## 方法名{},参数值{}",name,args+" ##########");
rtValue = p.proceed(args);
log.info("########## 返回值{}",rtValue+" ##########");
return rtValue;
}catch (Throwable t){
log.info("########## 异常 ##########");
throw new RuntimeException(t);
}finally {
log.info("########## 日志结束 ##########");
}
}
}