Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6
3.4.3 使用depends-on
使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化。
多个bean之间用“,”、“;”、“ ”隔开。
<bean id="beanOne" class="ExampleBean" depends-on="manager"/> <bean id="manager" class="ManagerBean" />
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean> <bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
3.4.4 Lazy-initialized beans
声明lazy-init="true"之后,仅仅有在第一次请求的时候才会对bean进行初始化,不会在容器初始化的时候初始化。
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/> <bean name="not.lazy" class="com.foo.AnotherBean"/>
当然,假设一个“not lazy-initialized”bean依赖于一个“lazy-initialized”bean,那么ApplicationContext会在启动的时候创建“lazy-initialized”bean
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
3.4.5 Autowiring collaborators(自己主动装配合作者)
长处:
1、明显降低指定properties货构造器属性;
2、当对象更新时,能够自己主动更新配置而不须要手动改动配置
Table 3.2. Autowiring modes
| Mode | Explanation |
|---|---|
| no |
不自己主动装配。bean的引用必须通过ref元素。 |
| byName |
依据属性名称自己主动装配。某个bean定义为byName,而且它有一个叫master的属性,那么Spring查找定义为master属性的bean,然后将它注入进去 |
| byType |
假设某个bean的属性的类型存在的话,就用这个类型的对象注入,假设存在多个,那么抛出异常,假设没有匹配到的话,当做没有注入看待 |
| constructor |
与byType类似,只是是提供给构造器的參数 |
3.4.5.1 自己主动装配的约束与缺点
缺点:
1、properties和constructor-arg明白的依赖设置一般会覆盖自己主动装配。不能自己主动装配那些简单的properties,如primitives,String,Classes
2、自己主动装配没有显示配置来的准确;
下面暂缺
3.4.6 方法注入
这种方法放弃了IoC。通过实现ApplicationContextAware接口,然后通过setApplicationContext方法获取ApplicationContext,再通过getBean方法来获取。
// a class that uses a stateful Command-style class to perform some processing
package fiona.apple; // Spring-API imports
import org.springframework.beans.BeansException;
import org.springframework.context.Applicationcontext;
import org.springframework.context.ApplicationContextAware; public class CommandManager implements ApplicationContextAware { private ApplicationContext applicationContext; public Object process(Map commandState) {
// grab a new instance of the appropriate Command
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
return command.execute();
} protected Command createCommand() {
// notice the Spring API dependency!
return this.applicationContext.getBean("command", Command.class);
} public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}
Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6的更多相关文章
- Spring3.0官网文档学习笔记(一)
Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...
- Spring3.0官网文档学习笔记(七)--3.4.2
3.4.2 依赖与配置的细节 3.4.2.1 Straight values (primitives, Strings, and so on) JavaBeans PropertyE ...
- Spring3.0官网文档学习笔记(二)
1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包) spring-*.jar *号代表 ...
- Spring3.0官网文档学习笔记(四)--3.1~3.2.3
3.1 Spring IoC容器与Beans简单介绍 BeanFactory接口提供对随意对象的配置: ApplicationContext是BeanFactory的子接口.整合了Sp ...
- android官网文档学习笔记
1.android的四大组件的了大概功能 activity:负责显示界面,和用户交互. service:运行在后台. content provider:为程序app之间的数据访问提供接口. broad ...
- Spring Security 官网文档学习
文章目录 通过`maven`向普通的`WEB`项目中引入`spring security` 配置 `spring security` `configure(HttpSecurity)` 方法 自定义U ...
- mongodb官网文档阅读笔记:与写性能相关的几个因素
Indexes 和全部db一样,索引肯定都会引起写性能的下降,mongodb也没啥特别的,相对索引对读性能的提示,这些消耗通常是能够接受的,所以该加入的索引还是要加入.当然须要慎重一些.扯点远的,以前 ...
- (五)Spring Boot官网文档学习
文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...
- (二)Spring Boot 官网文档学习之入门
文章目录 Spring Boot 是什么 系统要求 Servlet 容器 Maven方式安装Spring Boot 编写第一个 Spring Boot 项目 原文:https://docs.sprin ...
随机推荐
- 发现一个好的开源项目:lomoX(挑着看,每天看一点,看一年就ok了)——用Webkit开发桌面软件,炫
http://www.oschina.net/p/lomox https://github.com/caidongyun/lomox 用Web技术做桌面客户端.虽然仍然免不了要分发客户端,但好处是,界 ...
- 【HDOJ】1204 糖果大战
题目本身不难.类似于dp.f(i)表示手中现有i颗糖果赢的概率,则下一局赢的概率是p(1-q),下一局输的概率是q(1-p),下一句平手的概率是1-p(1-q)-q(1-p),平手包括两人均答对或答错 ...
- Android开发UI之自定义视图属性
Android框架中,所有自定义的view类都继承自View,也可以继承Button等view的子类 为了允许ADT能够与view交互,必须提供一个能够获取Context和作为属性的Attribute ...
- uiview scale
http://stackoverflow.com/questions/3946797/cgaffinetransformmakescale-makes-uiview-jump-to-original- ...
- 了解SQL Server锁争用:NOLOCK 和 ROWLOCK 的秘密
关系型数据库,如SQL Server,使用锁来避免多用户修改数据时的并发冲突.当一组数据被某个用户锁定时,除非第一个用户结束修改并释放锁,否则其他用户就无法修改该组数据. 有些数据库,包括SQL Se ...
- 如何判断 Android 应用的 Apk 签名是否一致?
可以比对apk签名的fingerprint. 假定安装了JDK,如果想查HelloWorld.apk所使用的签名的fingerprint,可以这样做: 1. 查找apk里的rsa文件 (Windows ...
- HDOJ 1863
#include<stdio.h> #include<string.h> int father[105],depth[105]; int dist[105],map[101][ ...
- Linux内核结构分析与移植
Linux内核主要的5个部分是:进程调度,内存管理,虚拟文件系统,网络接口,进程通信. 这5个部分之间的关系如下: (1)进程调度部分负责控制进程对CPU的访问. (2)内存管理允许多个进程安全地共享 ...
- git高级应用
高级应用之一: 二分法查找错误版本: 当前日志 $ git log --oneline ccda9d2 added test1 dd518f7 test zu 88095f9 dasfdasf 3 ...
- 关于Eclispse连接Mysql的Jdbc
1.在Eclipse中新建Java工程 2.引入JDBC库(在bulid path 的extenrnal里) 3. 1)导入sql包(import java.sql.*) 2)加载(注册)mysql ...