- 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.
- DevOps:軟件架構師行動指南
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- 神經網絡編程實戰:Java語言實現(原書第2版)
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- Learn React with TypeScript 3
- C語言程序設計上機指導與習題解答(第2版)
- 持續輕量級Java EE開發:編寫可測試的代碼
- Java Web應用開發項目教程
- INSTANT Apache ServiceMix How-to
- UI動效設計從入門到精通
- SQL Server 2008實用教程(第3版)
- Scala實用指南
- ServiceDesk Plus 8.x Essentials
- Visual FoxPro程序設計
- Java語言程序設計與實現(微課版)