【发布时间】:2014-08-23 04:39:45
【问题描述】:
注意:问题已经解决,目前我无法发布答案,只是发布以通知罕见情况。见底部评论。谢谢
我正在尝试将一个 spring bean 作为托管属性注入到 JSF bean 中。 初始化 JSF Bean 时不会抛出异常。 但 ManagedProperty 为空。我在 setter 方法上放了一些日志,但似乎从未调用过它。但我可以通过@Postconstruct 方法获取 Spring bean:
WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
modelOperations = (ModelOperations) webAppContext.getBean("modelOperations");
PS:我知道有很多类似的帖子,花了一整天,但读完却筋疲力尽,最终不知道为什么它不起作用
环境:Spring 3.2.1,JSF:2.2.4,Eclipse 上的 Jetty
@ManagedBean(name = "batchOperation")
@SessionScoped
public class BatchOperation implements Serializable {
@ManagedProperty(value = "#{modelOperations}", name = "modelOperations")
ModelOperations modelOperations;
@PostConstruct
public void prepareLazyDataModel() {
..
List<XXX> list = modelOperations.getXXXList()
..
}
public ModelOperations getModelOperations() {
return modelOperations;
}
public void setModelOperations(ModelOperations modelOperations) {
this.modelOperations = modelOperations;
}
}
ModelOperations bean:
package xxxxx.dao;
@Component(value = "modelOperations")
@Scope("prototype")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, value = "transactionManager")
public class ModelOperationsImpl implements ModelOperations {
protected SessionFactory sessionFactory;
}
faces.config:
<?xml version="1.0" encoding="utf-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
applicationContext.xml:
<context:annotation-config/>
<context:component-scan base-package="xxxxx"
name-generator="xxxxx.util.WithoutTrailingImplBeanNameGenerator">
<context:include-filter type="regex"
expression="xxxxx.dao.*" />
</context:component-scan>
(如果有人想知道,请说) 没有TrailingImplBeanNameGenerator:
public class WithoutTrailingImplBeanNameGenerator extends
AnnotationBeanNameGenerator {
public String generateBeanName(BeanDefinition definition,
BeanDefinitionRegistry registry) {
String beanName = super.generateBeanName(definition, registry);
if (!beanName.endsWith("Impl")) {
return beanName;
}
return beanName.substring(0, beanName.length() - "Impl".length());
}
}
【问题讨论】:
-
我在发帖之前解决了这个问题。我只是在这里发布通知人们,因为我在互联网上没有看到类似的解释。这只是一个xsd版本问题。在我将 applicationContext 和 databaseContext xmls 上的 springs xsd 版本从 3.0 更新到 3.2 后,setter 被正常调用。
-
谢谢!顺便说一句,欢迎来到 SO。