如何在Spring+JSF应用程序中使用JSF注释



我想在JSF ManagedBean中注入spring-bean。现在我使用:applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="service" class="com.evgeny.domain.TestService"></bean>
</beans>

face-config.xml:

<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_0.xsd"
    version="2.0">
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
    <managed-bean>
        <managed-bean-name>facesBean</managed-bean-name>
        <managed-bean-class>com.evgeny.jsf.FacesBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>service</property-name>
            <value>#{service}</value>
        </managed-property>
    </managed-bean>
</faces-config>

它是有效的。但是我想为JSF bean使用注释。那么,如何在@ManagedBean带注释的bean中注入TestService呢?

您可以使用@ManagedProperty

通过注入

@ManagedProperty(value="#{testService}")
private TestService testService;

将服务实现定义为使用注释

@Service(value = "testService")
public class TestServiceImpl implements TestService

正如您在xml文件中定义了bean一样,您可以用bean id 替换@ManagedProperty注入中的值

希望这能有所帮助!!!!

最新更新