
Create/Destroy the Beans Globally
All the complex applications use the design pattern these days. So if any one want to use the same createInstance method for all the beans creations in the application, then he needs to unnecessary copy the same factory-method="createInstance" in all the bean definitions. Spring provide a better solution for the same. You can use "default-destroy-method" and "default-init-method" attributes of the <beans> tag. The below example will explain the use of both attributes. You can also override the method names for some beans if you want by introducing the factory-method method and it is still valid.
Example To use the Factory method Instantiation in Spring:-
TestBean.java
package com.visualbuilder.beans; public class TestBean { |
Spring-beans.xml
<?xml version="1.0" encoding="UTF-8"?> </beans> |
Output:- On running the ApplicationContextExample.java file we got the following output.
Jun 1, 2008 1:49:48 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Jun 01 13:49:48 IST 2008]; root of context hierarchy
Jun 1, 2008 1:49:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]
Bean is created
Jun 1, 2008 1:49:48 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9
Jun 1, 2008 1:49:48 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9: defining beans [test]; root of factory hierarchy
Bean has been called
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin



