【发布时间】:2018-06-11 00:22:17
【问题描述】:
我正在做一个项目,其中我们有很多实体,我们将在这些实体上进行 CRUD 操作。我已经创建了一个基础实体类,并且在所有其他实体中我扩展了基础实体类,它具有诸如 created_date、created_by、last_updated_date、last_updated_by 等公共字段。现在,我想在 Spring CrudRepository 方法上实现方面并设置上述内容保存时提到的字段。
我尝试过实现类似的东西,但没有用。
package com.cerium.aop;
import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.cerium.datamodel.AccountDataModel;
import com.cerium.domain.Account;
import com.cerium.domain.BaseEntity;
import com.cerium.util.Constants;
/**
* @author Manikanta B Cerium
*
*/
@Component
@Aspect
public class SampleAspect {
private static final Logger LOG = LoggerFactory.getLogger(SampleAspect.class);
@Around("execution(* com.cerium.repository.*.save (com.cerium.domain.BaseEntity)) && args(saveData)")
public Object beforeSave(ProceedingJoinPoint proceedingJoinPoint, Object saveData) throws Throwable {
LOG.debug("Into aspect before save: {}", saveData);
BaseEntity baseEntity = (BaseEntity) proceedingJoinPoint.proceed(new Object[] { saveData });
// set the fields here......
baseEntity.setCreatedDate(new Date());
System.out.println(saveData);
return baseEntity;
}
}
【问题讨论】:
-
很难找出问题所在,因为我只看到了一个方面,而不是它所针对的实际代码(包名、类和方法声明)。另一个问题是:Spring AOP 是否配置正确,即它是否可以正常工作,例如对于像
execution(* *(..))这样的简单切入点和简单的@Before建议,或者可能是一个简单的@Around建议,它只会继续并返回结果,没有别的?
标签: spring-boot spring-data-jpa aspectj