切入点内的Spring AOP
我的业务逻辑类:切入点内的Spring AOP,spring,jakarta-ee,aspectj,spring-aop,Spring,Jakarta Ee,Aspectj,Spring Aop,我的业务逻辑类: package com.vanilla.daoService; @Repository("daoService") public class DaoServiceImpl implements DaoService { @Override public String addStudent(Student student) { //saving new user }
package com.vanilla.daoService;
@Repository("daoService")
public class DaoServiceImpl implements DaoService {
@Override
public String addStudent(Student student) {
//saving new user
}
@Override
public String updateStudent(Student student) {
//update new user
}
@Override
public String getStudent(String id) {
//update new user
}
}
现在我想测量在每个业务逻辑函数(daoservice.*)中执行的所有方法的执行情况
我创建我的方面类
package com.vanilla.blService;
@Service("blService")
public class BlServiceImpl implements BlService {
@Autowired
DaoService daoService;
@Override
public void updateStudent(String id){
Student s = daoService.getStudent(id);
set.setAddress(address);
daoService.updateStudent(s);
}
}
不幸的是,什么也没发生。我认为我的@PointCut
定义不正确,我如何才能正确地执行它?尝试使用:
@Aspect
public class BlServiceProfiler {
@Pointcut("within(com.vanilla.blService.BlService.*)")
public void businessLogicMethods(){}
@Around("businessLogicMethods()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("Going to call the method " + pjp.toShortString());
Object output = pjp.proceed();
System.out.println("Method execution completed.");
long elapsedTime = System.currentTimeMillis() - start;
System.out.println(pjp.toShortString()+" execution time: " + elapsedTime + " milliseconds.");
return output;
}
}
你可能想要这个:
within(com.vanilla.blService.BlService) && execution(public * com.vanilla.daoService..*.*(..))
BlService+
表示BlService和所有子类/实现类。您应该使用下面的切入点
@Pointcut("within(com.vanilla.blService.BlService+)")
public void businessLogicMethods(){}
您是否向Spring注册了aspect?(只是检查…)我测试了它,它看起来像是应用在BlService.updateStudent而不是daoService方法上,因为它将调用方法执行(BlService.updateStudent(..),如果您希望它应用到DAOs,那么将它改为(com.vanilla.daoService.daoService+)中的
。您的问题是希望使用切入点DAO,但您的代码示例暗示您正在尝试切入点服务,这有点让人困惑。@Niall,那么@pointcut(“in(com.vanilla.blService.blService+)”、@pointcut(“in(com.vanilla.blService.blService.*)和@pointcut(“execute(*com.vanilla.blService.blService.*))之间的区别是什么呢所有这些服务都有相同的结果?在这种情况下,它们将产生相同的结果,因为恰好存在重叠,也就是说,您的服务都在同一个包中,并且都扩展了BIService。我刚刚指出,您最初的问题是希望对包daoservice中的所有内容进行切入点,但您的切入点都引用包blservice。我得到:嵌套异常是org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:pointcut表达式'businessLogicMethods()'包含不支持的切入点原语'call'对不起。我忘了,@AspectJ在春季不支持call
。尝试像在固定代码中一样使用执行
。不幸的是,它没有执行,看起来缺少切入点。
@Pointcut("execution(* com.vanilla.blService.BlService.*(..))")