【发布时间】:2015-03-28 11:24:30
【问题描述】:
我的 Web 项目中的 Spring Injection 有问题。我必须在 JSF2 bean 中使用。
展示我的作品:
SgbdServiceImpl.java(短路)
@Service
public class SgbdServiceImpl implements SgbdService {
@Override
public List<Sgbd> findAll() {
return null;
}
@Override
public Sgbd findOneByName(String nom) {
return null;
}
}
SgbdBean
@Component
@SessionScoped
@ManagedBean(name="sgbd")
public class SgbdBean {
@Autowired
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
我把这个配置放在文件里:web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
applicationContext.xml中的这个Spring配置:
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="main.java.com.erdf.agir.services" />
</beans>
而且,在 faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
我想从服务中调用 findAll(),但我一直从 sgbdService 属性获取 nullPointerException(Autowired 失败?)
我按照这个例子:http://rsuna.blogspot.fr/2013/05/how-to-integrate-jsf-20-with-spring-3.html
我错过了什么吗?
【问题讨论】: