0

I am trying to advice a method with pointcut.

All other pointcut are working, This is the aspect signature, and commented @Before pointcuts are the ones that work. But I am not sure why last bulkInsert pointcut is not working.

   //@Before("execution(public * com.service.dao.WorkStayDao.update(..))")
  // @Before("execution(public * com.workunit.WorkService.isSkipUpdateWorkUnit(..))")
    @Before("execution(public * com.service.dao.WorkGoDao.bulkInsert(..))")
        public void logMethodCall(JoinPoint jp) throws JsonProcessingException {}

WorkGoDao.bulkInsert() gets called from WorkStayDao.update() method.

update() signature is

public Work update(String userID, final Work workt)

bulkInsert() Signature is

public void bulkInsert(String UserId, Set<Work> work, SqlSession session)

WorkGoDao class is also marked as @Service.

My Aspect class is also annotated with all required Annotations

@Aspect
@Configuration
@EnableAspectJAutoProxy
public class ErrorLoggingAspect

Goal is that: bulkInsert Should call the logMethodCall() before execution.

I am not sure why methods from same packages are behaving differently on being advised. I have looked at all aspect related queries but can't find the solution. And the biggest issue that it's really hard to debug as no error logs are printed. I am using Intellij to run locally.

Am I missing any concept or something, any help will be really appreciated, even ways to debug.

vikky
  • 77
  • 9
  • _"My Aspect class is also annotated with all required Annotations"_ No, it is not. An aspect is a `@Component`, not a `@Configuration`. Configuration is a global thing for your application and should be separate from application components such as services, controllers and aspects. – kriegaex Aug 27 '22 at 10:41
  • @kriegaex thanks for bringing up that in notice. But still after making these changes issue is still persisting. We are using drop wizard for our project. Its also strange that my Ide intellij is able to identify the advised method, but in runtime its not getting triggered. – vikky Aug 27 '22 at 17:17
  • Then please share a sample project on GitHub, so I can reproduce the issue and help you. – kriegaex Aug 27 '22 at 20:13

1 Answers1

0

I have found the issue with my code.

The mistake I was making was Creating User managed object for the class WorkGoDao and WorkStayDao in calling classes. AOP looks for the bean in spring context, since mine were user created object and hence unavailable in Context.

I fixed the issue by creating Spring managed object of WorkGoDao and WorkStayDao, for me an object with Field level @Autowired injection did the job. One can also create the setter or constructor injected object.

vikky
  • 77
  • 9
  • 1
    See? This is why nobody can help you without a minimal, but complete example called an [MCVE](https://stackoverflow.com/help/mcve). Otherwise I would have noticed in a second what was wrong with your code. As the [Spring AOP manual](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-introduction-spring-defn) says: _"Spring AOP currently supports only method execution join points (advising the execution of methods on **Spring beans**)."_ It also explains that you need native AspectJ to advise non-Spring classes. But I am happy you could solve your own problem. – kriegaex Sep 03 '22 at 14:49