Spring-Context之八:一些依赖注入的小技巧
2014-03-30 16:43
459 查看
Spring框架在依赖注入方面是非常灵活和强大的,多了解点一些注入的方式、方法,绝对能优化配置。
它和直接设置serviceRef属性的value为movieService的区别是前者是能够保证必须有一个名movieService的bean存在于当前的spring容器中,如果没有则Spring容器会在初始化阶段就会报错;而后者仅仅是一个字符串,spring容器在初始化不会做任何检查,很可能将异常推后到运行时抛出。
可以改为这种方式。
这样内部bean就不需要一个名字的,当然这也意味着它无法被其他bean引用了。当然即使你给它起了名字,Spring容器也会忽略这个名字,其他bean也无法引用它。neibubean的scope始终和waibubean的scope保持一致。
还可以直接配置java的Properties。
Restaurant.java
Person.java
需注意除了最后一个属性,其他属性不能为空值。
也可以同时deppends-on多个对象。
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> |
内部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> |
对集合的注入
Java的集合框架中包含很多集合元素,比如List、Map、Set等。Spring支持对这些集合元素的注入。1 2 3 4 5 67 | <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 67 | <bean id="cookbook" class="Cookbook"> <property name="recipe"> <list> <value>noodle</value> <value>rice</value> <value>meat</value> </list> </property> </bean> |
1 2 3 4 5 67 | <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 1112 | public class Restaurant { private Person manager; public Person getManager() { return manager; } public void setManager(Person manager) { this.manager = manager; } } |
1 2 3 4 5 6 7 8 9 10 1112 | 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 1112 | <beans> <bean id="movieService" class="DefaultMovieService"/> <bean id="cinema" class=“Cinema” depends-on="movieService"> <property name="serviceRef"> <idref bean="movieService"/> </property> </bean> </beans> |
1 2 3 4 5 | <bean id="cinema" class=“Cinema” depends-on=“movieService,accountService"> <property name="serviceRef"> <idref bean="movieService"/> </property> </bean> |
相关文章推荐
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- Spring IOC/BeanFactory/ApplicationContext的工作流程/实现原理/初始化/依赖注入源码详解
- Spring-Context的注解实现依赖注入功能
- Spring-Context之六:基于Setter方法进行依赖注入
- Spring中利用AOP对领域对象(非依赖注入的对象,如new操作符、反射的方式生存的对象)下面是spring配置文件的一部分 <context:load-time-weaver /> <be
- Spring整合Hibernate(1)-----applicationContext.xml 设值注入依赖方式
- Spring IoC 依赖注入 BeanFactory ApplicationContext WebApplicationContext
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- 【Spring】Spring依赖注入IOC的设值注入setter
- Spring依赖注入 — 注入依赖检查
- Spring 通过【构造函数】注入依赖
- java web之路 spring 基于构造函数的依赖注入
- Spring-依赖注入
- 传智播客Spring2.5视频教程_编码剖析Spring依赖注入的原理 2
- spring依赖注入原理(转载)
- spring依赖注入失效极度无语的,不知道为啥的
- Spring依赖注入:注解注入总结
- Spring依赖注入
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现
- Spring 有生命周期的bean的依赖注入