Announcement Announcement Module
Collapse
No announcement yet.
Self injection with spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Self injection with spring

    I tried the following code with Spring 3.x which failed with BeanNotFoundException (exception cause pasted below)

    Code:
    @Service
    public class UserService implements Service{
       @Autowired
       private Service self;
    }
    Since I was trying this with Java 6, I found the following code works fine:

    Code:
    @Service(value = "someService")
    public class UserService implements Service{
       @Resource(name = "someService")
       private Service self;
    }
    but I couldn't understand how it resolves the cyclic dependency. Anyone?

    I tried to search for the documentation but didn't find any(may be I missed), so couldn't able to make out whether this is the feature or bug

    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [com.spring.service.Service] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    Last edited by premraj; Mar 2nd, 2011, 02:49 AM.

  • #2
    Tip from spring documentation.

    Code:
    If you intend to express annotation-driven injection by name, do not primarily use
    @Autowired, even if is technically capable of referring to a bean name through
    @Qualifier values. Instead, use the JSR-250 @Resource annotation, which is
    semantically defined to identify a specific target component by its unique name, with the
    declared type being irrelevant for the matching process.
    but still not sure about self injection

    also read

    3.9 Annotation-based container configuration section in documentation in case if you have missed.

    Comment


    • #3
      I'm not worrying about whether to use name while injecting or not, I just wanted to know why @Resource Works and @Autowired doesn't and more importantly is it good to use @Resource for this kind of purpose, as I couldn't found documented behaviour for this I'm just confused whether spring will continue with this functionality or not.

      Comment

      Working...
      X