Spring autowired injection fails for my annotated beans

I'm trying to autowire some beans together (I've created a simplified version of what I'm trying to accomplish) but I keep getting told the autowired bean is null.

Caused by: java.lang.NullPointerException
    at bean.HelloWorld.getMessage(HelloWorld.java:15)

TestDAO.java

package dao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
@Primary
@Repository("testDAO")
public class TestDAO {
    public String getMessage() {
        return "Hello World!";
    }
}

TestSOA.java

package soa;
import dao.TestDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("testSOA")
public class TestSOA {
    @Autowired
    private TestDAO testDAO;
    public String getMessage() {
        return testDAO.getMessage();
    }
}

HelloWorld.java

package bean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import soa.TestSOA;
@ManagedBean(name = "helloWorld")
@SessionScoped
@Controller
public class HelloWorld implements Serializable {
    @Autowired
    private TestSOA testSOA;
    public String getMessage() {
        return testSOA.getMessage();
    }
}

applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="dao, soa, bean"/>

</beans>

Then in index.xhtml

Message:<h:outputText value="#{helloWorld.message}"/>

I don't understand, I thought I did everything I was suppose to. web.xml has the ContextLoaderListener declared. I've tried using CDI and other Spring methods and manually wiring with applicationContext.xml. Nothing...

My guess is I'm not getting some fundamental principal concerning Spring or injection. I'm using Java 1.6, Spring 3.1.1, and Tomcat 7.0.34.0

Answers


Try adding <context:annotation-config /> to your applicationContext.xml before <context:component-scan> to enable annotation based Spring configuration. See Annotation-based container configuration section in Spring reference.


Besides the provided, to inject a Spring managed bean into JSF managed bean, you have to be able to delegate resolution of managed beans to an external resolver.

Check your web.xml for org.springframework.web.context.request.RequestContextListener and

faces-config.xml for org.springframework.web.jsf.el.SpringBeanFacesELResolver.

See also http://deevodavis.wordpress.com/injecting-spring-beans-into-jsf-2-managedbean-classes/


Need Your Help

Share Data in android app for all applications

android facebook android-intent mobile-application

How to Share Data(txt or image ....) in android for all applications

Getting Saved cursor position 2/2 out of range for (restored) text error in android?

android textview cursor-position

Can any body help me what is the problem i am facing. My application works fine, But some time it will show FORCE CLOSE exception.