Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Microservices with Spring

You're reading from   Building Microservices with Spring Master design patterns of the Spring framework to build smart, efficient microservices

Arrow left icon
Product type Course
Published in Dec 2018
Publisher Packt
ISBN-13 9781789955644
Length 502 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Rajesh R V Rajesh R V
Author Profile Icon Rajesh R V
Rajesh R V
Dinesh Rajput Dinesh Rajput
Author Profile Icon Dinesh Rajput
Dinesh Rajput
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Title Page
Copyright
About Packt
Contributors
Preface
1. Getting Started with Spring Framework 5.0 and Design Patterns FREE CHAPTER 2. Overview of GOF Design Patterns - Core Design Patterns 3. Wiring Beans using the Dependency Injection Pattern 4. Spring Aspect Oriented Programming with Proxy and Decorator pattern 5. Accessing a Database with Spring and JDBC Template Patterns 6. Improving Application Performance Using Caching Patterns 7. Implementing Reactive Design Patterns 8. Implementing Concurrency Patterns 9. Demystifying Microservices 10. Related Architecture Styles and Use Cases 11. Building Microservices with Spring Boot 12. Scale Microservices with Spring Cloud Components 13. Logging and Monitoring Microservices 14. Containerizing Microservices with Docker 15. Scaling Dockerized Microservices with Mesos and Marathon 1. Other Books You May Enjoy Index

Using a Spring container to manage beans with the Factory pattern


Spring provides us with a container, and our application objects live in this Spring container. As shown in the following diagram, this container is responsible for creating and managing the objects:

In a Spring application, our application objects live in this Spring container

The Spring Container also wires the many Object together according to its configuration. It is configured with some initialized parameters, and manages their complete life cycle from start to finish.

Basically, there are two distinct types of Spring container:

  • Bean factory
  • Application contexts

Bean factory

In the Spring Framework, the org.springframework.beans.factory.BeanFactory interface provides the bean factory, which is a Spring IoC container. XmlBeanFactory is an implementation class for this interface. This container reads the configuration metadata from an XML file. It is based on the GOF factory method design pattern--it creates, manages, caches, and wires the application objects in a sophisticated manner. The bean factory is merely an object pool where objects are created and managed by configuration. For small applications, this is sufficient, but enterprise applications demand more, so spring provides another version of the spring container with more features.

In the next section, we will learn about the application context and how Spring creates it in the application.

Application contexts

In the Spring Framework, the org.springframework.context.ApplicationContext interface also provides Spring's IoC container. It is simply a wrapper of the bean factory, providing some extra application context services, such as support for AOP and, hence, declarative transaction, security, and instrumentation support such as support for message resources required for internationalization, and the ability to publish application events to interested event listeners.

Creating a container with an application context

Spring provides several flavors of application context as a bean container. There are multiple core implementations of the ApplicationContext interface, as shown here:

  • FileSystemXmlApplicationContext: This class is an implementation of ApplicationContext that loads application context bean definitions from the configuration files (XML) located in the file system.
  • ClassPathXmlApplicationContext: This class is an implementation of ApplicationContext that loads application context bean definitions from the configuration files (XML) located in the classpath of the application.
  • AnnotationConfigApplicationContext: This class is an implementation of ApplicationContext that loads application context bean definitions from the configuration classes (Java based) from the class path of the application.

Spring provides you with a web-aware implementation of the ApplicationContext interface, as shown here:

  • XmlWebApplicationContext: This class is a web-aware implementation of ApplicationContext that loads application context bean definitions from the configuration files (XML) contained in a web application.
  • AnnotationConfigWebApplicationContext: This class is a web-aware implementation of ApplicationContext that loads Spring web application context bean definitions from one or more Java-based configuration classes.

We can use either one of these implementations to load beans into a bean factory. It depends upon our application configuration file locations. For example, if you want to load your configuration file spring.xml from the file system in a specific location, Spring provides you with a FileSystemXmlApplicationContext, class that looks for the configuration file spring.xml in a specific location within the file system:

    ApplicationContext context = new
     FileSystemXmlApplicationContext("d:/spring.xml"); 

In the same way, you can also load your application configuration file spring.xml from the classpath of your application by using a ClassPathXmlApplicationContext class provided by Spring. It looks for the configuration file spring.xml anywhere in the classpath (including JAR files):

    ApplicationContext context = new 
     ClassPathXmlApplicationContext("spring.xml"); 

If you are using a Java configuration instead of an XML configuration, you can use AnnotationConfigApplicationContext:

    ApplicationContext context = new 
     AnnotationConfigApplicationContext(AppConfig.class); 

After loading the configuration files and getting an application context, we can fetch beans from the Spring container by calling the getBean() method of the application context:

    TransferService transferService = 
     context.getBean(TransferService.class); 

In the following section, we will learn about the Spring bean life cycle, and how a Spring container reacts to the Spring bean to create and manage it.

You have been reading a chapter from
Building Microservices with Spring
Published in: Dec 2018
Publisher: Packt
ISBN-13: 9781789955644
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime