Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

AbstractApplicationContext in Spring Framework

In this article, we will learn what is AbstractApplicationContext interface and why do we use this. We will also see how do use AbstractApplicationContext interface in spring framework.

Sometimes it is necessary to manage the initialize and destroy of a spring bean, when a spring bean is being initialized or destroyed. There are set of interfaces provided by spring framework that defines methods to manage initialization and destroy of a spring bean. We can also define our own methods that can manage the initialize and destroy of a spring bean. In this way, we are required to define these methods into spring configuration bean. These methods are accessed using AbstractApplicationContext interface, while a spring bean container is being initialized.

AbstractApplicationContext Interface in Detail:

AbstractApplicationContext interface reads spring configuration xml file and gets the information about the bean initialize and bean destroy using spring interfaces or using methods. Let’s go with an example that will list two interfaces to access the bean initialize and destroy information.

Listing 1: Triangle.java

	packagecom.spring.core;
	
	importorg.springframework.beans.factory.DisposableBean;
	importorg.springframework.beans.factory.InitializingBean;
	
	publicclass Triangle implementsInitializingBean, DisposableBean {
	
		private Point a;
		private Point b;
		private Point c;
		
	
		public Point getA() {
			returna;
		}
	
		publicvoidsetA(Point a) {
			this.a = a;
		}
	
		public Point getB() {
			returnb;
		}
	
		publicvoidsetB(Point b) {
			this.b = b;
		}
	
		public Point getC() {
			returnc;
		}
	
		publicvoidsetC(Point c) {
			this.c = c;
		}
		
		publicvoid draw(){
			System.out.println("Point (" + a.getX() + "," + a.getY() + ")");
			System.out.println("Point (" + b.getX() + "," + b.getY() + ")");
			System.out.println("Point (" + c.getX() + "," + c.getY() + ")");
		}
	
			@Override
		publicvoidafterPropertiesSet() throws Exception {
			System.out.println("This is from initializing method!!");
		}
			
		@Override
		publicvoid destroy() throws Exception {
			System.out.println("This is from disposable method!!");
		}
		
	}
	

Listing 1 defines a java bean class Triangle.java that defines three Point class member variables to display coordinates into the console. The class member variables a, b and c defines their getter() and setter() methods. This class also defines a draw() method that prints three triangle coordinates into the console. This class implements two interfaces “InitializingBean” and “DisposableBean” that overrides two methods afterPropertiesSet() and destroy(). The afterPropertiesSet() methods is called, when a bean is being initialized and the destroy() methods is being initialized, when a bean is destroyed. So, If there is any kind of configuration settings to be configured before the bean loaded, the method afterPropertiesSet() method will be helpful and if there are few instruction to be executed, when bean is being unloaded. These instructions may be placed into the method destroy().

Now, let’s define another bean class Points.java into Listing 2 as:

Listing 2: Points.java

	packagecom.spring.core;
	
	publicclass Points {
		privateintx;
		privateinty;
		
		publicintgetX() {
			returnx;
		}
		publicvoidsetX(int x) {
			this.x = x;
		}
		publicintgetY() {
			returny;
		}
		publicvoidsetY(int y) {
			this.y = y;
		}
		
		
	}
	

Listing 2 defines a java class Points.java that defines two integer type class member variables with its getter() and setter() methods.

Now let’s define the spring configuration file that defines the bean definitions to initialize the bean class member variables into Listing 3 as:

Listing 3: SpringBeans.xml

	<beansxmlns="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-3.0.xsd">
	
		<beanid="helloBean"class="com.spring.core.Triangle">
			<propertyname="a"ref="pointA"/>
			<propertyname="b"ref="pointB"/>
			<propertyname="c"ref="pointC"/>
		</bean>
		
	
		<beanid="pointA"class="com.spring.core.Points">
			<propertyname="x"value="0"/>
			<propertyname="y"value="0"/>
		</bean>
		<beanid="pointB"class="com.spring.core.Points">
			<propertyname="x"value="20"/>
			<propertyname="y"value="0"/>
		</bean>
		<beanid="pointC"class="com.spring.core.Points">
			<propertyname="x"value="-20"/>
			<propertyname="y"value="0"/>
		</bean>
	</beans>
	

Listing 3 defines four bean definitions, where one if for Triangle bean class and three are for Points bean class. Points bean class definitions are referenced to Triangle bean class.

To create an instance of Triangle bean class, a java application class MainApp.java class is defined into Listing 4:

Listing 4: MainApp.java

	packagecom.spring.core;
	
	importorg.springframework.context.support.AbstractApplicationContext;
	import org.springframework.context.support.ClassPathXmlApplicationContext;
	
	publicclassMainApp {
		publicstaticvoid main(String[] args) {
			AbstractApplicationContext context = newClassPathXmlApplicationContext(
					"SpringBeans.xml");
	
			Triangle obj = (Triangle) context.getBean("helloBean");
			obj.draw();
		}
	}
	

Listing 4 defines MainApp.java class that defines a main() method. This method creates an instance of AbstractApplicatinoContext class that loads “SpringBean.xml” configuration file. The next instruction creates an instance “obj” of Triangle class using context.getBean() method. The “obj” instance calls the method draw() that displays the initialized triangle points into the console as shows into Figure 1:

Three triangle points displayed into Console

Figure 1: Three triangle points displayed into Console

Figure 1 shows three triangle points into console, while MainApp class is executed.

AbstractApplicationContext class reads the set of instructions defined into methods afterPropertiesSet() and destroyed() methods.

We can also define our own methods that will be called before bean initialization and after a bean is destroyed. These methods need to be defined into the spring configuration file, so that spring container can be informed about these methods.

There are set of attributes to set that notify the spring container about the methods that needs to be loaded before the bean initialize and dispose. Attributes init-method and destroy-method should be defined into the <bean> tag or we can also define common methods to a set of beans by defining attributes default-init-method and default-destroy-method into <beans> tag.

In this way, we don’t need “InitializingBean” and “DisposableBean” interfaces anymore as listed into Listing 5 and Listing 6:

Listing 5: Triangle.java

	packagecom.spring.core;
	
	
	publicclass Triangle /*implements InitializingBean, DisposableBean*/ {
	
		private Points a;
		private Points b;
		private Points c;
		
	
		public Points getA() {
			returna;
		}
	
		publicvoidsetA(Points a) {
			this.a = a;
		}
	
		public Points getB() {
			returnb;
		}
	
		publicvoidsetB(Points b) {
			this.b = b;
		}
	
		public Points getC() {
			returnc;
		}
	
		publicvoidsetC(Points c) {
			this.c = c;
		}
		
		publicvoid draw(){
			System.out.println("Point (" + a.getX() + "," + a.getY() + ")");
			System.out.println("Point (" + b.getX() + "," + b.getY() + ")");
			System.out.println("Point (" + c.getX() + "," + c.getY() + ")");
			System.out.println("This is from disposable method!!");
		}
	
		/*@Override
		public void afterPropertiesSet() throws Exception {
			System.out.println("This is from initializing method!!");
		}
			
		@Override
		public void destroy() throws Exception {
			System.out.println("This is from disposable method!!");
		}*/
		
		publicvoidmyAfterPropertiesSet() throws Exception {
			System.out.println("This is from initializing method!!");
		}
			
		publicvoidmyDestroy() throws Exception {
			System.out.println("This is from disposable method!!");
		}
	}
	

Listing 5 re-define Triangle.java class to define own bean initializing and destroying methods.

Listing 6: SpringBeans.xml

	<beansxmlns="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-3.0.xsd">
	
		<beanid="helloBean"class="com.spring.core.Triangle"init-method="myAfterPropertiesSet"destroy-method="myDestroy">
			<propertyname="a"ref="pointA"/>
			<propertyname="b"ref="pointB"/>
			<propertyname="c"ref="pointC"/>
		</bean>
		
	
		<beanid="pointA"class="com.spring.core.Points">
			<propertyname="x"value="0"/>
			<propertyname="y"value="0"/>
		</bean>
		<beanid="pointB"class="com.spring.core.Points">
			<propertyname="x"value="20"/>
			<propertyname="y"value="0"/>
		</bean>
		<beanid="pointC"class="com.spring.core.Points">
			<propertyname="x"value="-20"/>
			<propertyname="y"value="0"/>
		</bean>
	</beans>
	

And Listing 6 re-defines SpringBeans.xml file to define own bean initialization and destroying methods.

To understand the flow of bean container initialization flow for bean initialization and disposable interfaces, let’s have a look into Figure 2:

Calling of initializing methods and disposable methods

Figure 2: Calling of initializing methods and disposable methods

Spring container calls the interface initializing method first, then own initializing method is called. In the same way, the interface destroy method is called, then own disposable method is called.

Required Tools & Application:

We are required listed tools and application to run above listed example:

  • Eclipse IDE (Eclipse JUNO)
  • JDK Run time environment (JDK 1.7)
  • Spring Jar Files (Spring 3.2 Shown in figure 3)
Set of RequiredSpring jar files

Figure 3: Set of RequiredSpring jar files

Conclusion:

In this article,we learn about the use of AbstractApplicationContext abstract class and InitializingBean and DisposableBean interfaces. We also went through an example that helps us to understand the flow of initializing and destroying methods. We also learn about the way of defining our own initializing and disposing methods.



Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login