Unit 5 Spring Framework in Java
Unit 5 Spring Framework in Java
The Spring framework comprises several modules such as IOC, AOP, DAO, Context,
ORM, WEB MVC etc.
class Employee{
1. Address address;
2. Employee(){
3. address=new Address();
4. }
5. }
In such case, there is dependency between the Employee and Address (tight
coupling). In the Inversion of Control scenario, we do this something like this:
1. class Employee{
2. Address address;
3. Employee(Address address){
4. this.address=address;
5. }
6. }
Thus, IOC makes the code loosely coupled. In such case, there is no need to modify
the code if our logic is moved to new environment.
1) Predefined Templates
Spring framework provides templates for JDBC, Hibernate, JPA etc. technologies. So
there is no need to write too much code. It hides the basic steps of these
technologies.
2) Loose Coupling
3) Easy to test
The Dependency Injection makes easier to test the application. The EJB or Struts
application require server to run the application but Spring framework doesn't
require server.
4) Lightweight
5) Fast Development
6) Powerful abstraction
It provides powerful abstraction to JavaEE specifications such as JMS, JDBC, JPA and
JTA.
7) Declarative support
1. package com.ucer;
2.
3. public class Student {
4. private String name;
5.
6. public String getName() {
7. return name;
8. }
9.
10. public void setName(String name) {
11. this.name = name;
12. }
13.
14. public void displayInfo(){
15. System.out.println("Hello: "+name);
16. }
17. }
2) Create the xml file
In case of myeclipse IDE, you don't need to create the xml file as myeclipse does this
for yourselves. Open the applicationContext.xml file, and write the following code:
1. package com.ucer;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.ClassPathResource;
6. import org.springframework.core.io.Resource;
7.
8. public class Test {
9. public static void main(String[] args) {
10. Resource resource=new ClassPathResource("applicationContext.xml");
11. BeanFactory factory=new XmlBeanFactory(resource);
12.
13. Student student=(Student)factory.getBean("studentbean");
14. student.displayInfo();
15. }
16. }
o org.springframework.core-3.0.1.RELEASE-A
o com.springsource.org.apache.commons.logging-1.1.1
o org.springframework.beans-3.0.1.RELEASE-A
IoC Container
The IoC container is responsible to instantiate, configure and assemble the objects.
The IoC container gets informations from the XML file and works accordingly. The
main tasks performed by IoC container are:
1. BeanFactory
2. ApplicationContext
Using BeanFactory
The XmlBeanFactory is the implementation class for the BeanFactory interface. To use
the BeanFactory, we need to create the instance of XmlBeanFactory class as given
below:
Using ApplicationContext
The ClassPathXmlApplicationContext class is the implementation class of
ApplicationContext interface. We need to instantiate the
ClassPathXmlApplicationContext class to use the ApplicationContext as given below:
1. ApplicationContext context =
2. new ClassPathXmlApplicationContext("applicationContext.xml");
o tight coupling The dependency lookup approach makes the code tightly
coupled. If resource is changed, we need to perform a lot of modification in
the code.
o Not easy for testing This approach creates a lot of problems while testing the
application especially in black box testing.
Dependency Injection
The Dependency Injection is a design pattern that removes the dependency of the
programs. In such case we provide the information from the external source such as
XML file. It makes our code loosely coupled and easier for testing. In such case we
write the code as:
1. class Employee{
2. Address address;
3.
4. Employee(Address address){
5. this.address=address;
6. }
7. public void setAddress(Address address){
8. this.address=address;
9. }
10.
11. }
In such case, instance of Address class is provided by external souce such as XML file
either by constructor or setter method.
Two ways to perform Dependency Injection in Spring
framework
o By Constructor
o By Setter method
o Employee.java
o applicationContext.xml
o Test.java
1. package com.ucer;
2.
3. public class Employee {
4. private int id;
5. private String name;
6. private String city;
7.
8. public int getId() {
9. return id;
10. }
11. public void setId(int id) {
12. this.id = id;
13. }
14. public String getName() {
15. return name;
16. }
17. public void setName(String name) {
18. this.name = name;
19. }
20.
21. public String getCity() {
22. return city;
23. }
24. public void setCity(String city) {
25. this.city = city;
26. }
27. void display(){
28. System.out.println(id+" "+name+" "+city);
29. }
30.
31. }
applicationContext.xml
Test.java
1. package com.ucer;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.*;
6.
7. public class Test {
8. public static void main(String[] args) {
9.
10. Resource r=new ClassPathResource("applicationContext.xml");
11. BeanFactory factory=new XmlBeanFactory(r);
12.
13. Employee e=(Employee)factory.getBean("obj");
14. s.display();
15.
16. }
17. }
o Employee.java
o applicationContext.xml
o Test.java
Employee.java
1. package com.ucer;
2.
3. public class Employee {
4. private int id;
5. private String name;
6.
7. public Employee() {System.out.println("def cons");}
8.
9. public Employee(int id) {this.id = id;}
10.
11. public Employee(String name) { this.name = name;}
12.
13. public Employee(int id, String name) {
14. this.id = id;
15. this.name = name;
16. }
17.
18. void show(){
19. System.out.println(id+" "+name);
20. }
21.
22. }
applicationContext.xml
Test.java
1. package com.ucer;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.*;
6.
7. public class Test {
8. public static void main(String[] args) {
9.
10. Resource r=new ClassPathResource("applicationContext.xml");
11. BeanFactory factory=new XmlBeanFactory(r);
12.
13. Employee s=(Employee)factory.getBean("e");
14. s.show();
15.
16. }
17. }
Constructor Injection with Dependent
Object
Address.java
1. package com.ucer;
2.
3. public class Address {
4. private String city;
5. private String state;
6. private String country;
7.
8. public Address(String city, String state, String country) {
9. super();
10. this.city = city;
11. this.state = state;
12. this.country = country;
13. }
14.
15. public String toString(){
16. return city+" "+state+" "+country;
17. }
18. }
Employee.java
1. package com.ucer;
2.
3. public class Employee {
4. private int id;
5. private String name;
6. private Address address;//Aggregation
7.
8. public Employee() {System.out.println("def cons");}
9.
10. public Employee(int id, String name, Address address) {
11. super();
12. this.id = id;
13. this.name = name;
14. this.address = address;
15. }
16.
17. void show(){
18. System.out.println(id+" "+name);
19. System.out.println(address.toString());
20. }
21.
22. }
Spring.xml
Test.java
1. package com.ucer;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.*;
6.
7. public class Test {
8. public static void main(String[] args) {
9.
10. Resource r=new ClassPathResource("spring.xml");
11. BeanFactory factory=new XmlBeanFactory(r);
12.
13. Employee s=(Employee)factory.getBean("e");
14. s.show();
15.
16. }
17. }
1. package com.ucer;
2.
3. import java.util.Iterator;
4. import java.util.List;
5.
6. public class Question {
7. private int id;
8. private String name;
9. private List<Answer> answers;
10.
11. public Question() {}
12. public Question(int id, String name, List<Answer> answers) {
13. super();
14. this.id = id;
15. this.name = name;
16. this.answers = answers;
17. }
18.
19. public void displayInfo(){
20. System.out.println(id+" "+name);
21. System.out.println("answers are:");
22. Iterator<Answer> itr=answers.iterator();
23. while(itr.hasNext()){
24. System.out.println(itr.next());
25. }
26. }
27.
28. }
Answer.java
1. package com.ucer;
2.
3. public class Answer {
4. private int id;
5. private String name;
6. private String by;
7.
8. public Answer() {}
9. public Answer(int id, String name, String by) {
10. super();
11. this.id = id;
12. this.name = name;
13. this.by = by;
14. }
15.
16. public String toString(){
17. return id+" "+name+" "+by;
18. }
19. }
applicationContext.xml
1. package com.ucer;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.ClassPathResource;
6. import org.springframework.core.io.Resource;
7.
8. public class Test {
9. public static void main(String[] args) {
10. Resource r=new ClassPathResource("applicationContext.xml");
11. BeanFactory factory=new XmlBeanFactory(r);
12.
13. Question q=(Question)factory.getBean("q");
14. q.displayInfo();
15.
16. }
17. }
1. package com.ucer;
2. import java.util.Iterator;
3. import java.util.Map;
4. import java.util.Set;
5. import java.util.Map.Entry;
6.
7. public class Question {
8. private int id;
9. private String name;
10. private Map<String,String> answers;
11.
12. public Question() {}
13. public Question(int id, String name, Map<String, String> answers) {
14. super();
15. this.id = id;
16. this.name = name;
17. this.answers = answers;
18. }
19.
20. public void displayInfo(){
21. System.out.println("question id:"+id);
22. System.out.println("question name:"+name);
23. System.out.println("Answers....");
24. Set<Entry<String, String>> set=answers.entrySet();
25. Iterator<Entry<String, String>> itr=set.iterator();
26. while(itr.hasNext()){
27. Entry<String,String> entry=itr.next();
28. System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValu
e());
29. }
30. }
31. }
applicationContext.xml
1. package com.ucer;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.ClassPathResource;
6. import org.springframework.core.io.Resource;
7.
8. public class Test {
9. public static void main(String[] args) {
10. Resource r=new ClassPathResource("applicationContext.xml");
11. BeanFactory factory=new XmlBeanFactory(r);
12.
13. Question q=(Question)factory.getBean("q");
14. q.displayInfo();
15.
16. }
17. }
Autowiring in Spring
Autowiring feature of spring framework enables you to inject the object dependency
implicitly. It internally uses setter or constructor injection.
Autowiring can't be used to inject primitive and string values. It works with reference
only.
Advantage of Autowiring
It requires the less code because we don't need to write the code to inject the
dependency explicitly.
Disadvantage of Autowiring
No control of programmer.
Autowiring Modes
There are many autowiring modes:
2) byName The byName mode injects the object dependency according to name of
the bean. In such case, property name and bean name must be same. It
internally calls setter method.
3) byType The byType mode injects the object dependency according to type. So
property name and bean name can be different. It internally calls setter
method.
4) constructor The constructor mode injects the dependency by calling the constructor
of the class. It calls the constructor having large number of parameters.
5) autodetect It is deprecated since Spring 3.
Example of Autowiring
B.java
1. package org.ucer;
2. public class B {
3. B(){System.out.println("b is created");}
4. void print(){System.out.println("hello b");}
5. }
A.java
1. package org.ucer;
2. public class A {
3. B b;
4. A(){System.out.println("a is created");}
5. public B getB() {
6. return b;
7. }
8. public void setB(B b) {
9. this.b = b;
10. }
11. void print(){System.out.println("hello a");}
12. void display(){
13. print();
14. b.print();
15. }
16. }
applicationContext.xml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans
7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
8.
9. <bean id="b" class="org.ucer.B"></bean>
10. <bean id="a" class="org.ucer.A" autowire="byName"></bean>
11.
12. </beans>
Test.java
1. package org.ucer;
2. import org.springframework.context.ApplicationContext;
3. import org.springframework.context.support.ClassPathXmlApplicationContext;
Output:
b is created
a is created
hello a
hello b