Spring 学习很瘦身1
来源:互联网 发布:斐波那契数列java递归 编辑:程序博客网 时间:2024/06/02 03:30
- IOC 容器
- 1 容器初始化
- 2 Context 获取
- 3 Spring注入
- 31 设值注入
- 32 构造注入
- Spring Bean 装配
- 1 配置项
- 2 作用域
- 3 生命周期
- 31 初始化
- 32 销毁代码示例同上
- 33 注意点
- 4 Aware 接口实现
- 5 Autowiring 自动装配
- 6 ResourceResourceLoader
- 7 注解与实现
- 71 扫描注册与过滤
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 配置项
2.2 作用域
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 自动装配
2.6 Resource,ResourceLoader
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 注解与实现
@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>
Spring 学习很瘦身1
Spring 学习很瘦身2
- Spring 学习很瘦身1
- Spring 学习很瘦身2
- JVM学习-JVM瘦身
- spring boot jar包 瘦身
- 1Android APK瘦身
- 瘦身
- 瘦身
- Spring学习1-初识Spring
- spring学习1-spring简介
- Flex瘦身方案知多少(1)
- iOS9-by-Tutorials-学习笔记四:APP-瘦身
- Makefile 学习日记(三)——makefile的瘦身
- 移动应用瘦身学习总结 (Android&iOS)
- Spring学习(1)
- Spring学习笔记1
- Spring学习笔记1
- spring学习笔记1
- Spring学习笔记1
- 为什么我的电脑这么卡(system占用CPU这么高)
- 玩起 Centos 篇
- Gradle:String index out of range: 0
- Oracle数据库的Connection URL格式问题
- tips
- Spring 学习很瘦身1
- 最基本的8086汇编语言程序
- AS配置文件+gradle配置+混淆配置
- BZOJ-1023 [SHOI2008]仙人掌图
- C/C++ "\t"的用法
- 关闭445端口
- 二分贪心04
- 关于素数的简单算法整理
- Quartz 2D编程指南(4) - 颜色和颜色空间