[关闭]
@zhuanxu 2017-11-08T09:35:35.000000Z 字数 6364 阅读 1839

第1章:Spring 基础

JavaEE开发的颠覆者SpringBoot实战


spring 简史

  1. xml配置
    spring1.x,主要配置各种Bean
  2. 注解配置
    spring2.x,基本配置用xml,业务配置用注解
  3. Java 配置
    spring3.x,spring boot 都推荐使用 Java 配置

spring 概述

spring framework runtime
包括内容很多,每一块都能单独拿出来讲。

spring 在 idea 中的搭建

为了体验spring,所以建立的是一个 maven quick start 的项目,建立后的 pom.xml 配置如下:

  1. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.wisely</groupId>
  5. <artifactId>highlight_spring5_idea</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>highlight_spring5_idea</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <java.version>1.8</java.version>
  13. </properties>
  14. <dependencies>
  15. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-context</artifactId>
  19. <version>5.0.1.RELEASE</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>3.8.1</version>
  25. <scope>test</scope>
  26. </dependency>
  27. </dependencies>
  28. <build>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.apache.maven.plugins</groupId>
  32. <artifactId>maven-compiler-plugin</artifactId>
  33. <version>3.7.0</version>
  34. <configuration>
  35. <source>${java.version}</source>
  36. <target>${java.version}</target>
  37. <!-- put your configurations here -->
  38. </configuration>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

spring 基础配置

spring 框架四大原则:

  1. 使用 POJO(Plain Old Java Object) 进行轻量级和最小侵入式开发
  2. 依靠依赖注入和接口实现松耦合
  3. 使用 AOP 和默认习惯进行声明式编程
  4. 使用 AOP 和模板减少代码冗余

依赖注入

依赖注入是通过配置+容器的方式实现的,配置可以通过 xml 配置、注解配置、java 配置实现,这些配置称为元配置数据,这些元配置本身不会有运行的能力,是通过程序解析代码后,根据元数据来做相应操作。

先介绍一个概念 Beans,Beans 是被 spring 容器管理的 POJO,Beans 以及 Beans 之间的依赖关系都是通过元数据来配置的,而这些元数据的使用方则是称为 IoC container 的容器。

Spring Framework 中 IoC container 主要代码是在 org.springframework.beansorg.springframework.context 两个包中。

在前面设置maven的依赖的时候,我们导入了 context, 看下依赖图:

dependence

可以看到org.springframework.context本身依赖于org.springframework.beans

IoC 容器具体到具体的类是BeanFactory,里面定义了基本的接口来获取 Beans,而ApplicationContext是为了企业级应用而对BeanFactory的一个扩展,具体可以看依赖图:
image_1bucpv7ih1nr81g9p1mfj14vv1kvlm.png-61.5kB

接口org.springframework.context.ApplicationContext的职责是负责Beans的实例化,配置,组装等工作,而这些对Beans的具体操作都是通过配置数据来控制的。因为ApplicationContext是一个接口,只要实现这个接口,就可以作为一个容器来使用,常用的容器有ClassPathXmlApplicationContextFileSystemXmlApplicationContext

下面是一个ApplicationContext工作的概括图:
image_1bucqalkp17dh13m9utv1ku71kfq13.png-10.1kB
ApplicationContext通过我们配置的元数据,棒我们实例化、配置好我们需要的Beans,当具体的一个ApplicationContext对象生成结束的时候,我们的系统也就做好运行的准备了。

元配置数据

配置有3类方式:xml、注解和java,我们此处主要讲xml。一个典型的xml配置文件结构如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="..." class="...">
  7. <!-- collaborators and configuration for this bean go here -->
  8. </bean>
  9. <bean id="..." class="...">
  10. <!-- collaborators and configuration for this bean go here -->
  11. </bean>
  12. <!-- more bean definitions go here -->
  13. </beans>

id 是对Bean的唯一标识,class则是具体的类名。

容器实例化

可以通过下面的代码得到一个具体的容器:

  1. ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

我们可以看到对于Beans的声明我们可以放到多个不同的xml文件中,还有另一个方式是在一个xml中导入其他的xml文件,如下:

  1. <beans>
  2. <import resource="services.xml"/>
  3. <import resource="resources/messageSource.xml"/>
  4. <bean id="bean1" class="..."/>
  5. <bean id="bean2" class="..."/>
  6. </beans>

Bean 概览

IoC 容器管理的Beans都是通过xml中定义的,在IoC中这些定义被表示为BeanDefinition对象,包含的信息有:

下面是一个具体属性和作用:
image_1bud0pssh19lt1kh0vgc1sb4ugj9.png-79.8kB

name

一个Bean可以有多个名字,也可以没有名字,在xml中可以通过id or name来指定,id只能指定唯一一个名字,name可以通过","或者";"或者空格将多个名字隔开,另一种指定名字的方式是通过alias,如下:

  1. <alias name="fromName" alias="toName"/>

如果我们没有指定id或者name,IoC容器会自动生成一个名字。

对于不指定名字的使用场景是:inner beans 和 autowiring collaborators,后面介绍。

实例化

实例化一个Bean的方式有两种

先看第一种,构造函数的方式。

构造函数的方式一般要求Bean有个默认的构造函数(没有任何参数),然后通过set方式来设置Bean的属性,配置如下:

  1. <bean id="exampleBean" class="examples.ExampleBean"/>
  2. <bean name="anotherExample" class="examples.ExampleBeanTwo"/>

如果构造函数需要一些依赖注入,稍后介绍怎么完成。

另外一种方式是静态工厂,看配置:

  1. <bean id="clientService"
  2. class="examples.ClientService"
  3. factory-method="createInstance"/>
  4. public class ClientService {
  5. private static ClientService clientService = new ClientService();
  6. private ClientService() {}
  7. public static ClientService createInstance() {
  8. return clientService;
  9. }
  10. }

另一种工厂是来自于另一个Bean的方法

  1. <!-- the factory bean, which contains a method called createInstance() -->
  2. <bean id="serviceLocator" class="examples.DefaultServiceLocator">
  3. <!-- inject any dependencies required by this locator bean -->
  4. </bean>
  5. <!-- the bean to be created via the factory bean -->
  6. <bean id="clientService"
  7. factory-bean="serviceLocator"
  8. factory-method="createClientServiceInstance"/>
  9. public class DefaultServiceLocator {
  10. private static ClientService clientService = new ClientServiceImpl();
  11. public ClientService createClientServiceInstance() {
  12. return clientService;
  13. }
  14. }

依赖关系

依赖注入的种类

常见的依赖注入有构造函数注入和set方法注入,先来看构造函数注入。
构造函数注入
假设我们有下面的类:

  1. package x.y;
  2. public class Foo {
  3. public Foo(Bar bar, Baz baz) {
  4. // ...
  5. }
  6. }

我们可以配置下面的xml,通过标签<constructor-arg/>来指定:

  1. <beans>
  2. <bean id="foo" class="x.y.Foo">
  3. <constructor-arg ref="bar"/>
  4. <constructor-arg ref="baz"/>
  5. </bean>
  6. <bean id="bar" class="x.y.Bar"/>
  7. <bean id="baz" class="x.y.Baz"/>
  8. </beans>

另外一种是构造函数依赖的是普通的类型,如int,String等,看例子:

  1. package examples;
  2. public class ExampleBean {
  3. // Number of years to calculate the Ultimate Answer
  4. private int years;
  5. // The Answer to Life, the Universe, and Everything
  6. private String ultimateAnswer;
  7. public ExampleBean(int years, String ultimateAnswer) {
  8. this.years = years;
  9. this.ultimateAnswer = ultimateAnswer;
  10. }
  11. }

此时参数不再是Bean了,我们通过type,value的方式实现:

  1. <bean id="exampleBean" class="examples.ExampleBean">
  2. <constructor-arg type="int" value="7500000"/>
  3. <constructor-arg type="java.lang.String" value="42"/>
  4. </bean>

也可以通过index-value的方式:

  1. <bean id="exampleBean" class="examples.ExampleBean">
  2. <constructor-arg index="0" value="7500000"/>
  3. <constructor-arg index="1" value="42"/>
  4. </bean>

另外一种是在开启debug模式下使用的name-value,

  1. <bean id="exampleBean" class="examples.ExampleBean">
  2. <constructor-arg name="years" value="7500000"/>
  3. <constructor-arg name="ultimateAnswer" value="42"/>
  4. </bean>

当然不想开启debug,也可以通过java的注释@ConstructorProperties来做:

  1. package examples;
  2. public class ExampleBean {
  3. // Fields omitted
  4. @ConstructorProperties({"years", "ultimateAnswer"})
  5. public ExampleBean(int years, String ultimateAnswer) {
  6. this.years = years;
  7. this.ultimateAnswer = ultimateAnswer;
  8. }
  9. }

下面总结下依赖解析的整个过程:

  1. ApplicationContext创建并且读取元配置信息生成BeanDefinition对象
  2. 对于每个Bean,其依赖通过properties,constructor arguments ,static-factory method来声明的。
  3. 每个properties,constructor arguments是以值的形式或者对其他Bean的引用提供的。
  4. 每个properties,constructor arguments如果是值的话,能自动转换到对应的类型(int,long,boolean等)。

下一篇将会详细介绍下依赖。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注