Spring框架在依赖注入方面是非常灵活和强大的,多了解点一些注入的方式、方法,绝对能优化配置。

idref

idref属性可以传入一个bean的名称,虽然它是指向一个bean的引用,但是得到的是该bean的id名。

1
2
3
4
5
6
7
8
9
10
11
<beans>

    <bean id="movieService" class="DefaultMovieService"/>

    <bean id="cinema" class=“Cinema">
<property name="serviceRef">
<idref bean="movieService"/>
</property>
</bean> </beans>

它和直接设置serviceRef属性的value为movieService的区别是前者是能够保证必须有一个名movieService的bean存在于当前的spring容器中,如果没有则Spring容器会在初始化阶段就会报错;而后者仅仅是一个字符串,spring容器在初始化不会做任何检查,很可能将异常推后到运行时抛出。

内部bean

bean的定义是可以嵌套的。

1
2
3
4
5
6
<beans>
<bean id=“movieService" class="DefaultMovieService"/>
<bean id="cinema" class=“Cinema">
<property name="movieService" ref=“movieService"/>
</bean>
</beans>

可以改为这种方式。

1
2
3
4
5
    <bean id="cinema" class="Cinema">
<property name="movieService">
<bean class="DefaultMovieService"/>
</property>
</bean>

这样内部bean就不需要一个名字的,当然这也意味着它无法被其他bean引用了。当然即使你给它起了名字,Spring容器也会忽略这个名字,其他bean也无法引用它。neibubean的scope始终和waibubean的scope保持一致。

对集合的注入

Java的集合框架中包含很多集合元素,比如List、Map、Set等。Spring支持对这些集合元素的注入。

1
2
3
4
5
6
7
8
    <bean id="accountService" class="AccountService">
<property name="accounts">
<map>
<entry key="bowen" value="1234"/>
<entry key="tom" value="3456"/>
</map>
</property>
</bean>
1
2
3
4
5
6
7
8
9
    <bean id="cookbook" class="Cookbook">
<property name="recipe">
<list>
<value>noodle</value>
<value>rice</value>
<value>meat</value>
</list>
</property>
</bean>

还可以直接配置java的Properties。

1
2
3
4
5
6
7
8
9
    <bean id="databaseSource" class="DatabaseSource">
<property name="source">
<props>
<prop key="port">2012</prop>
<prop key="host">localhost</prop>
<prop key="schema">db1</prop>
</props>
</property>
</bean>

嵌套属性名注入

Spring支持使用嵌套属性注入值。

Restaurant.java
1
2
3
4
5
6
7
8
9
10
11
12
public class Restaurant {

    private Person manager;

    public Person getManager() {
return manager;
} public void setManager(Person manager) {
this.manager = manager;
}
}
Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Person {

    private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
1
2
3
4
5
6
    <bean id="restaurant" class="huangbowen.net.service.Restaurant">
<property name="manager">
<bean class="huangbowen.net.PAndCNamespace.Person"/>
</property>
<property name="manager.age" value="20"/>
</bean>

需注意除了最后一个属性,其他属性不能为空值。

使用depends-on属性

当你初始化一个bean时,需要另一个bean先被初始化,这种情况很常见。虽然Spring在最大程度上能自动按照你期望的顺序来初始化bean(比如构造器注入的bean会优先初始化),但是不保证总能符合你的心意。你可以使用depends-on属性来显示指定bean的初始化顺序。

1
2
3
4
5
6
7
8
9
10
11
12
<beans>

   <bean id="movieService" class="DefaultMovieService"/>

    <bean id="cinema" class=“Cinema” depends-on="movieService">
<property name="serviceRef">
<idref bean="movieService"/>
</property>
</bean> </beans>

也可以同时deppends-on多个对象。

1
2
3
4
5
    <bean id="cinema" class=“Cinema” depends-on=“movieService,accountService">
<property name="serviceRef">
<idref bean="movieService"/>
</property>
</bean>

Spring-Context之八:一些依赖注入的小技巧的更多相关文章

  1. MEF依赖注入调试小技巧!

    自从哥的项目使用MEF以来,天天那个纠结啊,甭提了.稍有错误,MEF就报错,但就不告诉你哪错了,大爷的. 后来看了MEFX的相关调试方法,感觉也不太理想,根本不够直观的看到错误原因,也许是没有深入学习 ...

  2. (转)Spring读书笔记-----Spring核心机制:依赖注入

    Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...

  3. spring 控制反转与依赖注入原理-学习笔记

    在Spring中有两个非常重要的概念,控制反转和依赖注入:控制反转将依赖对象的创建和管理交由Spring容器,而依赖注入则是在控制反转的基础上将Spring容器管理的依赖对象注入到应用之中: 所谓依赖 ...

  4. Spring核心机制:依赖注入

    转载:http://www.cnblogs.com/chenssy/ Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因 ...

  5. Spring读书笔记-----Spring核心机制:依赖注入

    spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.今天就和大家一起来学习一下 依赖注入的基本概念 依赖注入(Dependecy Injection),也称为IoC(I ...

  6. Spring源码剖析依赖注入实现

    Spring源码剖析——依赖注入实现原理 2016年08月06日 09:35:00 阅读数:31760 标签: spring源码bean依赖注入 更多 个人分类: Java   版权声明:本文为博主原 ...

  7. 回客科技 面试的 实现ioc 容器用到的技术,简述BeanFactory的实现原理,大搜车面试的 spring 怎么实现的依赖注入(DI)

    前言:这几天的面试,感觉自己对spring 的整个掌握还是很薄弱.所以需要继续加强. 这里说明一下spring的这几个面试题,但是实际的感觉还是不对的,这种问题我认为需要真正读了spring的源码后说 ...

  8. Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

    Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...

  9. Spring的核心机制依赖注入

    原文地址:http://developer.51cto.com/art/200610/33311.htm 本文主要讲解依赖注入(设值注入.构造注入),作用是可以使Spring将各层的对象以松耦合的方式 ...

随机推荐

  1. make: Nothing to be done for `first'

    在qt目录下make后出现以下错误: make: Nothing to be done for `first' 解决:将你当前目录下的,删除你程序主要的 *.cpp 和 *.h文件以外的所有文件. 接 ...

  2. Trie树的创建、插入、查询的实现

    原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=3807947 1.什么是Trie树 Tr ...

  3. SVN服务器的配置(简单易懂,带配置文件,有注释)

    这两天在服务器搭建了一个SVN服务器,一些经验,也留作后用把,有不详细的欢迎批评指正 另外关于子目录的访问配置,这块我还是不懂,希望有前辈能教我一下 1.安装SVN Serveryum install ...

  4. LNMP脚本安装

    #!/bin/bash#nginx:nginx-1.8.0.tar.gz#mysql:mysql-5.5.50.tar.gz#php:php-5.5.31.tar.gz#the software pa ...

  5. js queue dequeue clearQueue stop

      一.queue( [queueName ], newQueue ) 操作欲执行队列方法 第一个参数是队列名称,不写的话默认是fx 第二个参数可以是一个函数数组,存放所有队列函数,也可以是一个回掉函 ...

  6. mysql 5.7开启并行复制

    开启多线程复制,默认关键的参数有两个: mysql> show variables like 'slave_parallel_%'; +------------------------+---- ...

  7. IPv4头部结构

    2.2 IPv4头部结构 2.2.1 IPv4头部结构 IPv4的头部结构如图2-1所示.其长度通常为20字节,除非含有可变长的选项部分. 4位版本号(version)指定IP协议的版本.对IPv4来 ...

  8. FreeRTOS run on eclipse

    所需软件包: FreeRTOS.7.1.0.7zeclipse-cpp-helios-SR2-win32.zipTDM-GCC-32(版本任意吧..但同平台就选择一致的,32位系统就一致32位的软件, ...

  9. [UCSD白板题 ]Small Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  10. 使用 AForge.NET 做视频采集

    AForge.NET 是基于C#设计的,在计算机视觉和人工智能方向拥有很强大功能的框架.btw... it's an open source framework. 附上官网地址: http://www ...