Spring Core API
The Core API in Spring is very limited and it generally involves in Configuring, Creating and Making Associations between various Business Components. Spring refers to these Business Components as Beans. The following are the Core Classes or the Interfaces that are available in the Spring for achieving the goal.
* Resource
* BeanFactory
Let us look into the above entities in brief.
1) Resource
A Resource in Spring represents any kind of Information that comes from a File or from a Stream. For example, resources could represent an Xml File containing the various Configuration Information needed for a Spring Application. Or it could represent a Java Class File representing a Bean object. Whatever be the case, it can be represented as a Resource object with the transparent nature of its implementation.
Suppose, we wish a load a Resource that represents the Java Class called MyJavaClass, then we can have,
Resource classRes = new ClassPathResource(“PathToClassFile”, MyJavaClass.class);
Note that ClassPathResource is the concrete implementation class for loading a Java Class file. The following code loads an Xml File from the local File System.
String xmlFile = “./resources/myXml.xml”);
Resource xmlResource = new FileSystemResource(xmlFile);
Note that FileSystemResource can be used to load any kind of files to make themselves available to the Spring Application, it is not restricted to only Xml Files. Other commonly used Resource in the InputStreamResource that loads content from an Input Stream. Apart from this, there are so many concrete implementations of various Resources are available in the Spring Framework.
2. BeanFactory
As mentioned, in Spring terminology a Bean refers to a Business Component in consideration. As such, BeanFactory is the factory class for creating Bean objects. The interesting thing is that how to configure the BeanFactory for creating Business Components. In other words, where the BeanFactory class should look for the Bean definition for creating Bean instances? One important thing to note that all Beans that reside in the Context of Spring Container are highly configurable through external files. It means that your Bean definitions can reside in an Xml File, a Java Property File or even in a database. Although, Bean definitions are not tightly coupled to any format, most developers prefer having their Bean definitions in Xml Format.
Following is the code that will load all Bean definition from an Xml File,
Resource xmlResource = new FileSystemResource(“beans.xml”);
BeanFactory factory = new XmlBeanFactory(xmlResource);
Note the XmlBeanFactory class which is one of the concrete implementations of BeanFactory class.
Sample Application
It is wise to look into a Sample Application before getting into the other details like the various functionalities and features that can be configured through the Xml File. Let us keep the functionality of the Business Object to a minimal extent.
Business Object
Following is the code for the sample Business Component Namer which is just used to store the given name.
Namer.java
package net.javabeat.articles.spring.introduction;
public class Namer {
private String name;
public Namer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Note that this class has a single property called ‘name’ along with its appropriate getter and setter methods.
Xml Configuration File
Now, let us look into the Xml Configuration File where we are going to define and configure the Bean class along with its properties. Following is the Xml code,
namer.xml
Javabeat
The very first thing to note that is this Xml follows the standard schema as defined in the url http://www.springframework.org/schema/beans/spring-beans-2.0.xsd. 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 singe value ‘Javabeat’.
5.3.3) Client Application
Following is the Client code which makes reference to the Xml File using the Resource object and then the contents of the Xml File (which contains the various Bean definitions) are read using the XmlBeanFactory class. An instance of the object of type Namer is then retrieved by calling the BeanFactory.getBean(id) method. Remember that, this id argument value corresponds to the ‘id’ attribute of the bean element defined in the Xml File.
SimpleSpringApp.java
package net.javabeat.articles.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());
}
}