Introduction To Spring 5 and Spring MVC
Introduction To Spring 5 and Spring MVC
DARWIN VARGAS
WORKSHOP OVERVIEW
An in-depth course teaching the use of Spring 5 to build enterprise applications using Java
Including newer areas of Spring/Java technology
Transactions: Controlling
transactions declaratively Web: Integrating Spring with
with Spring Web applications.
•Via both Spring annotations and XML Understand and use Spring
configuration
MVC/REST
WORKSHOP AGENDA
[Optional] Session
14: XML Specific
Configuration
TYPOGRAPHIC CONVENTIONS
Code in the text uses a fixed-width code font, e.g.: package com.javatunes.teach;
Catalog catalog = new CatalogImpl() public class CatalogImpl implements Catalog { public void
speakTruth() {
–Code fragments are the same, e.g. catalog.speakTruth() System.out.println("BeanFactories are way cool");
–We bold/color text for emphasis }
}
–Filenames are in italics, e.g. Catalog.java
–Notes are indicated with a superscript number (1) or a star *
–Longer code examples appear in a separate code box - e.g.
SESSION 1: INTRODUCTION TO SPRING
Spring Dependency
Overview
Introduction Injection
LESSON OBJECTIVES
Capabilities include:
•Dependency Injection (Inversion of Control/IOC) to manage bean dependencies
•DAO/Repository/Transaction support for persistent data
•ORM support (Object-relational mapping, e.g. JPA)
•AOP: Aspect-oriented programming
•Web: Integration with standard JEE Web apps
•Spring MVC: Model-View-Controller Web framework
•Security: Authentication and authorization
THE SPRING MODULES
THE SPRING DISTRIBUTION
•e.g. spring-beans-5.0.5.RELEASE.jar
•Has external dependencies - e.g. logging, JUnit, etc. (1)
•Generally use a tool like maven for dependencies
•We supply jars the jars for some labs, and use maven in others
At right, are the Spring libraries we supply for the early labs
They are a subset of Spring
Later labs, which need more jars, use maven for dependencies
Note the @Test on The test creates the application context, and checks that it's non-null
We use org.junit.Assert.assertNotNull to perform the test
testContextNotNullPositive() See notes about import static and assertTrue usage
•Uses configuration information (metadata) to define, instantiate, configure, and assemble beans
•Metadata: Information about your beans (e.g. bean definitions)
•Container uses the configuration to create and manage beans (1)
package com.javatunes.service;
public class CatalogImpl implements Catalog { // Detail
import org.springframework.context.support.ClassPathXmlApplicationContext;
1 2 3
Create Spring configuration Initialize the Spring container Retrieve beans via the context
data for your beans •e.g. create an application context instance and use them
•It's the "cookbook" telling Spring how to instance to read config data •e.g. use getBean() to look up a bean by
create objects •It will initialize the beans in the config type or name
•Via an XML file like file(s) •Lookup by type is preferred, unless you
applicationContext.xml or via can't do it
annotations •For instance, if you have two beans
implementing the same type
MORE ON APPLICATIONCONTEXT
Bean access
Access point for many Spring capabilities, Resource Access: Config files, URLs, other files
Message resources with I18N capability
including: Publishing events to managed beans that are listeners
Multiple, hierarchical contexts
• boolean containsBean(String): true if named bean • ant-style wildcards: e.g. conf/**/ctx.xml - All ctx.xml
exists files under conf
• <T> T getBean(Class<T> requiredType): Get by type • file: and classpath: prefixes - forces use of specified
• <T> T getBean(String, Class<T> requiredType): Get by location, e.g.
name • The following loads from the classpath:
• Class<?> getType(String name): Get type of named • new
bean FileSystemXmlApplicationContext("classpath:ctx.xml");
• boolean isSingleton(String): Is bean a singleton • Spring uses its resource classes under the hood to do
• String[] getAliases(String): Get any aliases for this bean this
• Many more methods - see the javadoc
MINI-LAB: REVIEW JAVADOC
Mini-Lab
import org.springframework.stereotype.Component;
package com.javatunes.service;
@Component("musicCatalog") // Declares bean with id musicCatalog public class
CatalogImpl implements Catalog {
/* Most detail omitted … */
}
A BRIEF NOTE ON ANNOTATIONS
•e.g. @Component
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.javatunes"/>
</beans>
OVERVIEW - SPRING'S XML SCHEMAS
• With custom namespaces and tags with complex • aop: Configures AOP support
behavior (1) • beans: The standard bean tags we've seen already
• e.g. the context: namespace we just used • context: Configures ApplicationContext related
things
• jee: JEE related configuration such as looking up a
JNDI object
• jms: Configures JMS related beans
• lang: Exposing objects written in language like JRuby
as beans
• tool: Adds tooling-specific metadata
• tx: Configures transaction support
• util: Common, utility configuration issues
In this lab, we'll work with Spring
LAB 1.3: SPRING Annotations
ANNOTATIONS
DEPENDENCY INJECTION
With XML,
Can easily inject With @Autowired, See notes for
<constructor-arg>
into a constructor apply it to the some additional
means "inject into
(ctor) constructor detail/capabilities
constructor"
– Assume the
CatalogImpl
constructor
shown below
CONSTRUCTOR INJECTION
public class CatalogImpl implements Catalog { // Most detail omitted
public CatalogImpl(ItemRepository repository) { /* … */ }
}
DEPENDENCY
INJECTION
REVIEW QUESTIONS
What is Spring, and how does it help you build enterprise apps?
What is an ApplicationContext?
•Dependency Injection
•Persistence support (Repository/DAO and ORM)
•Integration with standard Web technologies, and MVC Web apps
• Configure/wire beans, access program resources, work with resource bundles, load multiple
contexts, and publish events to beans
• Common implementations include:
• ClassPathXmlApplicationContext,
• FileSystemXmlApplicationContext, and
• AnnotationConfigApplicationContext