Reputation: 381
I am using the following structure but failing but failing to create the proxy call.
In controller-servlet.xml
<context:component-scan base-package="com.controller" />
<mvc:annotation-driven/>
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:default-servlet-handler/>
In application-context.xml
<context:component-scan base-package="com.common" />
<context:component-scan base-package="com.dao" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />
<!-- Aspect Bean Definition
<bean id="aspectBean" class="com.common.AspectImple" /> -->
Aspect class
package com.common;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectImple {
@Before("execution(* com.controller.JobController.*(..))")
public void beforeImpl() {
System.out.println(" Before Done ");
}
}
Controller class
@Controller
public class JobController {
@RequestMapping(method = RequestMethod.GET, value = "/xyz")
public ModelAndView abc(HttpServletRequest request,
HttpServletResponse response) {
...
m1(10);
...
}
}
public void m1(int i){
System.out.println(" AOP Done ");
}
The AOP is not working and during call of abc(..) & m1(..) method, beforeImpl() is not getting called. Could someone please help me out to solve this problem.
Upvotes: 3
Views: 9556
Reputation: 51
I had same issue but am using Annotation based configuration. To get AOP working for @Controller, this did the trick:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan(basePackages = {"com.mypackage"})
public class MvcConfig extends WebMvcConfigurationSupport
{
...
@Bean
public MyAspect myAspect()
{
return new MyAspect();
}
}
Upvotes: 5
Reputation: 456
Spring AOP Advice on Annotated Controllers - is a good answer http://mergetag.com/spring-aop-advice-on-annotated-controllers-2/
Upvotes: 0
Reputation: 7283
What about move
<context:component-scan base-package="com.dao" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />
from application-context.xml to controller-servlet.xml?
The aspects and the beans to be applied needs to be in the same ApplicationContext but ApplicationContext is not aware of WebApplicationContext .
Upvotes: 5