0

I am new to Spring AOP and annotations. I tried to write a simple program that uses Aspect. I am unable to figure out where I went wrong. Its doesn't print the what I have in my Aspect.

package com.business.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@Configuration
public class PrintMain {

    public static void main(String[] args) {
        // Do I always need to have this. Can't I just use @Autowired to get beans
        ApplicationContext ctx = new AnnotationConfigApplicationContext(PrintMain.class);
        CheckService ck = (CheckService)ctx.getBean("service");
        ck.print();
    }

    @Bean(name="service")
    public CheckService service(){
        return new CheckService();
    }

}

package com.business.main;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SimpleAspect {

    @Around("execution(* com.business.main.CheckService.*(..))")
    public void applyAdvice(){
        System.out.println("Aspect executed");
    }
}

package com.business.main;

import org.springframework.stereotype.Component;

@Component
public class CheckService{
    public void print(){
        System.out.println("Executed service method");
    }
}

Output: Executed service method

I expect to print what I have in my Aspect

karthik
  • 773
  • 2
  • 11
  • 19

1 Answers1

1

I think your @Component isn't work!
Maybe, you need the @ComponentScan

@EnableAspectJAutoProxy
@ComponentScan
@Configuration
public class PrintMain {

    public static void main(String[] args) {
        // Do I always need to have this. Can't I just use @Autowired to get beans
        ApplicationContext ctx = new AnnotationConfigApplicationContext(TNGPrintMain.class);
        CheckService ck = (CheckService)ctx.getBean("service");
        ck.print();
    }

    @Bean(name="service")
    public CheckService service(){
        return new CheckService();
    }

}
JonahCui
  • 125
  • 1
  • 5
  • I had to make two changes to make it work 1) Implement interface 2) add @ ComponentScan. But I would like to know why its not working without interface ...... I read something related to CGLIB which cannot be forced through annotations. Secondly why Its not working without @ ComponentScan – karthik Nov 30 '16 at 01:04
  • @karthik Because you just use Component, but spring not use it as bean, means the ApplicationContext doesn't has it. When ApplicationContext wired, it needs know who will be wired as a bean. ApplicationContext doesn't know whether you need Component or your use it, so you need use Xml or ComponentScan to tell Spring I have some beans need be wired. – JonahCui Nov 30 '16 at 01:04