Announcement Announcement Module
No announcement yet.
NoSuchBeanDefinitionException when using @Inject annotation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • NoSuchBeanDefinitionException when using @Inject annotation

    I am writing a library that should be usable with Spring as well as Java EE 6. Hence for autowiring I am using @Inject which, as I understand, is supported by both frameworks. However Spring is throwing a NoSuchBeanDefinitionException when resolving injected dependencies. For example:

    org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [test.SecurityService] found for dependency:
    expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
    My bean is declared as follows. Spring is not picking it up because it has no annotation. On the other hand, Java EE 6 (CDI) has no issues with it because POJOs in CDI are automatically managed beans (section 3.1.1 JSR 299):

    public class SecurityService {
    I do not want to introduce Spring specific annotations in this library. So I can think of two workarounds:

    1) Put @Named annotation on the bean and Spring will pick it up

    2) Declare the bean in applicationContext.xml

    Is there a more elegant way that I may be missing?