Introduction to Spring

namibiancurrishInternet and Web Development

Nov 12, 2013 (3 years and 10 months ago)

169 views

Introduction to Spring

Matt Wheeler

Notes


This is a training
NOT

a presentation


Please ask questions


Prerequisites


Introduction to Java Stack


Basic Java and XML skills

Overview

Learn the basics of the Spring Framework


Spring is

Goals of the Spring Framework


Simplify Java EE development


Solve problems not addressed by Java EE


Provide simple integration for best of breed
technologies

Defining Beans


Define beans to be managed by Spring (
IoC
)
container


Defined in xml (applicationContext.xml in the Stack)


Defined using annotations


Basic configuration

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




<!


Bean definitions go here
--
>


</beans>

Defining beans


Each bean should have


Unique name or
preferrably

id


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




<bean id=“whatever” class=“
org.lds.whatever.BeanName
” />


</beans>

Utilizing
ApplicationContext

//instantiate the application context

ApplicationContext

context = new
ClassPathXmlApplicationContext
(“beans.xml”);


Whatever bean = (Whatever)
context.getBean
(“whatever”);

bean.something
();



Lab 1: Bean Creation

https://tech.lds.org/wiki/Introduction_to_Spring#L
ab_1_Bean_Creation


Inversion of Control (
IoC
) the Concept


Simplify component dependencies and lifecycle
management


Dependency lookup vs. Dependency Injection

//dependency lookup

public class Lookup{


private Bean
bean
;



public Bean
findBean
(Container
container
) {


return (Bean)
container.getBean
(“
someBean
”);


}

}

//dependency injection

public class Injection {


private Bean
bean
;



public void
setBean
(Bean
bean
) {


this.bean

= bean;


}

}

Advantages of Inversion of Control (
IoC
)


Simplify component dependency and lifecycle
management


Injection eliminates need for:


Calling new


Looking up dependencies


Decouples code from
IoC

container


Injection is easier


less code


Simplifies testing


Easier to maintain


Inversion of Control (
IoC
)


Heart of Spring Framework is the
IoC

container


Two basic implementations of the
IoC

container


ApplicationContext


BeanFactory


BeanFactory

is stripped down version of
ApplicationContext


We will exclusively focus on
ApplicationContext


Dependency Injection (DI)


DI (setter injection)


DI (constructor injection)


Summary


Info


More info