Spring Tutorial 05 - ApplicationContext and Property Initialization

Spring Tutorial 05 - ApplicationContext and Property Initialization

Introduction to Application Context in Spring

Overview of Application Context

  • The tutorial introduces the concept of ApplicationContext, which is an extension of the BeanFactory used for object instantiation in Spring.
  • Unlike BeanFactory, ApplicationContext offers additional functionalities such as event notification and Aspect-Oriented Programming (AOP).
  • It is recommended to use ApplicationContext over BeanFactory unless resources are critically limited due to similar performance costs.

Transitioning from Bean Factory to Application Context

  • The speaker demonstrates how to change code from using a BeanFactory to an ApplicationContext.
  • An example implementation, ClassPathXmlApplicationContext, allows loading XML configuration directly from the classpath without needing a file system resource.

Configuring Triangle Object with Spring

Setting Up Configuration

  • The tutorial discusses moving the XML configuration file (spring.xml) into the classpath for proper access during application runtime.
  • The speaker confirms successful instantiation of a triangle object after adjusting the configuration file location.

Enhancing Triangle Class Functionality

  • A member variable named "type" is introduced in the triangle class, allowing preset values during object instantiation.
  • By modifying the XML blueprint, users can prefill properties like "type" when creating objects through Spring.

Defining Properties in spring.xml

Blueprint Configuration

  • The speaker explains how to define properties within the XML configuration by adding property tags that specify names and values.
  • An example is provided where "type" is set as "equilateral triangle," demonstrating how Spring uses this information during object creation.

Resulting Behavior of Triangle Object

  • When requesting a triangle object, Spring will instantiate it according to defined properties, ensuring that preset values are automatically assigned.
  • Modifications made in methods allow for displaying predefined attributes; running this results in output confirming that an equilateral triangle has been drawn.
Playlists: Spring Framework
Video description

We'll first replace the BeanFactory with the more powerful ApplicationContext. We'll then make Spring initialize a property of an object by specifying the value in the configuration XML.