Results 1 to 3 of 3

Thread: Annotations not working though DI works in Spring AOP

  1. #1
    Join Date
    Sep 2009
    Posts
    1

    Default Annotations not working though DI works in Spring AOP

    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

  2. #2

    Default

    Anyone having this issue? I am too using Spring 3.0 GA. It's driving me crazy.

  3. #3

    Default

    Double check to make sure you aren't calling the following:

    Quote Originally Posted by kunal.j View Post
    <context:component-scan base-package="com" />
    In more than one context (e.g. applicationContext.xml and dispatcher-servlet.xml).

    That happened to be my issue.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •