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.
From the console output it's clear that Spring Context is first using no-args constructor to initialize the bean object and then calling the post-init method. The order of bean initialization is same as it's defined in the spring bean configuration file.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With