我试图将一些豆子自动装配在一起(我已经创建了我试图完成的简化版本)但我不断被告知自动装配的bean是空的。
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>
然后在index.xhtml
Message:<h:outputText value="#{helloWorld.message}"/>
我不明白,我以为我做了我想做的一切。 web.xml声明了ContextLoaderListener。我尝试过使用CDI和其他Spring方法,并使用applicationContext.xml手动连接。 没什么......
我的猜测是我没有得到关于春天或注射的基本原理。 我使用的是Java 1.6,Spring 3.1.1和Tomcat 7.0.34.0
答案 0 :(得分:0)
尝试在<context:annotation-config />
之前将applicationContext.xml
添加到<context:component-scan>
以启用基于注释的Spring配置。请参阅Spring参考中的Annotation-based container configuration部分。
答案 1 :(得分:0)
除了提供的,要将Spring托管bean注入JSF托管bean,您必须能够将托管bean的解析委托给外部解析器。
检查web.xml
org.springframework.web.context.request.RequestContextListener
和
faces-config.xml
org.springframework.web.jsf.el.SpringBeanFacesELResolver
。
另见http://deevodavis.wordpress.com/injecting-spring-beans-into-jsf-2-managedbean-classes/