Software Architecture

Components and Spring IoC Container

Software Architecture

Architecture is a subjective thing, a shared understanding of a system’s design by the expert developers on a project. Commonly this shared understanding is in the form of the major components of the system and how they interact. It’s also about decisions, in that it’s the decisions that developers wish they could get right early on because they’re perceived as hard to change.


-- Martin Fowler, 2003

Components-based

软件工程强调软件开发过程应该采用工程化开发方法和工业化生产技术,从传统工业借鉴方法和技术是一种有效手段

  • 基于标准化构件的生产技术
    • 汽车生产:汽车零件
    • 建筑工程:建筑材料
  • 第三方提供 vs. 自身积累
    • 汽车轮胎 、变速箱总成

Software Component


A software component is a unit of composition with contractually specified interfaces and explicit context dependencies only. A software component can be deployed independently and is subject to composition by third parties.


-- Clemens Szyperski

类比:装机

  • A unit of composition
  • Contractually specified interfaces
  • Explicit context dependencies
  • Composition by third parties

CBSE

Component-based software engineering (CBSE), i.e. components-based development (CBD), is ... a reuse-based approach to defining, implementing and composing loosely coupled independent components into systems.

Goals of CBD

  • Assemble a system out of existing (third-party) components
  • Update a system by adding / replacing components, which are
    • units of deployment
    • are handled as they are (blackboxes)

CCM Component

Component Composition

Spring

The Spring framework is a modular framework that offers a large set of "tools" to support the developer in writing modern component-based applications.

https://spring.io/projects/spring-framework

CBD a web app with Spring

https://www.bilibili.com/video/BV1A7411F7Jh?p=3 (5:30)


sa-spring/spring-bootstrap

Compositing/Assembling/Wiring

控制反转 IoC

提供服务的组件是被“注入”的而不是被“直接写入”到请求者的代码中


https://martinfowler.com/articles/injection.html

Solution Design

  • MovieFinder is an interface

Bad Design


Goal: loosely coupled components

  • MovieLister should work with any MovieFinderImplementation
    • MovieLister does not need to know the particular type of finder implementation it is using
  • The good solution: eliminate all lines of code such as:
    • MovieFinder f = new MyParticularMovieFinderImpl();
    • A component should NEVER create (instantiate) its dependencies
  • The solution is called “Inversion of Control”
    • A component does not create (instantiate) its dependencies but has someone else creating them for it

Concept of “Inversion of Control”

In the context of components: the application independently defines a set of components and their dependencies and the component framework (called container) uses this information to

  • Wire the components together at run-time
  • Call its code at specific times in the life cycle

Inversion of Control

Inversion of Control can be achieved through several patterns:

Dependency Injection

An Assembler instantiates concrete implementations and “injects” them into the component that needs them.

Forms of Dependency Injection

  • Constructor Injection
    • MovieLister has a constructor that will get the MovieFinderImplementation
  • Setter Injection
    • MovieLister has a setter method that will get the MovieFinderImplementation
  • Interface Injection
    • An interface InjectFinder, with method injectFinder, defined by the provider of the MovieFinder interface
    • MovieLister (and any class that wants to use a MovieFinder) needs to implement this interface

Component Containers

What has the DI pattern to do with component frameworks ?

  • The Assembler component of the DI pattern is called a Component Container and is part of the component framework
  • The assembler (Container) is generic (for any application), thus it:
    • Requires that components follow a certain convention (constructor, setter, injector interfaces)
    • Requires to be told (by code or configuration files) which implementation to associate with which interface

Example: Setter Injection with Spring

  1. Defining setters

Example: Setter Injection with Spring

  1. Describing the configuration(spring.xml)

bean与component都是Spring管理的软件构件,我们在demo中看到的controller是一种特殊的component。

Example: Setter Injection with Spring

  1. Start the container
public void testWithSpring() throws Exception{
  ApplicationContext ctx = new FileSystemXmlApplicationContext("spring.xml");
  MovieLister lister = (MovieLister) ctx.getBean("MovieLister");
  Movie[] movies = lister.moviesDirectedBy("Sergio Leone");
  assertEquals("Once Upon a Time in the West", movies[0].getTitle());
}
sa-spring/spring-bean

https://www.bilibili.com/video/BV1A7411F7Jh?p=5

3 Types of Configuration Metadata

  • XML-based configuration
  • Annotation-based configuration
  • Java-based configuration
sa-spring/cashregister

相关阅读

Spring Core Technologies/ The IoC Container

Spring 教程

小结

  • 软件构件与构件化开发
  • Spring构件化开发演示
  • 构件框架核心:反转控制/依赖注入
  • Spring bean/component

![height:350px](images/02-dependency-injection.png)

```xml <beans> <bean id="MovieLister" class="spring.MovieLister"> <property name="finder"> <ref local="MovieFinder" /> </property> </bean> <bean id="MovieFinder" class="spring.ColonMovieFinder"> <property name="filename"> <value>movies1.txt</value> </property> </bean> </beans> ```

![height:170px](images/02-start-context.png)