如何在 AOP 建议中将日志保存到数据库 [英] How to save logs to database in AOP advice

查看:38
本文介绍了如何在 AOP 建议中将日志保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Spring AOP 时在数据库中保存日志(或任何其他信息)时遇到问题.

I have a problem with saving logs (or any other information) in the database when using Spring AOP.

问题的根源如下:

@Aspect
@Configuration
@Component
@EnableAspectJAutoProxy
public class LoggingAspect {

    @Autowired
    private LogService logService;

    @AfterThrowing(pointcut = "execution(* org.springframework.web.client.RestTemplate+.*(..))", throwing = "ex")
    public void logError(Exception ex) {
        Log log = new Log("Error!");
        logService.save(log);
    }
}

logService 实例是一个简单的 @Service 类,它在内部调用 @Repository 方法.没什么特别的.Log 类更简单:) 它只包含一个变量(如果我们包含 id 则为两个):

logService instance is a simple @Service class which calls @Repository methods internally. Nothing special. Log class is even simpler:) It contains only one variable (two if we include id):

@Entity
@Table
public class Log {
    @Id
    private Long id;

    @Column
    private String message;

    //getters, setters, constructors, etc.
}

LoggingAspect 工作正常.@AfterThrowing 建议在出现任何异常时被正确调用.但是调用 save 方法后,db 中没有保存任何内容.当我尝试调用 getAll() 方法而不是 save 时,一切都按预期工作.所有数据都是从数据库加载的.此问题仅适用于 save 方法.

LoggingAspect works correctly. @AfterThrowing advice is called properly in case of any exceptions. But nothing is saved in db after calling save method. When I try to call getAll() method instead of save everything works as expected. All data is loaded from the database. This problem applies only to the save method.

如有任何提示,我将不胜感激.

I'll be grateful for any hints.


正如我之前提到的,@Service@Repository 类现在非常基础.也许值得补充的是,外部方面类的一切都完美无缺.可以将日志保存在数据库中.此问题仅在保存时发生,并且仅在用于各种建议时才会发生.添加服务代码


As I mentioned previously, the @Service and @Repository classes are very basic for now. And maybe it's worth adding that outside aspect class everything works perfectly. It is possible to save logs in the database. This issue occurs only with saving and only when used in all kinds of advices. Service code added

@Service
public class LogService {

    @Autowired
    private LogRepository logRepository;

    @Transactional(readOnly = true)
    public List<Log> getAllLogs() {
        return logRepository.findAll();
    }

    @Transactional
    public void save(Log log) {
        logRepository.save(log);
    }
}

添加了存储库代码

@Repository
public interface LogRepository extends JpaRepository<Product, Log> {
}

推荐答案

听起来像事务处理.

但更多信息会更好,我只能在这里猜测.如果您还没有检查,请检查以下几点.

But more info would be good, I can only guess here. So few points to check, if you didn't already.

我猜你把@Transational 放在了 Service 或 Repositoty 上,对吧?还要确保您在 Spring 默认代理机制的独立类中拥有 Repository 和 Service.

I guess you put @Transational on the Service or Repositoty, right? Also make sure you have Repository, Service in seperate classes as of the Spring default proxying mechanism.

为 jpa、hibernate 和 spring 事务调试和添加日志记录.

Debug and add logging for jpa, hibernate and spring transaction.

这篇关于如何在 AOP 建议中将日志保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆