350 likes | 480 Vues
This training session introduces the Spring Framework, focusing on its core concepts such as Inversion of Control (IoC) and Dependency Injection (DI). Participants will learn about the Spring architecture, bean creation, bean scopes, and the application context. We encourage questions throughout the session to enhance understanding. Prerequisites include basic Java and XML skills. By the end of this session, attendees will be familiar with Spring documentation, bean definitions, and the Spring ecosystem, providing a solid foundation for Java EE development.
E N D
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 • Become familiar with Spring documentation • http://www.springsource.org/documentation • Learn basics of the Spring architecture • Learn about bean creation • Learn about bean scopes • Learn about the application context • Inversion of Control (IoC) • Dependency Injection (DI)
Goals of the Spring Framework • Simplify Java EE development • Solve problems not addressed by Java EE • Provide simple integration for best of breed technologies • Provide modular/pluggable architecture • Use what you want – don’t use what you don’t • http://www.springsource.org/about
Explore the Spring Ecosystem • Main Page: http://www.springsource.org/ • Documentation: http://www.springsource.org/documentation • Forum: http://forum.springsource.org/ • Jira: https://jira.springsource.org/secure/Dashboard.jspa
Spring Framework Modified from http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/images/spring-overview.png
Data Access Integration • Data Access Integration layer includes: • JDBC (abstraction layer over native JDBC) • ORM (integration support for JPA, Hibernate, …) • Transaction support (declarative and programmatic)
Web • Basic web integration features • File upload • Initialization of IoC container using servlet listeners • Contains Spring’s model view controller (MVC) implementation
Test • Test module provides integration with test frameworks • JUnit • TestNG • Provides ability to load test specific ApplicationContexts • Also provides helpful mock objects
Core Container • Core and Beans modules provide framework fundamentals • Inversion of Control (IoC) and Dependency Injection (DI) • BeanFactory provides factory pattern implementation for creating Java objects • Allows decoupling of configuration and dependencies from actual code
Application Context configuration file • Objects managed by the Spring IoC container are called beans • Objects must be defined to be managed by the Spring (IoC) container • Defined in xml (applicationContext.xml in the Stack) • Defined using annotations • Definition contains “configuration metadata” • Template for creating beans and their dependencies
Loading the Application Context • org.springframework.context.ApplicationContext interface represents the Spring IoC container • Responsible for instantiating and assembling beans • Template for doing so is in the defined configuration metadata (xml, annotations, or Java code) • The application context can be loaded as follows: ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBeansomeBean = (SomeBean) context.getBean("someBean"); someBean.callMethod();
Defining Beans • When defined in xml the Spring configuration file looks like the following: <!– Bean configuration file --> <?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"> <!-- bean definitions go here --> </beans>
Defining beans • Each bean should have • Unique id • Fully qualified package and name of the object <?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"> <bean id="someBean" class="org.lds.training.SomeBean"> <!-- more bean definitions go here --> </beans> </beans>
Lab 1: Bean Creation https://tech.lds.org/wiki/Introduction_to_Spring#Lab_1_Bean_Configuration
Bean Scopes • Where Spring starts to pay dividends • Sometimes you want to be able to control the life of a bean • Bean scopes provide this ability • By default all beans are singletons
Singleton Scope • Remember these days • The new world public static synchronized MySingletongetInstance() { public static MySingletongetInstance() { if (instance == null) { instance = new MySingleton(); } return instance; } <!– singleton is the default so these two definitions are equivalent --> <bean id="whatever" class="org.lds.whatever.MyBean" /> <bean id="whatever" class="org.lds.whatever.MyBean" scope="singleton" />
Prototype Scope • Equivalent to calling new every time a an instance of a class is needed • Spring does not manage he lifecycle of prototype bean • The configuration is as follows: <bean id="whatever" class="org.lds.whatever.MyBean" scope="prototype" />
Web application scopes • Without Spring you would have to manage bean creation and lifecycles manually <bean id=“whatever” class=“org.lds.whatever.MyBean” scope=“request” /> <bean id=“whatever” class=“org.lds.whatever.MyBean” scope=“session” />
Lab 2: Bean Scopes https://tech.lds.org/wiki/Introduction_to_Spring#Lab_2_Bean_Scopes
Inversion of Control (IoC) the Concept • Objects define dependencies by: • Constructor arguments • Setter arguments • Arguments to factory method • Container injects dependencies when the bean is created • Inverse of bean controlling instantiation and/or location of its dependencies
Inversion of Control (IoC) the Concept • Managing dependencies on other beans • Dependency lookup vs. Dependency Injection //dependency lookup public class Lookup { private SomeBeansomeBean; public SomeBeanfindBean(Container container) { return (SomeBean) container.getBean(“someBean”); } } //dependency injection public class Injection { private SomeBeansomeBean; public void setSomeBean(SomeBeansomeBean) { this.someBean = someBean; } }
Advantages of Inversion of Control (IoC) • Simplifies component dependency and lifecycle management • Eliminates need for: • Calling new or looking up dependencies • Decouples code from IoC container • Injection is easier – less code – easier to maintain • Minimizes need for creational pattern implementation • Simplifies testing
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) • Two basic types of injection • Setter injection • Constructor injection
DI (setter injection) • Say we have to following Rabbit class • Example public class Rabbit { private String favoriteFood; public void setFavoriteFood(String favoriteFood) { this.favoriteFood = favoriteFood; } public void printFavoriteFood() { System.out.println(favoriteFood); } } <bean id="rabbit" class="org.lds.farm.Rabbit"> <property name="favoriteFood" value="lettucev /> </bean>
DI (constructor injection) • Say we have to following Rabbit class • Example public class Rabbit { private String favoriteFood; public Rabbit(String favoriteFood) { this.favoriteFood = favoriteFood; } public void printFavoriteFood() { System.out.println(favoriteFood); } } <bean id="rabbit" class="org.lds.farm.Rabbit"> <constructor-arg value="lettuce" /> </bean>
DI (continued) • Ability to inject many data types • Lists, Sets, Properties, Maps (most collection types) • Other beans • Lets us look at a few examples: • http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-collection-elements
DI Collections • Say our rabbit has many favorite foods public class Rabbit { private Set<String> favoriteFoods; public void setFavoriteFoods(List<String> favoriteFoods) { this.favoriteFoods = favoriteFoods; } public void printFavoriteFood() { for (String favoriteFoods : favoriteFood) { System.out.println(favoriteFood); } } } <bean id="rabbit" class="org.lds.farm.Rabbit"> <property name="favoriteFoods"> <set> <value>lettuce</value> <value>carrot</value> </set> </property> </bean>
DI Bean References • Lets expand our rabbit concept to an entire farm • And then modify our rabbit class as follows public class Farm { private List<Rabbit> rabbits; public void setRabbits(List<Rabbit> rabbits) { this.rabbits = rabbits; } } public class Rabbit { private String name; public Rabbit(String name) { this.name = name; } public void setName(String name) { this.name = name; } //… }
Bean Reference Examples <bean id="rabbit" class="org.lds.model.Rabbit"> <property name="name" value="Bubba" /> </bean> <bean id="farm" class="org.lds.model.Farm"> <property name="rabbits"> <list> <ref bean="rabbit" /> <!– anonymous inner bean --> <bean class="org.lds.model.Rabbit"> <property name="name" value="Snowshoe" /> </bean> </list> </property> </bean>
Another public class Farm { private Rabbit prizeRabbit; public void setPrizeRabbit(Rabbit prizeRabbit) { this.prizeRabbit = prizeRabbit; } } <bean id="prize" class="org.lds.model.Rabbit"> <property name="name" value="Queen Bee" /> </bean> <bean id="farm" class="org.lds.model.Farm"> <property name="prizeRabbit" ref="prize" /> </bean>
Lab 3: Dependency Injection https://tech.lds.org/wiki/Introduction_to_Spring#Lab_3_Dependency_Injection
Summary • Info • More info