--- – SPRING FRAMEWORK – CORE



--- – SPRING FRAMEWORK – CORE

0 1


TrainingSpringCoreSlide

TrainingSrpingCoreSlide By Anuchit prasertsang

On Github AnuchitPrasertsang / TrainingSpringCoreSlide

---

SPRING FRAMEWORK

@NongComEng@

Anuchit Prasertsang

from SoftPlus Family

CORE

  • อนุชิต ประเสริฐสังข์  (หน่อง)GOAL          เขียนโปรแกรมต้องสนุกสนานเพลิดเพลิน(ฟิน)LITTLE KNOW FACT          ชอบนั่งสมาธิ          ชอบสีขาว,ฟ้า          อยากเป็น Super Saiyan GoD

CORE

Introduction

Spring Framework

CORE

Agenda

  • What is Spring framework ?
  • Why Spring framework ?
  • Spring framework architecture
  • Spring Concept
    • Inversion of Control (IoC)
    • Dependency Injection (DI)
Test Note

CORE

Agenda (Continued)

  • Spring Bean Definition
  • Spring Injecting Collection
  • Spring Bean Definition Inheritance
  • Spring Bean Scopes
  • Spring Bean Life Cycle
  • Spring Beans Auto-Wiring
  • Spring Annotation Based Configuration
Test Note

CORE

CORE

What is Spring framework ?

  • Spring Framework is open source application framework for Java platform
  • Light-weight yet comprehensive framework for building Java SE and Java EE applications

CORE

why

Spring framework ?

  • Spring Framework does not enforce any specific programming model it has become widely popular in the Java community primarily as an alternative and replacement for the Enterprise JavaBean model

CORE

why

Spring framework ?

  • Wiring components (JavaBeans) through Dependency Injection
  • Design to interfaces
    • Insulates a user of a functionality from implementation details

CORE

why

Spring framework ?

  • Simplify use of popular technologies
  • Integration with other technologies
    • EJB for J2EE
    • Hibernate, iBates, JDBC (for data access)
    • Velocity (for presentation)
    • Struts and WebWork (For web)
    • Java Persistence API (JPA)

CORE

CORE

Spring framework architecture

CORE

CORE

Inversion of Control (IoC)

  • IoC is a principle that externalizes the creation and management of component dependencies.

CORE

Dependency Inversion Principle (DIP)

"High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions."

"Abstraction"

CORE

Cr.www.somkiat.cc

CORE

public class Email { 
	public void send() {	
	}
}
                
public class Notification {
	Email email;
	public Notification() {
		    email = new Email();
	}
	public void notifyPromotion() {
		    email.send();
	}
 
}                

Violation DIP

CORE

CORE

public interface MessageService {
	public void send();
 
}
                
public class Email implements MessageService { 
	public void send() {	
	}
}
                
public class Notification {
	MessageService messageService;
	public Notification() {
		    messageService = new Email();
	}
	public void notifyPromotion() {
		    messageService.send();
	}
 
}            

CORE

Dependency Injection (DI)

  • Aims to offer a simpler mechanism for provisioning component dependencies and managing these dependencies throughout their lifecycles.
  • "Hollywood Principle"
    • Don't call me, I'll call you
  • "Container" resolves (injects) dependencies of components by setting implementation object (push)

CORE

CORE

Two Dependency Injection Variants

  • Constructor dependency Injection
    • Dependencies are provided through the constructors of the component
  • Setter dependency injection
    • Dependencies are provided through the JavaBean-style setter methods of the component
    • More popular than Constructor dependency injection

CORE

Constructor Dependency Injection

public class ConstructorInjection {

    private Dependency dependency;
    
    public ConstructorInjection(Dependency dependency) {
        this.dependency = dependency;
    }
}
				

CORE

Setter Dependency Injection

public class SetterInjection {
			
    private Dependency dependency;
    
    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }
}
				

CORE

Lab1

http://goo.gl/QmdJ34 Spring3.1-API/

CORE

CORE

DI Support in Spring 

CORE

Sub-topics

  • BeanFactory interface
  • XmlBeanFactory implementation
  • Bean configuration file
    • Setter dependency injection
    • Constructor dependency injection

CORE

BeanFactory

  • BeanFactory object is responsible for managing beans and their dependencies
  • Your application interacts with Spring's DI container through BeanFactory interface
    • BeanFactory object has to be created by the application typically in the form of XmlBeanFactory
    • Once created, the application can access the beans via BeanFactory interface

CORE

BeanFactory Implementations

  • XmlBeanFactory
    • Convenience extension of DefaultListableBeanFactory that reads bean definitions from an XML document

CORE

Reading XML Configuration File via XmlBeanFactory class

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class XmlConfigWithBeanFactory {
    public static void main(String[] args) {
        XmlBeanFactory factory = 
              new XmlBeanFactory(new FileSystemResource("beans.xml"));
       SomeBeanInterface b = (SomeBeanInterface) factory.getBean("nameOftheBean");
    }
}
			   

CORE

Bean Configuration File

  • Each bean is defined usingtag under the root of the                                        tag
  • The id attribute is used to give the bean its default name
  • The class attribute specifies the type of the bean (class of the bean)

CORE

Spring Bean Definition

  • Basic Bean Creation
  • Basic Constructor Injection
  • Basic Setter Injection
  • Reference Injection

CORE

Basic Bean Creation

  HelloWorld hello = new mypackage.HelloWorld();
                
 <bean id="hello" class="mypackage.HelloWorld"></bean>
                

CORE

Basic Constructor Injection

HelloWorld hello = new mypackage.HelloWorld("Hello World");
                
 <bean id="hello" class="mypackage.HelloWorld">
    <constructor-arg value="Hello World"/>
 </bean>
                

CORE

Basic Setter Injection

   HelloWorld hello = new mypackage.HelloWorld();
              hello.setMessage("Hello World");
                
 <bean id="hello" class="mypackage.HelloWorld">
    <property name="message" value="Hello World"/>
 </bean>
                

CORE

Reference Injection

   HelloWorld hello = ...
   
   MessageService msgService = new mypackage.MessageService();
                  msgService.setMessageProvider(hello)
                
 <bean id="hello" class="mypackage.HelloWorld">
    .....
 </bean>
 
 <bean id="msgService" class="mypackage.MessageService">
    <property name="messageProvider" ref="hello"/>
 </bean>
                 

CORE

Lab2

http://goo.gl/QmdJ34

CORE

CORE

Spring Injecting Collection

 

  • List
  • Set
  • Map
  • Properties

CORE

 

  • List(example)
                         <property name="addressList">
                            <list>
                                <value>INDIA</value>
                                <value>Pakistan</value>
                                <value>USA</value>
                                <value>USA</value>
                                <value>INDIA</value>
                            </list>
                         </property>
				

CORE

 

  • Set(example)
                     <property name="addressSet">
                        <set>
                            <value>INDIA</value>
                            <value>Pakistan</value>
                            <value>USA</value>
                            <value>USA</value>
                        </set>
                     </property>
				

CORE

 

  • Map(example)
                    <property name="addressMap">
                        <map>
                            <entry key="1" value="INDIA"/>
                            <entry key="2" value="Pakistan"/>
                            <entry key="4" value="USA"/>
                        </map>
                    </property>
				

CORE

 

  • Properties(example)
                    <property name="addressProp">
                        <props>
                            <prop key="one">INDIA</prop>
                            <prop key="two">Pakistan</prop>
                            <prop key="three">USA</prop>
                            <prop key="four">USA</prop>
                        </props>
                    </property>
				

CORE

 

Lab3

http://goo.gl/QmdJ34

CORE

CORE

Spring Bean Definition

  • Configuration metadata
    • class
    • id/name
    • scope [Bean Scope]
    • constructor-arg [DI]
    • properties [DI]
    • autowiring mode [Auto wire]
    • lazy-initialization mode [init when fisrt request]
    • initialization method [Life Cycle]
    • destruction method [Life Cycle]

CORE

Spring Bean Definition Inheritance

  • A child bean definition inherits configuration data from a parent definition.
  • The child definition can override values.
  • Spring Bean definition inheritance has nothing to do with Java class inheritance but inheritance concept is same.
  • attribute [parent]
Ex.Spring Framework 3.1[page 39]

CORE

Bean Scopes

CORE

singleton [default]

CORE

prototype

CORE

Other Scopes

  • request [HTTP request]
  • session [HTTP session]
  • global-session [gobal HTTP session]
  • custom scopes

CORE

CORE

Spring Bean Life Cycle

CORE

Initialization callbacks

    public class ExampleBean {
        public void init() {
            // do some initialization work
        }
    }
                
 <bean id="exampleBean" class="examples.ExampleBean" init-method="init"/>
                

CORE

Destruction callbacks

    public class ExampleBean {
        public void destroy() {
            // do some destruction work
        }
    }
			    
 <bean id="exampleBean" class="examples.ExampleBean" destroy-method="destroy"/>
				

CORE

CORE

Spring Beans Auto-Wiring

  • Spring can autowire dependencies through introspection of the bean classes so that you do not have to explicitly specify the bean properties or constructor arguments.

CORE

Autowiring Properties

  • autowire="byName"
    • The property names of target bean (actually set<Property-name>() methods of the target bean) are used to search beans
  • autowire="byType"
    • The property types of target bean - actually argument types of set<Property-name>(ArgumentType arg) - are used to match a bean instance in the container

CORE

Autowiring Properties

  • autowire="constructor"
    • Match constructor argument types
    • The argument types of Constructor(ArgumentType arg) - are used to match a bean instance in the container
  • autowire="autodetect"
    • Spring will attempt to autowire by constructor first,If a suitable constructor-to-bean match cant't be found,then Spring will attempt to autowiring using "type"

CORE

CORE

Spring Annotation Based Configuration

 

 

 <?xml version="1.0" encoding="UTF-8"?>
 <beans  xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         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">

         <context:annotation-config/>

         <!-- bean definitions go here -->
 </beans>		
				

CORE

 

 

  • @Required(require property)
  • @Autowired(Injection by Type)
  • @Qualifier(specify by name)
  • @PostConstruct(initialization callback in life cycle)
  • @PreDestroy(destruction callback destruction callback)
  • @Resource(@Autowired+@Qualifier)

CORE

CORE

any question?

CORE

0