Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

How to use JSF annotations in Spring+JSF application

I'm want to inject spring bean in JSF ManagedBean. Now I use: 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>

...and it works. But I want to use annotations for the JSF beans. So how to inject TestService in @ManagedBean annotated bean?

Upvotes: 0

Views: 729

Answers (1)

baba.kabira
baba.kabira

Reputation: 3171

You can use @ManagedProperty

Inject it via

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

Define service Implementaion as if using annotation

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

As you have defined bean in xml file you can substitute value in @ManagedProperty injection by that bean id

Hope this helps !!!!

Upvotes: 1

Related Questions