Module 10 - Spring Container-Upd
Module 10 - Spring Container-Upd
© 2014 Time2Master 2
A basic Spring application Create an
ApplicationContext
package module2.helloworld; based on
import org.springframework.context.ApplicationContext; springconfig.xml
import org.springframework.context.support.ClassPathXmlApplicationContext;
© 2014 Time2Master 3
Spring Container
© 2014 Time2Master 4
The spring ApplicationContext
Reads the Spring XML configuration file
Instantiates objects declared in the Spring
configuration file
Wires objects together with dependency
injection
Creates proxy objects when needed
© 2014 Time2Master 5
Creation of the ApplicationContext
© 2014 Time2Master 6
Spring Container
SPRING BEANS
© 2014 Time2Master 7
Spring beans are default singletons
public class Application{
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("module2/singleton/springconfig.xml");
CustomerService customerService1 = context.getBean("customerService", CustomerService.class);
CustomerService customerService2 = context.getBean("customerService", CustomerService.class);
System.out.println("customerService1 ="+ customerService1);
System.out.println("customerService2 ="+ customerService2);
}
}
customerService1 =module2.singleton.CustomerService@29e357
customerService2 =module2.singleton.CustomerService@29e357
customerService1
Applicationprototype CustomerService
customerService2
© 2014 Time2Master 8
Prototype beans
public class Application{
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("module2/prototype/springconfig.xml");
CustomerService customerService1 = context.getBean("customerService", CustomerService.class);
CustomerService customerService2 = context.getBean("customerService", CustomerService.class);
System.out.println("customerService1 ="+ customerService1);
System.out.println("customerService2 ="+ customerService2);
}
}
customerService1 =module2.prototype.CustomerService@1632847
customerService2 =module2.prototype.CustomerService@e95a56
prototype
customerService1
CustomerService
Applicationprototype
CustomerService
customerService2
© 2014 Time2Master 9
Eager-instantiation of beans
public class Application {
public static void main(String[] args) {
System.out.println("1");
ApplicationContext context = new
ClassPathXmlApplicationContext("/module2/eagerinstantiation/springconfig.xml");
System.out.println("2");
CustomerService customerService = context.getBean("customerService", CustomerService.class);
System.out.println("3");
customerService.addCustomer("Frank Brown");
System.out.println("4");
}
}
public class CustomerServiceImpl implements CustomerService {
public CustomerServiceImpl() {
System.out.println("calling constructor of CustomerServiceImpl");
}
1
calling constructor of CustomerServiceImpl
2
3 The CustomerService bean is eagerly
calling addCustomer of CustomerServiceImpl instantiated
4 10
© 2014 Time2Master
Lazy-instantiation of beans
public class Application {
public static void main(String[] args) {
System.out.println("1");
ApplicationContext context = new
ClassPathXmlApplicationContext("/module2/lazyinstantiation/springconfiglazy.xml");
System.out.println("2");
CustomerService customerService = context.getBean("customerService", CustomerService.class);
System.out.println("3");
customerService.addCustomer("Frank Brown");
System.out.println("4");
}
}
public class CustomerServiceImpl implements CustomerService {
public CustomerServiceImpl() {
System.out.println("calling constructor of CustomerServiceImpl");
}
© 2014 Time2Master 12
Spring Container
LIFECYCLE METHODS
© 2014 Time2Master 13
Lifecycle methods
public interface CustomerService {
public void addCustomer(String customername);
public void init();
public void cleanup();
}
Method called just after the Method called when you close the
constructor ApplicationContext
© 2014 Time2Master 14
Lifecycle methods example
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext
System.out.println("1");
ConfigurableApplicationContext context = new
ClassPathXmlApplicationContext("/module2/xmllifecycle/springconfig.xml");
System.out.println("2");
CustomerService customerService = context.getBean("customerService", CustomerService.class);
System.out.println("3");
customerService.addCustomer("Frank Brown");
System.out.println("4");
context.close();
}
Close the ApplicationContext
}
1
calling constructor of CustomerServiceImpl
calling init method of CustomerService init method
2
3
calling addCustomer of CustomerServiceImpl
4
calling cleanup method of CustomerService cleanup method
© 2014 Time2Master 15
Lifecycle methods with annotations
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
1
calling constructor of CustomerServiceImpl
calling init method of CustomerService init method
2
3
calling addCustomer of CustomerServiceImpl
4
calling cleanup method of CustomerService cleanup method
© 2014 Time2Master 16
Lifecycle methods with annotations
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
© 2014 Time2Master 17
Active Learning
When a bean is declared with scope =
prototype does it load eagerly or lazily?
© 2014 Time2Master 18
Summary
The Spring ApplicationContext instantiates all
Spring beans declared in the Spring XML
configuration file
Spring beans are eagerly instantiated
singletons by default
Spring allows you to call your init methods and
destroy methods anything you like.
Just tell spring what the name of the method is,
and Spring takes care of the rest.
© 2014 Time2Master 19