Spring Core

The core API in Spring is too short and it generally consists of Configuring, Creating and Making Associations between different Business Components. Spring relate to these Business Components as Beans. The core container provides the essential functionality of the spring framework.

A primary component of the core container is the BeanFactory, an implementation of the Factory pattern. The BeanFactory uses the Inversion of Control (IOC) pattern to separate an application’s configuration and dependency specification from the actual application code.

For achieving the goal Spring provides the following Core Classes or the Interfaces.

  1. Resource
  2. BeanFactory
  3. Sample Application

Resource

Resource is a kind of information’s that comes from File or from a Stream.Resources could represent an Xml File containing the various Configuration Information needed for a spring application or it could represent a Java Class File serving as a  Bean object.

Eg: – We want to load a resource that represent Java Class called MyJavaClass

Resource classRes =new ClassPathResource(“PathToClassFile”, MyJavaClass.class);

In the above example ClassPathResource is the solid implementation class for loading a Java Class file. Code for loads an Xml file from the local File System is,

Syntax: –

String xmlFile = "./resources/myXml.xml");
Resource xmlResource = new FileSystemResource(xmlFile);

FileSystemResourse is not only controlled to only Files it can be used to load any types of files to make themselves to the spring.

BeanFactory

As mentioned, in the spring of a Bean terminology refers to a business component into consideration. As such, BeanFactory is the factory class to create objects of beans. Interestingly, when the class definition should look BeanFactory Bean for bean instantiation? One thing important to note that all the beans reside in the context of the spring container is highly configurable via external files. This means that the bean definitions can reside in an XML file, a Java properties file or a database. While bean definitions are not tightly coupled to any format, most developers prefer their bean definitions in XML format. Code used for loading all Bean definition from an Xml file is,

Syntax: –

Resource xmlResource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(xmlResource);

XmlBeanFactory class is one of the concrete implementations of BeanFactory class.

Sample Application

For making a sample application before that we are accessing into other details like the various functionalities and features. That is it can be configured through Xml File. Let us keep the functionality of the Business Object to a minimal extent.

Business Object

The syntax used for the sample business Component Namer which is used to store the given name.

Syntax: –

Namer.java

package com.spring.introduction;
public class Namer {
   private String name;
    public Namer() {
    }  
    public String getName() {
        return name;
    }
   public void setName(String name) {
        this.name = name;
    }
}

This class has a single property called ‘name’ along with its appropriate getter and setter methods.

Xml Configuration File

The code used for the Xml Configuration File where we are going to define and configure the Bean class along with its properties.

Syntax: – namer.xml

<?xml version="1.0" encoding="UTF-8"?>
&lt;beans xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"
        xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
         xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>
         <a href="http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</a>"&gt;   
 &lt;bean id = "namerId" class = "com.spring.introduction.Namer"&gt;
        &lt;property name = "name"&gt;
            &lt;value&gt;J2EEbrain&lt;/value&gt;
        &lt;/property&gt;
    &lt;/bean&gt;&lt;/beans&gt;

The first thing to note that is this Xml follows the standard schema as defined in the new URL. The root element of the Xml file is the ‘beans’ element which represents the collection of Business objects to be defined. Every Business Object is identified by the ‘bean’ element which has two attributes name ‘id’ and ‘class’. The ‘id’ attributes should be unique in the Xml File and it can be used in code to refer the Bean class whereas the attribute ‘class’ represents the fully qualified class name of the Bean definition. Also note the property element which represents a single value ‘J2EEbrain’.

Client Application

The below client code helps to make a reference to the Xml File using the Resource object and then the contents of the Xml File. This file consists of different Bean definitions that are read using XmlBeanFactory calss. An instance of the object of type Namer is then retrieved by calling the BeanFactory.getBean(id) method. This id argument value corresponds to the ‘id’ attribute of the bean element defined in the Xml File.

Eg: – SimpleSpringApp.java

package com.spring.introduction;
import org.springframework.beans.factory.*;
Import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
public class SimpleSpringApp {
   public static void main(String args[]){
      Resource namerXmlFile = new FileSystemResource("src/resources/namer.xml");
      BeanFactory factory = new XmlBeanFactory(namerXmlFile);      
       Namer namer = (Namer)factory.getBean("namerId");
       System.out.println(namer.getName());
   }
}

Properties of Bean

Property tag: – When declaring a bean (but not use the constructor-arg) constructor is called with no arguments when the object is instance. You can use tags to specific <property…/> it setter methods are called after the constructor executes.

Syntax: –

<bean><property value="some-value"/></bean>

This means that when getBean is called the zero SomeClass constructor with no arguments is called, then setSomeProp called. Simple type conversions take place, so you can take setSomeProp String, int, Integer, Double, Double, etc.

Idea:- The bean definition file refers to a “bean property,” which is a shortcut to a setter method name.

  • The instance variable name is irrelevant

In general, it comes with the name of the bean property for falling “package” of the setter method name, then changing the next letter in lower case.

  • But if the first two letters after “set” are uppercase, Java assumes it is an acronym, and the bean property name does not start with a lowercase letter.

Eg:-

Setter Method Name Bean Property Name
setFirstName FirstName
setURL URL

ref property: – This property is used to refer to bean declared elsewhere.

Syntax :-

<bean …>…</bean>
<bean …>
<property ref="bean1"/>
</bean>

Nested <bean> property:- It helps to supply new bean as value

Syntax: –

<property>
<bean…>…</bean>
</property>

list property:- To pass a List to the setter method

Syntax: –

<property>
<list>…</list>
</property>

map property: – To pass a Map to the setter method.

Syntax:-

<property>
<map>…</map>
</property>