Spring

spring logo

– Spring IOC

in this topic im going to explain and introduce the basic works of the framework Spring .

firstly this is  the essencial  fonction of spring ioc :

1-1 create beans by the invocation of constructor

Before starting explaining the concept of this section we must reply to this question
Why I should use this solution and in which reason ?

Problem :

The problem that we need to create a bean on the spring IOC container by the invocation of her constructor. This method it’s the equivalent and direct one to create a bean without using the new operator for create java object.

Solution:

Whene we define the attribute class of a bean, we ask the spring ioc container to create an instance of this bean by the invocation of her constructor .

demonstration

supose that we develop a solution about an E-Commerce web site where we have a liste of products for sale , we start by  create a class product with the properties like the name and the price of products , as our example offer a liste of products , then our class product it’s an abstract class and the other subclasses of products will extend form it .

 

package com.apress.springrecipes.shop;
public abstract class Product {
private String name;
private double price;
public Product() {}
public Product(String name, double price) {
this.name = name;
this.price = price;
}
// Accesseurs et mutateurs.
...
public String toString() {
return name + " " + price;
}
}

after we going creat two subclasses of products : Batery and Disc and we add there own properties .

 

package com.apress.springrecipes.shop;
public class Battery extends Product {
private boolean rechargeable;
public Battery() {
super();
}
public Battery(String name, double price) {
super(name, price);
}
// Accesseurs et mutateurs.
...
}
  package com.apress.springrecipes.shop;
public class Disc extends Product {
private int capacity;
public Disc() {
super();
}
public Disc(String name, double price) {
super(name, price);
}
// Accesseurs et mutateurs.
...
}

the next beans config file define some product on spring IOC container :

<beans xmlns="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-2.5.xsd">
<bean id="aaa" class="com.apress.springrecipes.shop.Battery">
<property name="name" value="AAA" />
<property name="price" value="2.5" />
<property name="rechargeable" value="true" />
</bean>
<bean id="cdrw" class="com.apress.springrecipes.shop.Disc">
<property name="name" value="CD-RW" />
<property name="price" value="1.5" />
<property name="capacity" value="700" />
</bean>
</beans>

when no element <constructor-arg> is not specified , the default constructor without argument will be invoked . after , for any element <property > spring will injects a value by the help of the corresponding setter , the last configuration that we have seeing in the beans config file it’s equivalent to the following code :

Product aaa = new Battery();
aaa.setName("AAA");
aaa.setPrice(2.5);
aaa.setRechargeable(true);
Product cdrw = new Disc();
cdrw.setName("CD-RW");
cdrw.setPrice(1.5);
cdrw.setCapacity(700);

else , when one or more elements <constructor-arg > are defined , spring will choose the equivalent constructor with the corresponding arguments , for to be invoked .

<beans ...>
<bean id="aaa" class="com.apress.springrecipes.shop.Battery">
<constructor-arg value="AAA" />
<constructor-arg value="2.5" />
<property name="rechargeable" value="true" />
</bean>
<bean id="cdrw" class="com.apress.springrecipes.shop.Disc">
<constructor-arg value="CD-RW" />
<constructor-arg value="1.5" />
<property name="capacity" value="700" />
</bean>
</beans>

Since there is no ambiguity in the constructor of the Product class and subclasses,
the following configuration is equivalent to the following code:

Product aaa = new Battery("AAA", 2.5);
aaa.setRechargeable(true);
Product cdrw = new Disc("CD-RW", 1.5);
cdrw.setCapacity(700);

we write a main class for test our products by taking them from the Spring IoC container.

package com.apress.springrecipes.shop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
Product aaa = (Product) context.getBean("aaa");
Product cdrw = (Product) context.getBean("cdrw");
System.out.println(aaa);
System.out.println(cdrw);
}
}

1-2 create beans by the invocation of factory statique method

Problem :

we need to create a bean on the spring IOC container by the invoked of a factory statique method , with the rule to encapsulate the processus of creation of an object . the user who request an object he need to call just the method without knowing the detail of creation .

Solution :

spring enable to create a bean by the invoketion of the factrory statique method specified in the attribute
factory-method .

demonstration :

we can create for example a factory statique method createProduct() to create a product from an identifie of product,
on function of this identifie , the method will choose the concrete class to instanciate . if there’s any coresponding product to this identifie , an exception will be invoked IllegalArgumentException .

package com.apress.springrecipes.shop;
public class ProductCreator {
public static Product createProduct(String productId) {
if ("aaa".equals(productId)) {
return new Battery("AAA", 2.5);
} else if ("cdrw".equals(productId)) {
return new Disc("CD-RW", 1.5);
}
throw new IllegalArgumentException("Produit inconnu");
}
}

for declar a bean created by the help of a factory statique method ,we indicate the class that contain the method where the attribute class is the name of the method in the attribute factory-method .the arguments of the method passed we use the element <constructor-arg> .

<beans ...>
<bean id="aaa" class="com.apress.springrecipes.shop.ProductCreator"
factory-method="createProduct">
<constructor-arg value="aaa" />
</bean>
<bean id="cdrw" class="com.apress.springrecipes.shop.ProductCreator"
factory-method="createProduct">
<constructor-arg value="cdrw" />
</bean>
</beans>

if the factory method invoked an exception , spring envelope it in an exception BeanCreationException. the precedent configuration it’s equivalent of the next code :

Product aaa = ProductCreator.createProduct("aaa");
Product cdrw = ProductCreator.createProduct("cdrw");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s