码迷,mamicode.com
首页 > 编程语言 > 详细

Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6

时间:2014-06-22 06:28:01      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:spring3.0   官网文档   学习笔记   

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,布布扣,bubuko.com

Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6

标签:spring3.0   官网文档   学习笔记   

原文地址:http://blog.csdn.net/abauch_d/article/details/32966195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!