0

I have the following, but my @Autowired doesn't work for some reason because it remains set to null:

@Component
public class Face {
    public void smile();
}  

public class Parent {
    @Autowired
    protected Face face;
}

public abstract class AbstractParent extends Parent {
}

@Named
@Path("/parent/blueParent")
public class BlueParent extends AbstractParent {
    @POST
    @Consumes("application/json")
    @Produces("text/plain")
    public void describe() {
        // crash here because face is null when I curl to /parent/blueParent
        face.smile();
    }
}

Here is my app context

<?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-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.company.parent"/>
</beans>

Anyone know what may be the problem? Is it because @Autowired doesn't work with abstract classes?

Popcorn
  • 5,188
  • 12
  • 54
  • 87
  • 1
    It is because CDI and Spring beans are managed by different containers – Luiggi Mendoza Aug 27 '13 at 01:00
  • It would be helpful to know what you mean by "doesn't work". Is there an error? If so, which error? Is it an unexpected value? – David V Aug 27 '13 at 01:00
  • Sorry, I've edited the post. The problem is that it's never initialized, and ends up pointing to null. – Popcorn Aug 27 '13 at 01:02
  • 1
    Again, the problem is because Spring managed beans **are not** recognized by CDI managed beans because they are in different containers. Related: http://stackoverflow.com/q/4144039/1065197 – Luiggi Mendoza Aug 27 '13 at 01:02
  • @LuiggiMendoza I believe Spring can read `@Named` and `@Inject` just like it does `@Component` and `@Autowired`. [This](http://blog.springsource.com/2009/09/29/spring-framework-3-0-rc1-released/) specifies support for JSR-330. – Sotirios Delimanolis Aug 27 '13 at 01:20
  • @Popcorn Can you post your application context? – Sotirios Delimanolis Aug 27 '13 at 01:21
  • @SotiriosDelimanolis Yes, I've edited my original post. Thanks for the responses so far – Popcorn Aug 27 '13 at 01:29
  • @Popcorn Assuming all the classes above are in the `com.company.parent` package, you should have an instance of `BlueParent` with an injected instance of `Face` in your context. `3.0` should support CDI annotations, but just for kicks, try with a higher version of Spring. – Sotirios Delimanolis Aug 27 '13 at 01:33
  • Also, what is your REST provider? Do they have a spring integration module? – John Ament Aug 27 '13 at 22:41

0 Answers0