0% found this document useful (0 votes)
4 views19 pages

Module 10 - Spring Container-Upd

Uploaded by

surafelbehailu90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Module 10 - Spring Container-Upd

Uploaded by

surafelbehailu90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Module 10: Spring Container

CS544: Enterprise Architecture


© 2014 Time2Master 1
Spring Basics
 In this module we are going start by looking at
a basic hello world spring application. Then
we will look into the details of:
 The Spring Application Context
 Spring Bean initialization
 Spring Bean lifecycle methods
 We are going to take a look at the basic outer
layer of spring, the application context, and
the configuration of beans. Life is found in
layers.

© 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;

public class Application {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("module2/helloworld/springconfig.xml");
CustomerService customerService = context.getBean("customerService", CustomerService.class);
customerService.sayHello();
}
} Get the bean with
id=“customerService”
package module2.helloworld;
from the
public class CustomerService { ApplicationContext
public void sayHello(){
System.out.println("Hello from CustomerService");
}
}

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
springconfig.xml
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">

<bean id="customerService" class="module2.helloworld.CustomerService" />


</beans> Bean declaration

© 2014 Time2Master 3
Spring Container

THE APPLICATION CONTEXT

© 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

ApplicationContext context = new ClassPathXmlApplicationContext("springconfig.xml");

Resource on the classpath

ApplicationContext context = new FileSystemXmlApplicationContext("C:\springconfig.xml");

Resource on the filesystem

© 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);
}
}

public class CustomerService {


public CustomerService() {
}
}

<bean id="customerService" class="module2.singleton.CustomerService" />

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);
}
}

public class CustomerService {


public CustomerService() {
}
}

<bean id="customerService" class="module2.prototype.CustomerService" scope="prototype" />

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");
}

public void addCustomer(String customername) {


System.out.println("calling addCustomer of CustomerServiceImpl");
}
}
<bean id="customerService" class="module2.eagerinstantiation.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");
}

public void addCustomer(String customername) {


System.out.println("calling addCustomer of CustomerServiceImpl");
}
}
<bean id="customerService" class="module2.lazyinstantiation.CustomerServiceImpl"
lazy-init="true" />
Lazy instantiation
1
2
calling constructor of CustomerServiceImpl
3 The CustomerService bean is lazy
calling addCustomer of CustomerServiceImpl instantiated
4 © 2014 Time2Master 11
Spring Beans
 Spring beans default to eagerly instantiated
singletons, but can be configured to lazily
instantiate or even not be a singleton at all.
 Spring beans are unaware of the fact that they
are organized by Spring, regardless of what
their configuration is.

© 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();
}

public class CustomerServiceImpl implements CustomerService {


public CustomerServiceImpl() {
System.out.println("calling constructor of CustomerServiceImpl");
}
public void addCustomer(String customername) {
System.out.println("calling addCustomer of CustomerServiceImpl");
}
public void init() {
System.out.println("calling init method of CustomerService");
}
public void cleanup() {
System.out.println("calling cleanup method of CustomerService");
}
}

<bean id="customerService" class="module2.xmllifecycle.CustomerServiceImpl"


init-method="init" destroy-method="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;

public class CustomerServiceImpl implements CustomerService {


public CustomerServiceImpl() {
System.out.println("calling constructor of CustomerServiceImpl");
}
public void addCustomer(String customername) {
System.out.println("calling addCustomer of CustomerServiceImpl");
}
@PostConstruct @PostConstruct
public void init() {
System.out.println("calling init method of CustomerService");
}
@PreDestroy @PreDestroy
public void cleanup() {
System.out.println("calling cleanup method of CustomerService");
}
}

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">

Tells Spring to check for annotation


configuration in the Spring beans
<context:annotation-config/>
<bean id="customerService" class="module2.annotationslifecycle.CustomerServiceImpl"/>
</beans>

© 2014 Time2Master 17
Active Learning
 When a bean is declared with scope =
prototype does it load eagerly or lazily?

 Specifying a destroy method is not enough for


Spring to use it, what else is needed?

© 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

You might also like