- Spring Essentials
- Shameer Kunjumohamed Hamidreza Sattari
- 535字
- 2021-07-16 13:05:46
Your first Spring application
Let's start with a very simple Spring application now. This application simply greets the user with a welcome message. Technically, it demonstrates how you configure a Spring ApplicationContext
(IoC container) with just a single bean in it and invoke that bean method in your application. The application has four artifacts in it (besides the project build file, of course):
GreetingService.java
: A Java interface—just a single methodGreetingServiceImpl.java
: A simple implementation ofGreetingService
Application.java
: Your application with amain
methodapplication-context.xml
: The Spring configuration file of your application
The following are the service components of your application. The service implementation just prints a greeting message to the logger:
interface GreetingService { void greet(String message); } public class GreetingServiceImpl implements GreetingService { Logger logger = LoggerFactory.getLogger(GreetingService.class); public void greet(String message) { logger.info("Greetings! " + message); } }
Now let's take a look at the application-context.xml
file, which is your Spring configuration file, where you register GreetingService
as a Spring bean in the following listing:
<?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.xsd"> <bean id="Greeter" class="com.springessentialsbook.chapter1.GreetingServiceImpl"> </bean> </beans>
Finally, you invoke the GreetingService.greet()
method from your Spring application, as given in the following code:
public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application-context.xml"}); GreetingService greeter = (GreetingService) context.getBean("Greeter"); greeter.greet("I am your first Spring bean instance, configured purely with XML metadata. I am resolved by name."); } }
We will explore and conquer the mighty Spring Framework right from this very simple and pretty much self-explanatory application. We will discuss and elaborate the concepts behind this application, and more, in the following sections.
Inversion of Control explained
IoC is a design principle that decouples objects of an object-oriented program from their dependencies (collaborators), that is, the objects they work with. Usually, this decoupling is achieved by externalizing the responsibility of object creation and Dependency Injection to an external component, such as an IoC container.
This concept is often compared to the Hollywood principle, "Don't call us, we will call you." In the programming world, it recommends the main program (or a component) not to instantiate its dependencies by itself but let an assembler do that job.
This immediately decouples the program from the many problems caused by tightly coupled dependencies and relieves the programmer to let them quickly develop their code using abstract dependencies (program to interfaces). Later, at runtime, an external entity, such as an IoC container, resolves their concentrate implementations specified somewhere and injects them at runtime.
You can see this concept implemented in the example we just saw. Your main program (Application.java
) is not instantiating the GreetingService
dependency; it just asks the ApplicationContext
(IoC container) to return an instance. While writing Application.java
, the developer doesn't need to think about how the GreetingService
interface is actually implemented. The Spring ApplicationContext
takes care of object creation and injects any other functionality transparently, keeping the application code clean.
Objects managed by an IoC container do not control the creation and resolution of their dependencies by themselves; rather, that control is inverted by moving it away to the container itself; hence the term "Inversion of Control".
The IoC container assembles the components of the application as specified in the configuration. It handles the life cycles of the managed objects.
- Android Wearable Programming
- FuelPHP Application Development Blueprints
- 新手學Visual C# 2008程序設計
- Python編程完全入門教程
- Monitoring Elasticsearch
- Responsive Web Design by Example
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- Machine Learning in Java
- Swift 4 Protocol-Oriented Programming(Third Edition)
- 人工智能算法(卷1):基礎算法
- Data Science Algorithms in a Week
- 嵌入式Linux C語言程序設計基礎教程
- Mastering XenApp?
- Java基礎案例教程(第2版)
- WordPress 3.7 Complete(Third Edition)