IOC 容器
1.1 容器初始化
- 由 BeanFactory 提供配置结构与基本功能,加载并初始化 Bean
- 由 ApplicationContext 保存并于 Spring 当中使用
1.2. Context 获取
AnnotationConfigApplicationContext
从一个或多个基于 Java 的配置类中加载 Spring 应用上下文,如下:
new AnnotationConfigApplicationContext(“com…TestConfig.class”)
AnnotationConfigWebApplicationContext
从一个或多个基于 Java 的配置类中加载 Spring Web 应用上下文
ClassPathXmlApplicationContext
从类路径下的一个或多个 XML 配置文件中加载上下文定义,把应用上下文的定义文件作为类资源 ,如下:
new ClassPathXmlApplicationContext(“classpath:spring-context.xml”);
FileSystemApplicationContext
从文件系统下的一个或多个 XML 配置文件中加载上下文定义,如下:
new FileSystmeXmlApplicationContext(“F:/…/appcontext.xml”);
XMLWebApplicationContext
从 Web 应用下的一个或多个 XML 配置文件中记载上下文定义
1.3. Spring注入
1.3.1 设值注入
通过对象引用与 set() 函数赋值的一种注入方式
<beans ...>
<bean id=[child_clazz_id] class=[child_clazz_path]></bean>
<bean id=[parent_clazz_id] class=[parent_clazz_path]>
<property name=[child_clazz_name] ref=[child_clazz_id]></property>
</bean>
</beans>
1.3.2 构造注入
通过对象引用注入构造函数赋值的一种注入方式
<beans ...>
<bean id=[child_clazz_id] class=[child_clazz_path]></bean>
<bean id=[parent_clazz_id] class=[parent_clazz_path]>
<constructor-arg name=[child_clazz_name] ref=[child_clazz_id]></constructor-arg>
<constructor-arg>
<!-- 数组参数赋值 <list>/<set> -->
<list>
<ref bean=[child_clazz_id]>
...
</list>
</constructor-arg>
</bean>
</beans>
Spring Bean 装配
创建应用组件之间写作的行为通常成为装配,配置方式有以下几种:
- 在 XML 中进行显式配置
- 构造器注入初始化 Bean
- < constructor-arg > 各元素使用
- Spring 3.0 命名空间使用,暂无法实现装配集合
- Spring util 命名空间使用
- 在 Java 中进行显式配置
- 隐式的 bean 发现机制和自动配置(推荐)
- 组件扫描(component scanning)
- 自动装配(autowring)
2.1 配置项
配置项 | 描述 |
---|---|
Id | 唯一标识 |
Class | 指定实例化类(必配) |
Scope | 范围/作用于 |
Constructor arguments | 构造函数 参数 |
Properties | 属性 |
Autowiring mode | 自动装配(模式) |
lazy-initialization mode | 懒加载(模式) |
Inistalization/destruction method | 初始化/销毁(模式) |
component-scan | 自动扫描(模式) |
2.2 作用域
作用域 scop | 描述 |
---|---|
singleton | 单例, 即 Bean 容器/测试案例 中只存在一个 |
prototype | 单次创建单次使用 |
request | 仅在 http 请求创建的实例中使用,有且只在当前 request 中有效 |
session | 同上,有且仅在当前 session 中有效 |
global session | 基于 portle 的 web 中有效 |
2.3 生命周期
2.3.1 初始化
- 接口覆盖方法
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean id="beanLifeCycle" class="main.lifecycle.BeanLifeCycle"></bean>
</beans>
public class BeanLifeCycle implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("bean afterPropertiesSet!");
}
@Override
public void destroy() throws Exception {
System.out.println("bean destroy!");
}
}
- init-method 全局/配置
<?xml version="1.0" encoding="UTF-8"?>
<beans ...
<!-- 全局实现 default-init-method="start" default-destory-method="stop" -->
>
<bean id="beanLifeCycle" class="main.lifecycle.BeanLifeCycle"
init-method="start" destroy-method="stop"></bean>
</beans>
public class BeanLifeCycle {
public void start() {System.out.println("bean start!");}
public void stop() {System.out.println("bean stop!");}
}
2.3.2 销毁(代码示例同上)
接口覆盖方法
destory-method 全局/配置
2.3.3 注意点
- 接口覆盖与 bean 配置将覆盖 beans 默认方法
- 接口覆盖实现优先于 bean 配置
- 无实现 beans 默认方法不影响 bean 的初始化
- 无实现 bean 配置方法将无法正常初始化 bean
2.4 Aware 接口实现
2.5 Autowiring 自动装配
Name | Describe | Use |
---|---|---|
No | 不执行操作 | default-autowire=”no” |
byname | 根据属性名称自动装配。此选项将检测容器并根据名字查找与属性完全一致的 bean ,并将其与属性自动装配 | default-autowire=”byName” |
byType | 匹配容器中类型一致的 bean;如果存在多个该类型 bean,则抛出异常,如果一个都没找到,则什么事都不发生 | default-autowire=”byType” |
constructor | 应用于构造参数,根据构造器参数类型 bean 类型匹配,如果没有找到与之类型一致的 bean ,则抛出异常 | default-autowire=”constructor” |
2.6 Resource,ResourceLoader
Name | Describe |
---|---|
UrlResource | URL对应的资源,根据一个 URL 地址即可构建 |
ClassPathResource | 获取类路径下的资源文件 |
FileSystemResoutce | 获取文件系统里的资源 |
ServletContextResource | ServletContext 封装的资源,用于访问 ServletContext 环境下的资源 |
InputStreamResoutce | 针对于输入流封装的资源 |
ByteArrayResource | 针对与直接数组封装的资源 |
applicationContext.getResource(“classpath:resources/myfile.txt”);
applicationContext.getResource(“file:E:\j2ee\SpringTest\src\resources\myfile.txt”);
applicationContext.getResource(“url:http://www.imooc.com/learn/196“);
2.7 注解与实现
注解类型 | 描述 |
---|---|
@Component @Named | 通用注解任意实体,可指定其初始化名称/ID(默认与类同名且首字母小写) |
@ComponentScan | 指定基础包名称 |
@Repository | 针对 DAO 类执行注解,即持久层 |
@Service | 针对 Service 类执行注解,即服务层 |
@Controller | 针对 Controller 类执行注解,即控制层(MVC) |
@Scope | 指定作用域[singleton/prototype/request/session/global session] |
@Required | 注解于 bean 的 setter 方法,如设置值为 false,Spring 会尝试执行自动装配,如果没有匹配的 bean,Spring 将会让该 bean 出于为装配状态。 |
@Autowired @Inject | 1.注解于 bean 的 setter 方法,构造器或成员变量 2.适用于存在异常抛出的函数处理 3.每个类只有一个构造器可被标记 4.注解含抽象类/接口的数组对象将包含所有已继承对象 |
@Order | 实现数组按序排序(对 Map 无效) |
@Qualifier | 1.用于指定 bean 实例/实现类,缩小指定范围 2.一般会与 @Autowired 配合使用 3.适用于构造参数或多参数方法 |
@Resource | 1.与 @Qulifier 同作用,但仅适用于只有 setter 方法或成员变量 2.注解时若无提供 name 的值则默认以变量名或方法名作为 bean 的命名 3.解析过程其实际上为 ApplicationContext 中的 CommonAnnotationBeanPostProcessor 发现并处理 |
@Configuration | 注解类相当与声明一个配置文件 |
@Bean | 1.用于适配和初始化一个由 SpringIoC 容器管理的新对象的方法 2.类似 XML 配置文件的 ‘bean’ 标签 3.可指定 bean 的 name,initMethod,destroyMethod 4.一般与 Configuration 配合使用 |
@ImprotResource | 1.类配置文件当中外部资源的引用 2.类似 XML 配置文件的 ‘context:property-placeholder’ 标签 |
@Value | 1.类配置文件当中对外部资源成员变量的声明 2.类似 XML 配置文件的 ‘property’ 标签 |
@PostConstruce | 初始化时调用 |
@PreDestroy | 销毁时调用 |
2.7.1 扫描注册与过滤
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 扫描路径 -->
<context:component-scan base-package=[scanPackage]
scope-resolver="Scope 策略"
scope-proxy="代理方式[no,interfaces,targetClass]">
<!-- 包含 -->
<context:include-filter type=[FilterType] expression=".*Stub.*Repositpry"/>
<!-- 排除 -->
<context:include-filter type=[FilterType] expression=""/>
</context:component-scan>
</beans>
FilterType | Example |
---|---|
annotation | org.example.SomeAnotation |
assignable | org.example.SomeClass |
aspectj | org.example..*Service+ |
regex | org.example.Dfault.* |
custom | org.example.MyTypeFilter |