使用注释在 JSF2 Bean 中注入问题 spring
Trouble spring injection in JSF2 Bean with annotation
我的 Web 项目中的 Spring 注入有问题。我必须在 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>
这个Spring配置在applicationContext.xml:
<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
我错过了什么吗?
我遇到的事情
1) 最好用注解提及服务名称 - @Service("sgbdService")
2) 与其使用@ManagedBean,不如使用@Qualifier 注解——
@Qualifier("sgbdBean")
3) 添加 <context:sping-configured/>
条目到 applicationContext.xml 文件
4) 尝试使用顶级组件扫描条目作为
<context:component-scan base-package="main.java.com.erdf" />
你有上下文冲突;在同一个 class 定义上同时使用 @Component
和 @ManagedBean
将您的 bean 置于两个上下文中:JSF 和 Spring。现在让我们确定 @Autowired
在 JSF 上下文中不起作用。
您可以摆脱基于 spring 的注释并使用以 JSF 为中心的设置。这会给你留下什么
@SessionScoped
@ManagedBean(name="sgbd")
public class SgbdBean {
@ManagedProperty(value="#{sgbdServiceImpl}")
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
您可以坚持严格以 spring 为中心的方法
@Component
public class SgbdBean {
@Autowired
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
我的 Web 项目中的 Spring 注入有问题。我必须在 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>
这个Spring配置在applicationContext.xml:
<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
我错过了什么吗?
我遇到的事情
1) 最好用注解提及服务名称 - @Service("sgbdService")
2) 与其使用@ManagedBean,不如使用@Qualifier 注解—— @Qualifier("sgbdBean")
3) 添加 <context:sping-configured/>
条目到 applicationContext.xml 文件
4) 尝试使用顶级组件扫描条目作为
<context:component-scan base-package="main.java.com.erdf" />
你有上下文冲突;在同一个 class 定义上同时使用 @Component
和 @ManagedBean
将您的 bean 置于两个上下文中:JSF 和 Spring。现在让我们确定 @Autowired
在 JSF 上下文中不起作用。
您可以摆脱基于 spring 的注释并使用以 JSF 为中心的设置。这会给你留下什么
@SessionScoped @ManagedBean(name="sgbd") public class SgbdBean { @ManagedProperty(value="#{sgbdServiceImpl}") SgbdService sgbdService; public List<Sgbd> findAll(){ return sgbdService.findAll(); }
}
您可以坚持严格以 spring 为中心的方法
@Component public class SgbdBean { @Autowired SgbdService sgbdService; public List<Sgbd> findAll(){ return sgbdService.findAll(); } }