当 AspectJ ExpressionPointcutAdvisor 以编程方式创建时不会触发切入点
Point cut is not triggered when AspectJExpressionPointcutAdvisor create programatically
我正在根据应用程序属性文件中存在的切入点数量创建 AspectJExpressionPointcutAdvisor。它创建的对象没有错误,但未触发切入点。
注意:需要根据属性文件中切入点表达式的数量动态创建bean(可变)。
应用程序属性文件
pointcut.expression.projectUpdate[0]= execution(* com.abc.app.service.impl.TestServiceImpl.updateProjectDetails(..))
pointcut.expression.projectUpdate[1]= execution(* com.abc.app.service.impl.TestServiceImpl.cancelProject(..))
pointcut.expression.projectUpdate[2]= execution(* com.abc.app.service.impl.TestCSATRatingServiceImpl.saveRatingDetails(..))
TestConfig.class
@Configuration
public class TestConfig implements BeanFactoryAware {
@Autowired
private PointcutExprProperties pcExprProp;
@Autowired(required=false)
private ProjectUpdateAspect projectUpdateAdvice;
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@PostConstruct
public void configure() {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
int i=1;
for(String pointCut : pcExprProp.getProjectUpdate()) {
AspectJExpressionPointcutAdvisor projectUpdateAdvisor = new AspectJExpressionPointcutAdvisor();
projectUpdateAdvisor.setExpression(pointCut);
projectUpdateAdvisor.setAdvice(projectUpdateAdvice);
configurableBeanFactory.registerSingleton("beanName_"+i, projectUpdateAdvisor);
i++;
}
}
}
ProjectUpdateAspect.class
@Component
@Aspect
public class ProjectUpdateAspect implements AfterReturningAdvice {
private static final Logger log = LoggerFactory.getLogger(ProjectUpdateAspect.class);
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
try {
// some thing
}catch (Exception exception) {
log.error("Error while processing ProjectUpdateAspect",exception);
}
}
}
PointcutExprProperties
@Configuration
@ConfigurationProperties(prefix = "pointcut.expression")
@Validated
public class PointcutExprProperties {
@NotNull
private List<String> projectCreate;
@NotNull
private List<String> projectUpdate;
public List<String> getProjectCreate() {
return projectCreate;
}
public void setProjectCreate(List<String> projectCreate) {
this.projectCreate = projectCreate;
}
public List<String> getProjectUpdate() {
return projectUpdate;
}
public void setProjectUpdate(List<String> projectUpdate) {
this.projectUpdate = projectUpdate;
}
}
请建议我如何解决这个问题。
我建议你这样做:
- 您没有将您的“方面”定义为
@Component @Aspect
,而是使其实现 MethodInterceptor
。
- 您使用属性文件中的值创建
AspectJExpressionPointcut
。
- 您将
DefaultPointcutAdvisor
(使用切入点和拦截器配置)注册为 bean。
另请参阅 and my GitHub sample repository,我刚刚为您更新了它,以便包括阅读来自 application.properties
的切入点。
我正在根据应用程序属性文件中存在的切入点数量创建 AspectJExpressionPointcutAdvisor。它创建的对象没有错误,但未触发切入点。
注意:需要根据属性文件中切入点表达式的数量动态创建bean(可变)。
应用程序属性文件
pointcut.expression.projectUpdate[0]= execution(* com.abc.app.service.impl.TestServiceImpl.updateProjectDetails(..))
pointcut.expression.projectUpdate[1]= execution(* com.abc.app.service.impl.TestServiceImpl.cancelProject(..))
pointcut.expression.projectUpdate[2]= execution(* com.abc.app.service.impl.TestCSATRatingServiceImpl.saveRatingDetails(..))
TestConfig.class
@Configuration
public class TestConfig implements BeanFactoryAware {
@Autowired
private PointcutExprProperties pcExprProp;
@Autowired(required=false)
private ProjectUpdateAspect projectUpdateAdvice;
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@PostConstruct
public void configure() {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
int i=1;
for(String pointCut : pcExprProp.getProjectUpdate()) {
AspectJExpressionPointcutAdvisor projectUpdateAdvisor = new AspectJExpressionPointcutAdvisor();
projectUpdateAdvisor.setExpression(pointCut);
projectUpdateAdvisor.setAdvice(projectUpdateAdvice);
configurableBeanFactory.registerSingleton("beanName_"+i, projectUpdateAdvisor);
i++;
}
}
}
ProjectUpdateAspect.class
@Component
@Aspect
public class ProjectUpdateAspect implements AfterReturningAdvice {
private static final Logger log = LoggerFactory.getLogger(ProjectUpdateAspect.class);
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
try {
// some thing
}catch (Exception exception) {
log.error("Error while processing ProjectUpdateAspect",exception);
}
}
}
PointcutExprProperties
@Configuration
@ConfigurationProperties(prefix = "pointcut.expression")
@Validated
public class PointcutExprProperties {
@NotNull
private List<String> projectCreate;
@NotNull
private List<String> projectUpdate;
public List<String> getProjectCreate() {
return projectCreate;
}
public void setProjectCreate(List<String> projectCreate) {
this.projectCreate = projectCreate;
}
public List<String> getProjectUpdate() {
return projectUpdate;
}
public void setProjectUpdate(List<String> projectUpdate) {
this.projectUpdate = projectUpdate;
}
}
请建议我如何解决这个问题。
我建议你这样做:
- 您没有将您的“方面”定义为
@Component @Aspect
,而是使其实现MethodInterceptor
。 - 您使用属性文件中的值创建
AspectJExpressionPointcut
。 - 您将
DefaultPointcutAdvisor
(使用切入点和拦截器配置)注册为 bean。
另请参阅 application.properties
的切入点。