Spring (framework)
Notes - Mastering Spring Framework Fundamentals
Chapter 1: Getting Started
The smallest addition to a Maven
pom.xmlfile to include Spring is the following:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${VERSION}</version>
</dependency>The Bill of Materials (BOM) is a particular
pom.xmlfile that only defines a set of dependencies. Including it in a Spring project assures that all the different Spring modules will be from the same version:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${VERSION}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Chapter 2: Creating the Spring Container with Application Contexts
The main interface for the Spring framework is called an
ApplicationContext, and its three main implementations are:AnnotationConfigApplicationContext: it will scan some packages of the project and find components by looking at annotations;FileSystemXmlApplicationContext: it loads the configuration file that describes the beans from the file system;ClassPathXmlApplicationContext: it loads the configuration file that describes the beans from the classpath.
A profile is an environment-specific configuration on how you want the Spring application to run.
Chapter 3: Creating, Retrieving, and Using Spring-Managed Beans
Declaring a bean via XML:
Retrieving a bean from the file-based
ApplicationContext:
By default, Spring creates beans as singletons:
To create different instances every time, we can change the
scopeto"prototype":
Chapter 4: Understanding Dependency Injection and Inversion of Control
By default, beans are initialized using the zero-arguments constructor. To inject dependencies through the constructor:
Hollywood principle: don't call us, we'll call you
An alternative approach to the constructor-based depedency injection is the property-based dependency injection, where the dependencies are injected through setter methods:
Chapter 5: Auto-wiring Dependencies in the Spring Container
An alternative to specifying beans and their dependencies manually within an XML file is to use annotations. In order to do so, we define in the XML file a
context:component-scantag, where we specify the package name that Spring will introspect and retrieve the beans from:
After that, we can use the
@Componentannotation on all the classes that Spring should consider beans, and the@Autowiredannotation on the dependencies to be injected:
Component is the most general annotation that we can use to annotate beans; more specific annotations include:
Controller: the class is a web controller in a Spring MVC application
Service
Repository
Configuration
The
@Autowiredannotation can be used on member fields for setter-based dependency injection, or on the constructor for constructor-based dependency injection.To completely get rid of the XML context file, we can use the
AnnotationConfigApplicationContextclass, referencing the base package to scan for components:
Alternatively, we can create a class annotated with the @Configuration annotation, and reference that in the AnnotationConfigApplicationContext constructor:
When multiple beans could be eligible to be injected (e.g., two concrete implementations of a common interface), we need to use the
@Qualifierannotation to help Spring decide which one to use:
Chapter 6: Using Properties and Profiles in Spring Projects
It is possible to define properties for the application in a property file, then add a
@PropertySourceannotation on the@Configurationcomponent, and then inject the value in fields using the@Valueannotation:
Other ways to inject values is to provide them through environment variables or command line arguments (with the
-Dswitch).Profiles are useful to inject different values for the same variables (e.g., an application deployed locally and on a production instance). The main two ways to set a profile is either to pass a
-Dspring.profiles.active=<profile>command line argument, or callSystem.setProperty("spring.profiles.active", "<profile>")from the code.We can annotate classes with the
@Profileannotation, which means those classes will only be instantiated if the profile within the annotation matches the active profile.
Chapter 7: Getting to Know the Spring Container
It is possible to make a bean aware of two lifecycle methods (one when the bean is created, and one when it is destroyed) by making the bean implement two interfaces:
We can achieve the same result by using the JDK lifecycle annotations instead:
The general bean lifecycle consists of:
the constructor being called
setter methods are called
bean post-processor is called
post-constructor method is called
destroy method is called
finalize method is called
Chapter 8: Accessing Databases with Spring
The
javax.sql.DataSourceclass is essential to interface Spring with a database, as it will return connections (new or reused) to the database:
Other useful types of DataSource implementations are:
SingleConnectionDataSource
HikariDataSource
JndiDataSourceLookup
@Repositoryis the correct annotation to use with DAO (data access objects):
There is an abstract class called JdbcDaoSupport which removes a lot of the boilerplate needed to configure a DAO:
Chapter 9: Aspect-oriented Programming
Aspect-oriented Programming (AOP) allows you to define aspects, which are methods that are invoked before or after some events are fired in the application.
In order to use AOP in Spring, we need to include
spring-aspectsin thepom.xmlfile:
In order to make AOP working in the Spring application, we need to annotate the
@Configurationclass with the@EnableAspectJAutoProxyannotation:
There are three types of Advice in Spring:
@Before: the aspect will run before the original method@After: the aspect will wun after the original method@Around: the aspect wraps the original method (which can be invoked via theProceedingJoinPoint.proceed()method)
A pointcut is the string within the Advice annotation, and it allows to specify on what event(s) the aspect should run - e.g., execution of a method, accessing a variable, execution of any method in a class, etc.
A joinpoint is created in Spring anytime an event that can be matched against a pointcut is fired, and if any pointcuts match, the corresponding methods will be invoked. It is possible to access the joinpoint from the aspect method, in order to retrieve information about the original method invocation:
Chapter 10: Web Application Development in Spring
The Spring MVC component allows us to use the popular Model-View-Controller pattern to create applications that handle web requests and produce responses. In order to use it, we need to add
spring-webmvcas a dependency in thepom.xmlfile.The
web.xmlfile (in the webapp/WEB-INF folder) is a deployment descriptor, which tells the essential configuration the app needs to run to its web container (JBoss, Tomcat, etc.)
The
@Controllerannotation tells Spring MVC that the annotated class is a Controller.
We can also access path variables and request parameters as arguments to the method:
Resources
Courses
Mastering Spring Framework Fundamentals - Matthew Speake
Websites
Last updated
Was this helpful?