2

This specific class's member private fields which are annotated @autowired are within a service JAR, and when I am trying to get an instance of the class within my code, the @autowired member is always coming back as null. I have the below component-scan statements within my project's context.xml, which is trying to lookup the base-package within the JAR, but still it appears nulls are being injected into the annotated members. The JAR internally also has a context.xml which has the same component-scan entries as below. The JAR source code cannot be changed now, is there anything I am missing?

<!-- local WEB-INF/spring-context/spring-application-context.xml -->
<context:annotation-config /> 
<context:component-scan base-package="blah.blah" />

//this code within my project
//wjc.getMethod() dereferencing comes back null
 public class A{
    WithinJARInterface wjc = new WithinJARInterfaceImpl()
    List someObject = wjc.getMethod()
 }

 //this code interface within JAR
 public interface WithinJARInterface{
    public List getMethod() throws Exception(); 
 }

 //this code within JAR
 public class WithinJARInterfaceImpl implements WithinJARInterface{

    //below member always comes back NULL
    @Autowired
    private JARService jService;

    public List getMethod(){.....}

 }

 public interface JARService{
    public List method1();
 }

  @Service("JARService") 
   public class JARServiceImpl implments JARService {
     public List method1(){ }
  }
AKSM
  • 327
  • 7
  • 20
  • 2
    How do you obtain your object that has `null` fields annotated with `@Autowired`? Show the code. – Tomasz Nurkiewicz May 08 '12 at 16:05
  • Maybe it is only a typo in the question, but the annotation is `@Autowired` (uppercase A). -- If this is not a typo in the question, then its likely to be the root of your problem. – Ralph May 08 '12 at 16:46

2 Answers2

7

You are constructing the WithinJARInterfaceImpl yourself (by calling new)so it isn't managed by spring so the values won't be injected.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
0

Did you try replacing @Autowired by @Resource? I think @Autowired wires by type while @Resource injects by bean name.

kyiu
  • 1,926
  • 1
  • 24
  • 30