0

I am using ehcache in my project so when server start data of few table will be loaded into the cache ..in my application i am using Spring,Hibernate,JSF I m using this configurationin applicationCOntext.xml file

<bean id="cacheManager"  class="com.ccc.service.cache.CacheManager" init-method="init">
        <property name="delay" value="${timer.delay}" />
    </bean>   
 <bean id="companyCache" class="com.ccc.service.cache.clients.ValidCacheClient"/>

        <context:component-scan base-package="com.ccc.spring" />
        <context:annotation-config />
        <context:spring-configured />

In Jsf Managed Bean i am creating Object of Service class like this

    @ManagedProperty(value = "#{GlobalDataService}")
    static GlobalDataService globalDataService;

But in ValidCacheClient.java how to create object of Service class? ValidCacheClient.java is not a manged class so how to create the Object of Service class?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
JavaBeigner
  • 608
  • 9
  • 28

1 Answers1

2

You have two options:

  • Inject the necessary beans to be known from JSF as ServletContext attributes, so these beans will be treat by JSF as application scoped attributes. You can do this using Spring ServletContextAttributeExporter:

    <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
        <property name="attributes">
            <map>
                <entry key="globalDataService" value-ref="GlobalDataService" />
            </map>
        </property>
    </bean>
    

    Then you can inject it without problems in JSF:

    @ManagedProperty(value = "#{globalDataService}")
    GlobalDataService globalDataService; //no need to be static
    
  • Let Spring container manage the lyfecycle of JSF managed beans. With this approach, you may inject the springs beans using @Autowired. This is covered in Spring 3 + JSF 2 tutorials. Still, note that if you do this, you will lose access to JSF 2 view scope (crucial when working with ajax requests in the same view) because Spring still cannot support it. But this can be solved by creating a custom implementation for view scope, like Cagatay's

IMO I would use the latter approach rather than the former.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I am ok with inbjecting service class into JSFManagedBean i want to inject it non managed bean class how to do it? – JavaBeigner May 17 '14 at 14:23
  • @askkuber read the tutorial I've posted. There are plenty tutorials on the net about it as well. Links in answers are not for decoration purpose, they're intended to add complementary info to the answer and to not reinvent the wheel for long explanations. – Luiggi Mendoza May 17 '14 at 14:24
  • @askkuber answer updated. Added more relevant info for the second approach. – Luiggi Mendoza May 17 '14 at 14:29