简体   繁体   中英

Unable to intercept the advice method in Spring AOP

I am implementing custom annotation processing using Spring AOP. I have below code.

public class CacheDemo  {

    private static ApplicationContext applicationContext;

    public static void main(String args[])
    {
         applicationContext =
               new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
    }
  }

//Application Configuration

@Configuration
@ComponentScan("Demo")
@Component
public class ApplicationConfiguration implements ApplicationContextAware {

    @Autowired
    TestCacheDemo testCacheDemo;
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {

        applicationContext = ctx;
    }
   
    @Bean
    public void testCacheDemoIntialize()
    {
        testCacheDemo.setApplicationContext(applicationContext);
        testCacheDemo.test();
    }
}

//CustomAnnotation processor

@Aspect
@Component
public class CustomAnnotationAspect {

    @Autowired
    private AbstractCacheService cacheService;

    @Around("@annotation(Demo.CustomCacheable)")
    public Object customCacheable(ProceedingJoinPoint joinPoint) throws Throwable { // This method is not called at all

       joinPoint.proceed();

      // Some other code to follow

}

// Custom Annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomCacheable {

}

// Annotation user

 @Component
public class CacheProvider
{
    @Autowired
    AbstractCacheService abstractCacheService;
    CacheManager<String,String> cacheManager;


    @PostConstruct
    void init()
    {
        cacheManager = CreateCache.create(s -> {return s.toUpperCase();});
        abstractCacheService.setCacheManager(cacheManager);

    }

    @CustomCacheable
    String getCacheValue(String s)
    {
        String str=s.toUpperCase();
        return str;
    }

}

For testing purpose I have created the below bean

@Component
public class TestCacheDemo extends TimerTask
{
    private  ApplicationContext applicationContext;
    private Timer timer;
    @Autowired
    CacheProvider cacheProvider;
    void test()
    {
        System.out.println("Called test");
        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }

        //CacheProvider cacheProvider = applicationContext.getBean(CacheProvider.class);
        //cacheProvider.getCacheValue("Hello");

        timer = new Timer();
        timer.schedule(this,1000,3000);
    }
    void setApplicationContext(ApplicationContext ctx)
    {
        applicationContext=ctx;
    }


    @Override
    public void run() {
        cacheProvider.getCacheValue("Hi");
    }
}

Whenever the application is started it will call the test method of the TestCacheDemo Class and sets the timer to be fired after 3 secs so that I can call the annotated method getCacheValue from inside the run method of the timer task. But when the annotated method is called the annotation processor is not invoked. Hence I am unable to do annotation processing. Please let me know where is the problem?

To use AspectJ in spring boot you must enable it. You should add the following annotation to your application main class (CacheDemo) or application configuration class.

@EnableAspectJAutoProxy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM