Reputation: 1888
I am trying to call aspect after method execution. But is doesn't even go to code inside aspect.
Here is my configuration class:
@Configuration
@PropertySource("classpath:auditorium.properties")
@ComponentScan(basePackages = { "com.epam.spring.hometask.*" })
@EnableAspectJAutoProxy
public class AppConfig {
///Bean definitions
}
This is my aspect:
@Component
@Aspect
public class CounterAspect {
@AfterReturning(pointcut = "execution(* com.epam.spring.hometask.service.EventService.getByName(..))", returning = "event")
public void calculateAccessEventByNameCounter(Event event) {
System.out.println("Aspect running");
}
This is function signature I want to provide with my aspect:
public Event getByName(String name);
And test:
@Test
public void getByNameTest(){
Event actualEvent = eventService.getByName("Test event 1");
}
Nothing is printed after getByName call
Upvotes: 0
Views: 135
Reputation: 1955
Event event = new Event();
event.getByName();
Event should be a spring-managed bean for the aspects to be applied, i.e. you should obtain objects of type Event from the ApplicationContext as follows:
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:META-INF/Spring.xml");
Event event = context.getBean(Event.class);
or get them injected using @Resource or @Autowired annotations.
Then, the call to getByName() on event object will go through the AOP proxy created by Spring framework and the code in the AOP advices that match the method definition get executed.
Another way to get a spring-managed bean, while creating objects using new operator, is using the @Configurable annotation.
Upvotes: 0