Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization through Spring

 
Saptarshi Chakraborty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new in Spring framework. I want to initialize a process using spring framework.
Typically this can be done by overriding init() method in servlet and making the <load-on-startup>1</load-on-startup> in web.xml .I want to use spring as it provides DI features through which I can initialize the dependent objects in time of initialization.
Please guide me how this can be done by using spring. What should be the servlet class in web.xml.


 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:-

<beans>
<bean id="..." class="..." init-method="..."/>
</beans>

This will call the 'init-method' method when the bean is instantiated.

Another way is to implement InitializingBean available along in the Spring framework and overload afterPropertiesSet() to perform custom initialization.

Raj.
 
Saptarshi Chakraborty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raj.
but what to write inside web.xml as <servlet-class> ?
There should be a spring servlet (like org.springframework.web.servlet.DispatcherServlet when I am using spring MVC) which will initialize the other objects through IOC.
 
Saptarshi Chakraborty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raj.
But anything to write inside web.xml as <servlet-class> (like org.springframework.web.servlet.DispatcherServlet when we use spring MVC) .
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<servlet>
<servlet-name>.....</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>.....</param-value>
</init-param>
</servlet>
 
Saptarshi Chakraborty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use ContextLoaderServlet instead of DispatcherServlet ?
I think ContextLoaderListener or ContextLoaderServlet should be used if I want the objects to be visible application wide.The code above will initialize objects for that specific servlet.
Am I right ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic