切入点内的 Spring AOP

发布于 2024-11-26 23:46:10 字数 1702 浏览 0 评论 0原文

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
             }
    }

我的业务逻辑类:

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);
}

}

现在我想测量每个业务逻辑函数 (daoservice.*) 中执行的所有方法的执行情况,

我创建了我的 Aspect 类,

@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;
      }

}

不幸的是,什么也没发生。我认为我的 @PointCut 定义不正确,如何正确执行?

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
             }
    }

my Business Logic class:

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);
}

}

Now I would like to measure execution of all methods executed within each of Business logic functions (daoservice.*)

I create my Aspect classes

@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;
      }

}

Unfortunately, nothing happened. I think my @PointCut definition is incorrect how can I do it correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2024-12-03 23:46:10

您可能想要这样:

@Pointcut("within(com.vanilla.blService.BlService+)")
public void businessLogicMethods(){}

BlService+ 表示 BlService 和所有子类/实现类。

You probably want this:

@Pointcut("within(com.vanilla.blService.BlService+)")
public void businessLogicMethods(){}

BlService+ means BlService and all subclasses / implementing classes.

拔了角的鹿 2024-12-03 23:46:10

您应该使用下面的切入点

@Pointcut("execution(* com.vanilla.blService.BlService.*(..))")

You should use the below pointcut

@Pointcut("execution(* com.vanilla.blService.BlService.*(..))")
許願樹丅啲祈禱 2024-12-03 23:46:10

尝试使用:

within(com.vanilla.blService.BlService) && execution(public * com.vanilla.daoService..*.*(..))

Try to use:

within(com.vanilla.blService.BlService) && execution(public * com.vanilla.daoService..*.*(..))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文