5

I have the code @Inject works in one class but not in other. Here's my code:

  • context.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:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation=" http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    ">
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
    <context:component-scan base-package="com.myfashions.services"/>
    <context:component-scan base-package="com.myfashions.dao"/>
</beans>
  • SellerRetriever.java
public class SellerRetriever {
    @Inject
    UserDAO userDAO;
    ...
    ...
}

UserDAO class is present in com.myfashions.dao package. @Inject is not working in Seller.java. Any reason why?

vicky
  • 1,046
  • 2
  • 12
  • 25

3 Answers3

7

Make sure that both SellerRetriever and the implementation of UserDAO are annotated for the component scan. This will ensure that the latter is injected into the former:

@Service
public class SellerRetriever {
    @Inject
    UserDAO userDAO;
    ...
}

Annotate the UserDAO implementation with @Component.

When scanning multiple paths use:

<context:component-scan base-package="com.myfashions.services, com.myfashions.dao"/>
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 1
    UserDAO is getting injected in other service classes but not in SellerRetriever.java – vicky May 20 '13 at 12:40
  • 1
    I assume that `SellerRetriever` in `com.myfashions.services`. Try using a single component scan tag in your application context – Reimeus May 20 '13 at 12:57
  • @Reimeus That's an interesting suggestion. Could Spring do the first component scan and miss `UserDAO` because it doesn't get around to it until the second component scan? – davidfmatheson May 20 '13 at 14:25
  • @davidfmatheson TBH both are equivalent, just a preference. Suspect that problem here _could possibly be_ an old class version. A rebuild should determine if that is the case – Reimeus May 20 '13 at 15:06
3

To be eligible to scan, your class must be annotated with either a more generic @Component, or @Service or @Repositories etc.. In your case, @Service logically better fits. You could then (if you need) define some aspects (AOP) focused specifically on services call.

Besides, you may want to use @Autowired instead of @Inject to retrieve your bean.

For more information about differences concerning these two annotations:

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

and you can see my comment just below explaining one good reason to keep @Autowired instead of @Inject.

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • Its a great answer, but still I'm getting NullPointerException. – vicky May 20 '13 at 12:29
  • I thought you could use either `@Inject` or `@Autowired` in conjunction with `component-scan`... as long as you're using Spring 3. – Jonathan May 20 '13 at 12:38
  • add @Component or @ Service or @ Repositories above your class declaration, otherwise it would be not scanned. – Mik378 May 20 '13 at 12:41
  • @Mik378 describe you answer in more detail. I do not agree with you. – Michal Borek May 20 '13 at 13:12
  • @Michal Boreck Yes, indeed we could use `@Inject` since Spring 3. But in order to fit the standard way, we commonly may use `@Autowired`. – Mik378 May 20 '13 at 13:15
  • There is no `standard` way. There is only the new way and the old one. – Michal Borek May 20 '13 at 13:16
  • @Michal Borek Take the case of an application using Spring 2. Since `@Inject`(JSR-299) and `@Autowired` (owning by Spring even before the third version) behave identically, when we want to upgrade Spring 2 to Spring 3, two scenarios: blending `@Autowired` with `@Inject`, replacing all `@Autowired` by `@Inject`, or continue using `@Autowired`. In a huge project, I think that the cleanest and effortless way is to continue using `@Autowired`, in order to make Spring 3 upgrade more transparent to user. – Mik378 May 20 '13 at 13:40
  • 1
    @Mik378 That's one use case, legacy support between Spring 2 & 3. The question doesn't mention such a requirement, but OK. There are other things that might drive you away from `@Autowired`, such as a desire to wire beans up by name using annotations. In this case Spring recommends that you use `@Resource` (http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-annotation-config). You might also want to move towards a Java standard (like `@Inject`) in case you switch to a different dependency injection framework down the line. – davidfmatheson May 20 '13 at 14:23
  • @davidfmatheson I agree with you. – Mik378 May 20 '13 at 14:26
  • 2
    Since `@Inject` is a java annotation, and `@Autowired` is spring depdendant, `@Inject` has an advantage if you ever want to change DI-framework. – Tobb Apr 08 '14 at 07:11
2

I found my mistake, I'm posting this because in case anyone has the same problem. I used new operator to create an SellerRetriver object. Inject won't work if new operator is used to call that particular class.

vicky
  • 1,046
  • 2
  • 12
  • 25