-3

In a Java Web Project with Spring and JSF, I want do this: I want to have a service that in the first run of the app, reads a file and puts its data to a variable. then other classes can read that variable.

In fact I want that file reads one time and after that I just query the data, even web pages changed via links and navigation system.

Is there a Spring annotation to turn class to a service like this? Should I have some XML config files to specify a class as a service? I don't know what I have to do. What I know is that it can be done via Spring and I can get its data from JSF components, but how?

I have to do this based on MVC.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • Spring-mvc AND jsf? Are you sure? And java-se? – Kukeltje Oct 27 '15 at 22:44
  • @Kukeltje: this combination cant be done? Or its irrelevant? Actually im not sure but i searched and i think combination of these is nice. But i cant understand relationship of spring and jsf in a project. Right now i do my project with jsf but i dont know where can i put spring. Can you explain it to me? – Fcoder Oct 27 '15 at 23:02
  • 1
    Do a search in stackoverflow or google for the two tags. Easy to do. One article you'll find is this http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc – Kukeltje Oct 27 '15 at 23:08
  • The last statement makes this question genuinely confusing. Perhaps you're confusing Spring DI with Spring MVC? – BalusC Oct 28 '15 at 06:50

1 Answers1

0

You can have it lazy-loaded on first call of the service. Or you can add a ContextListener to initialize the service on startup of the web context, assuming you are deploying to a J2EE container. Or you can make that service a spring bean with an init-method that initializes the data. Or any one of a hundred different ways. You'll need to determine what works best for your application.

Here's a spring example:

<beans xmlns="http://www.springframework.org/schema/beans"
  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-2.5.xsd">

  <bean id="myService"
        class="org.your.Service" 
        init-method="initializeData"></bean>

</beans>

How you get hold of that service is up to you. You can either have the data stored statically on the class, or you can use the spring context to retrieve this single instance of the spring bean.

Spring documentation for lifecycle methods: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-lifecycle

jgitter
  • 3,396
  • 1
  • 19
  • 26