1

We recently encountered an issue where we had a bean being injected using the @Resource annotation. This was working fine but we have been moving over to the @Inject annotation as our preferred method of injecting beans.

What we found however was that when we changed one of our beans to use @Inject instead of @Resource then a large number of our tests began to fail with the exception:

Error creating bean with name 'myBean': Injection of autowired dependencies failed

Sample code:

@Component
public class MyClassImpl implements MyParentClass {
    @Resource
    private MyBean myBean;
}

Could anyone advise on situations where @Inject would not work but @Resource would?

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
mchinaloy
  • 1,444
  • 3
  • 25
  • 42
  • 2
    Try here:http://stackoverflow.com/questions/20450902/inject-and-resource-and-autowired-annotations – Patryk Dobrowolski Jan 07 '15 at 11:40
  • Thanks, I had a read of that post but from what I understand if the Resource annotation is working then surely the Inject should work also? It might just take a bit longer due to its different search path when locating the relevant bean? The error I receive seems to suggest its not finding any beans at all – mchinaloy Jan 07 '15 at 11:49
  • Could you please add the sample code where you have used @Resource annotation? – Mithun Jan 07 '15 at 12:20
  • `@Resource` and `@Inject` are different implementations and reside in different jars. If you don't have `@Inject` on the class path the annotation will be ignored and well injection will fail. Please add the full stack trace. – M. Deinum Jan 07 '15 at 12:26

0 Answers0