8

Now come directly to my point,

In JSP I will do the initialization process of my application like,

<%! public void jsp_init(){

      //Initialise the domain server to create protocol
      //Create the logging file
}%>

Now i am going to rebuild my previous application from Servlets to Spring 3.2 .

How I can I do this with Spring 3.2 ?

One of my colleague said me to do this initialization with Constructor of the Spring Controller .

Because I have created the bean for controller class in the applicationContext.xml and I am loading the applicationContext.xml file withe ContextLoadListner in web.xml .

Is this the right way of Initialization ?

What about ApplicationListener in spring ?

Which is the best way to initialize the application in spring 3.2 ?

Hope our stack users will give a good solution.

Human Being
  • 8,269
  • 28
  • 93
  • 136

5 Answers5

9

Spring will do a great deal of this for you if configured properly. If you really need to execute code (vs using something that will automatically configure itself like Log4J), I would suggest registering an InitializingBean and overriding afterPropertiesSet. You then would add this bean definition to the applicationContext.xml file:

<bean id="initializer" class="com.myproject.MyInitializer" />

As a result, Spring will invoke the MyInitializer.afterPropertiesSet() method when the application has been fully initialized. You alternatively could use the @PostConstruct annotation on a bean that has been registered with the application context, but there's no guarantee the rest of the application will be initialized when that method gets invoked. If you want it to run when everything has been set up, the Initializing Bean method is the way to go. I've used this strategy to start a server socket, etc that needed to run independently of the web request life cycle.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
6

Why would u initialize spring application by yourself? Spring will do automatically for you: This is how you tell your server to initialize spring:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/app.xml</param-value>
  </context-param>

  <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>

This will happen during deploymentand all beans are defined there will be initialized(depending on laziness). If you really want to do something once bean is initialized, before usage then use InitializingBean Example would be

MyBean implements InitializingBean{
   afterPropertiesSet() {

      //do here
   }

}
Elbek
  • 3,434
  • 6
  • 37
  • 49
  • It might not be a good idea to initialize the application through `ContextLoaderListener`. It is better to configure your `DispatcherServlet` and delegate specific tasks through `ContextLoaderListener` only where you need the context. – Risav Karna Jul 22 '13 at 12:41
  • 1
    Why it is bad idea? Spring framework itself comes with this listener and recommends you to use it to load it. `DispatcherServlet` is for mvc and mvc is less than 10% of framework itself, what if I do not want to use mvc? – Elbek Jul 23 '13 at 08:39
2

ContextLoaderListener is sort of bootstrapper to start up Spring's WebApplicationContext while ApplicationListener is more at the java application level itself rather than at the web application context.

ContextLoaderListener is a great and standard tool for the contextualization of your app if it has multiple DispatcherServlets or some servlets/servlet filters mapped to different services. Basically, it is handy to have listeners for different servlets of such apps so that you can have fine-grained contextualization.

I do not know the nature of the application you are building but I assume that you are trying something basic for now. If this is the case, and even in cases where you have a more complex setup, it is better to load on startup a controller that takes care of your main initialization routines including your contextualization. You can use the controller bean you have with something like this in your web.xml:

<servlet>
    <servlet-name>your-servlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

This servelt can also be mapped and invoked whenever you read a certain url pattern E.g.

@RequestMapping("/welcome")         
    public ModelAndView helloWorld() 

And in the web deployment descriptor, this bit is just like you map your servlets to other Spring services like Spring Security:

<servlet-mapping>
    <servlet-name>crunchify</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

So you don't really need to use the constructor of a controller and neither do you always need to use ContextLoaderListener or other listeners for simple initialization tasks. Nevertheless, it is handy to learn their use cases as you will need it when scaling your app.

Read more about dispatcher servlet here:

http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html

Risav Karna
  • 936
  • 8
  • 16
  • what if he does not want to use mvc, yet he wants to load spring? – Elbek Jul 23 '13 at 08:40
  • He says he has made a controller and is most probably going the mvc way already. See more details on this issue here [link](http://syntx.co/languages-frameworks/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/): "It is a best practice to keep a clear separation between middle-tier services such as business logic components and data access classes (that are typically defined in the ApplicationContext) and web- related components such as controllers and view resolvers (that are defined in the WebApplicationContext per Dispatcher Servlet)." – Risav Karna Jul 23 '13 at 12:22
0

You could do this by having a bean with @PostConstruct and injecting in in your servlet-config in spring. Take a look at the code here and one of the lines at the bottom.

<beans:bean id="PlayerImportDaoImpl"
        class="com.footieview.app.importer.dao.PlayerImportDaoImpl" />

This will inject this bean at startup a method on this bean has the annotation @PostConstruct - this means at startup this method gets called.

David
  • 19,577
  • 28
  • 108
  • 128
0

Create a Spring 3 MVC application and you do not need to do this!

Spring MVC supports JSPs and you can do all the configuration via Annotations

See http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/ for an example.

Ayub Malik
  • 2,488
  • 6
  • 27
  • 42