iForeRunner
 
   
 
  JAVA  
   
   
   
 
  Java|J 2EE| Spring FrameWork  
 
 


Important packages in Springs

the important packages in springs are org.springsframework.beans and the org.springsframework.context packages. The BeanFactory package provides advanced configuration mechanism for handling beans of any nature and any storage facility . The ApplicationContext package sits on top of the BeanFactory and enhances the capabilities of the BeanFactory . The specific of these we would know as we dig in detail. These packages provide the Inversion of Control or also called and Dependancy Injection.

Dependancy injection

Dependancy injection is a design pattern which is used to abstract the provider from the class using it. Data Access is a classic example of this.

for example this is how an actual code would look for data access

public class A{

A(){
Connection con;
....
....
....
statements to connect to the database provider and then initiate the connection object CON
}

public resultset getdata(){

.....
.....
statements to execute a query and return the data read from the db .
}

the calling class would create an object of class A and call getdata method


but now with Dependancy Injection pattern being used my class would look like this ..

public class A{

A( Connection con){

this.con = con;
}

public resultset getdata(){

...
...
statements to execute a query and return data
}

now the calling calss would create the connection object and also the database provider handle and then just pass the connection object to the object of class A.


Sometimes the user is in a dilemma to use BeanFactory or the Aplication context , in J2EE environment ApplicationContext is the best one as it has the functionality of BeanFactory and other features and also has a more declarative approach. The BeanFactory can be used only when the memory managent is a primary concern.


BeanFactory and Bean Definitions

The BeanFactory [http://www.springframework.org/docs/api/org/springframework/beans/factory/BeanFactory.html] is the actual container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another, and thus have dependencies between themselves. These dependencies are
reflected in the configuration data used by the BeanFactory . A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, for which
there are multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory. an instance of this can be instantiated as follows ..

InputStream is = new FileInputStream("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);

A BeanFactory configuration consists of, at its most basic level, definitions of one or more beans that the BeanFactory must manage. In an XmlBeanFactory, these are configured as one or more bean elements inside a top-level beans element. The config file template would look like this.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="..." class="...">
...
</bean>
<bean id="..." class="...">
...
</bean>
...
</beans>


Bean Definition

- ClassName ie the name of the bean class with the implementation. This attribute is mandatory
- bean behavioural configuration details which would state how the bean should behave in the container , singleton , prototype ...etc
- Constructor arguements
- dependancies.


Bean Class

usually the BeanFactory itself creates the bean by calling its constructor.

- an example of specifying a bean class and hence using the bean constructor

<bean id="exampleBean" class="example.ExampleBean"/>

- an example of specyfying a bean class and using factory method to create the bean

<bean id="exampleBean" class="example.ExampleBean" factory-method="createInstance"/>

note that the method createInstance should be a static method.

- an example of specifying a bean class using instance factory method

<bean id="exampleFactoryBean" class="..."/>
<bean id="exampleBean" factory-bean="exampleFactoryBean" factory-method="createInstance"/>

Note that here in this example the second definition does not require the class attribute as the bean define would have a class whih would contain the create
instance method

Every bean has a single or multiple ids . mutiple ids would denote the aliases as usually there should be one id specified . name attribute can also be used for the same. but there should be one id specified atleast.

The beans can be deployed in two ways , singleton and prototype. with singleton specified there would be only one instance of the bean that is created and the handle of the same is returned to all the ids that which mathc this bean definition. where as the in prototype mode there would a new instance of the bean that is created.usualyy beans are created in singeton mode. an example of this can be given as

<bean id="exampleBean" class="Example.ExampleBean" singelton="false"/> for prototype mode
<bean id="exampleBean" class="Example.ExampleBean" singelton="true"/> for singleton mode