我想从 faces-config.xml 初始化 bean 的属性(应用程序范围)。我尝试了不同的配置但没有成功。在库级别,我使用的是 jsf 2.2 - jboss-jsf-api_2.2_spec.jar。在项目级别 faces-config 配置为 2.0 版本。我不知道这是否是问题所在。JBDS 7 不允许我因为与其他 Project Facets 冲突而更改为 2.2。
这是 faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" 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_0.xsd">
<managed-bean>
<managed-bean-name>appBean</managed-bean-name>
<managed-bean-class>package.ApplicationBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
<managed-property>
<property-name>cookieNameLocale</property-name>
<property-class>java.lang.String</property-class>
<value>someText</value>
</managed-property>
<managed-property>
<property-name>debug</property-name>
<property-class>boolean</property-class>
<value>true</value>
</managed-property>
</managed-bean>
<application>
<locale-config>
<default-locale>xx_XX</default-locale>
<supported-locale>xx_XX</supported-locale>
</locale-config>
<resource-bundle>
<base-name>locale</base-name>
<var>i18n</var>
</resource-bundle>
</application>
</faces-config>
这是应用程序范围 bean:
public class ApplicationBean implements Serializable {
private boolean debug;
private String cookieNameLocale;
//respectively getters and setters
}
当 @Inject appBean 到另一个会话范围 bean 时,属性不会被初始化。没有错误并且在会话bean之前创建了appBean(使用@PostConstruct打印)