SpringAOP案例

1. 异常信息收集
在业务方法执行时,如果有异常抛出,则根据异常信息记录日志

package cn.tedu.em.aspect;

import java.io.FileWriter;
import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggerAspect {
   
	@AfterThrowing(value = "execution(* cn.tedu.em.service..*.*(..))",throwing="e")
	public void afterThrowing(JoinPoint jp,Throwable e) throws Exception{
   
		Class clz = jp.getTarget().getClass();
		String name = jp.getSignature().getName();
		String eMsg = e.getMessage();
		FileWriter writer = new FileWriter("err.log",true);
		String info = new Date().toLocaleString()+"--"+clz+"--"+name+"--"+eMsg;
		writer.write(info+"\r\n");
		writer.flush();
		writer.close();
	}
}

2.统计业务方法执行的时间
统计所有业务方法执行时的耗时

package cn.tedu.em.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class UseTimeAspect {
   
	@Pointcut("execution(* cn.tedu.em.service..*(..))")
	public void mx(){
   }
	
	@Around("mx()")
	public Object around(ProceedingJoinPoint pjp) throws Throwable{
   
		long begin = System.currentTimeMillis();
		Object obj = pjp.proceed();
		long end = System.currentTimeMillis();
		System.out.println("--useTime:["+(end - begin)+"]--");
		return obj;
	}
}

3. 实现事务控制
通过AOP实现事务控制,通过注解来标识方法是否需要事务

a. 开发事务注解

package cn.tedu.em.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Trans {
   

}

b. 在业务层使用注解

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用Spring AOP around通知的示例: 假设我们有一个服务类 `UserService`,其中有一个方法 `getUserById`,通过用户ID从数据库中获取用户信息。我们想要在方法执行前后添加日志记录,可以使用Spring AOP的around通知来实现。 首先,我们需要创建一个切面类 `LogAspect`,并在其中定义around通知方法 `logAround`: ```java @Aspect @Component public class LogAspect { @Around("execution(* com.example.UserService.getUserById(..))") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { // 记录方法执行之前的日志 System.out.println("Before getUserById method execution"); // 执行目标方法 Object result = joinPoint.proceed(); // 记录方法执行之后的日志 System.out.println("After getUserById method execution"); return result; } } ``` 上述代码中,我们使用了`@Aspect`注解将类声明为切面类,并使用`@Around`注解定义了around通知方法`logAround`。在`@Around`注解中,我们指定了需要拦截的方法为`com.example.UserService.getUserById`,并使用`ProceedingJoinPoint`参数来执行目标方法。 在`logAround`方法中,我们先记录了方法执行之前的日志,然后通过`joinPoint.proceed()`执行了目标方法`getUserById`,最后记录了方法执行之后的日志,并返回了方法执行的结果。 接下来,我们需要在Spring配置文件中添加以下配置: ```xml <aop:aspectj-autoproxy /> <bean id="logAspect" class="com.example.LogAspect" /> ``` 上述配置中,`<aop:aspectj-autoproxy />`用于启用AspectJ自动代理,并通过`<bean>`标签定义了切面类`LogAspect`的bean。 最后,我们可以在`UserService`中调用`getUserById`方法并观察控制台输出的日志: ```java @Service public class UserService { public User getUserById(Long userId) { // 根据用户ID从数据库中获取用户信息 User user = userDao.getUserById(userId); return user; } } ``` 当我们调用`getUserById`方法时,控制台会输出以下日志: ``` Before getUserById method execution After getUserById method execution ``` 这说明我们成功地在方法执行前后添加了日志记录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值