Hi,
I am facing a bit strange behavior with Spring Aop.
Listed below is the code i have
**** LoginController ****
package com.aspect;
@Controller
@RequestMapping("/app/*")
public class LoginController
{
private LoginService loginService;
public LoginService getLoginService()
{
return loginService;
}
@Autowired
public void setLoginService(LoginService loginService)
{
this.loginService = loginService;
}
@RequestMapping(value = "home")
public String home(Model model, HttpServletRequest req, HttpServletResponse res)
{
this.loginService.test();
return "app/home";
}
***** Login Service *****
package com.aspect;
//@Service
public class LoginService
{
public void test(){
System.out.println("LoginService.test....");
}
****** Aspect *****
@Component
@Aspect
public class TestAspect{
@Order(value = 1)
@Around("execution(public * com.aspect.LoginService.*(..)) ")
public Object intercept(ProceedingJoinPoint pjp) throws Throwable
{
Object r = null;
System.out.println("Aspect Is Working");
pjp.proceed();
return r;
}
}
******** ApplicationContext.xml ******
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schem...-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schem...ng-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schem...ontext-2.5.xsd
http://www.w3.org/1999/XSL/Transform
http://www.w3.org/1999/XSL/Transform.xsd"
>
<context:component-scan base-package="com" />
<aop:aspectj-autoproxy />
<bean id="LoginService" class="com.aspect.LoginService" />
</beans>
Now if i run the application, as shown above in tomcat 6, the aspect works and intercept method in LoginService.java.
But if i comment bean definition of LoginService from applicationContext and uncomment @Service annotation in LoginService, with this apsect intercept doesnt work.
I am not able to figure out why the same piece of code works with DI happening through applicationContext but doesn't work when it happens through annotations.
Please let me know if i am missing something out here.
Thanks for any help.
Regards
Kunal


Reply With Quote
