Spring 学习笔记 ----依赖注入
依赖注入 有三种方式,本文只学习下属性注入.
属性注入
属性注入的实例
package com.spring.model; public class Car {
/**
* 系统为自动我们提供一个无参的构造方法
*/
private String brand;
private String color;
private int maxSpeed;
public void setBrand(String brand) {
this.brand = brand;
}
public void setColor(String color) {
this.color = color;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
<bean id="car" class="com.spring.model.Car" >
<property name="maxSpeed"><value>200</value></property>
<property name="brand"><value>红旗CAT2</value> </property>
<property name="color"><value>红色</value></property>
</bean>
我们配置了一个Car Bean,并为该Bean的三个属性提供了属性值。具体来说,Bean的每一个属性对应一个<property>标签,name为属性的名称,在Bean实现类中拥有与其对应的Setter方法:maxSpeed对应setMaxSpeed。
注入参数详解
字面值
引用其它Bean
package com.spring.model; public class Boss {
private Car car=new Car(); public Car getCar() {
return car;
}
//设置Car属性
public void setCar(Car car) {
this.car = car;
}
..........
}
<!-- 1 car bean -->
<bean id="car" class="com.spring.model.Car" />
<bean id="boss" class="com.spring.model.Boss">
<property name="car">
<!--2 引用1处定义的car bean -->
<ref bean="car"/>
</property>
</bean>
<ref>元素可以通过以下三个属性引用容器中的其它Bean。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--1 在父容器定义的bean -->
<bean id="car" class="com.spring.model.Car">
<property name="brand">
<value>红旗</value>
</property>
<property name="color" value="红色"/>
<property name="maxSpeed" value="200"/>
</bean>
</beans>
beans2.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--该bean和父容器的car bean具有相同的id-->
<bean id="car" class="com.spring.model.Car">
<property name="brand">
<value>白旗CAR</value>
</property>
<property name="color" value="红色"/>
<property name="maxSpeed" value="200"/>
</bean> <bean id="boss" class="com.spring.model.Boss">
<property name="car">
<!-- 引用父容器的car,而非beans2.xml定义的car ,如果采用<ref bean="car" 则会引用本容器的car/> -->
<ref parent="car"/>
</property>
</bean>
</beans>
在beans1.xml中定义了一个car bean,在beans2.xml也定义了一个car Bean。分别通过父子容器加载beans1.xml和beans2.xml,beans2.xml中的boss通过<ref parent="car">将引用到父容器中的car.
@Test
public void test1(){
//父容器
ApplicationContext pFactory=new ClassPathXmlApplicationContext("beans1.xml");
//指定pFactory为该容器的父容器
ApplicationContext factory=new ClassPathXmlApplicationContext(new String[]{"beans2.xml"},pFactory); Boss boss=(Boss) factory.getBean("boss");
System.out.println(boss.getCar().getBrand()); }
运行后结果如下:
红旗
内部Bean
<bean id="boss" class="com.spring.model.Boss">
<property name="car">
<bean class="com.spring.model.Car">
<property name="maxSpeed" value="200"/>
<property name="color" value="白色"/>
</bean>
</property>
</bean>
内部bean和JAVA代码中匿名内部类很相似,即没有名字,也不能被其它bean引用,只能在声明处为外部bean提供实例注入。 内部bean即使提供了id、name、scope属性,也会被忽略,scope默认为prototype类型.
null值
<property name="barnd">
<value><null/> </value>
</property>
级联属性
<bean id="boss2" class="com.spring.model.Boss">
<!-- 以原点的方式定义级别属性 -->
<property name="car.brand" value="吉利Ct50"/>
</bean>
按照上面的配置,Spring将调用Boss.getCar.setBaran("吉利Ct50"),方法进行属性的注入操作。
集合类型属性
package com.spring.model; import java.util.ArrayList;
import java.util.List; public class Boss { private List favourites=new ArrayList(); public List getFavourites() {
return favourites;
}
public void setFavourites(List favourites) {
this.favourites = favourites;
}}
对应Spring 中的配置片段如下所示:
<bean id="boss" class="com.spring.model.Boss">
<property name="favourites">
<list>
<value>看报</value>
<value>赛车</value>
<value>高尔夫</value>
</list>
</property>
</bean>
List属性既可以通过<value>注入字符串,也可以通过<ref>注入容器中其它的Bean.
<bean id="boss" class="com.spring.model.Boss">
<property name="favourites">
<set>
<value>看报</value>
<value>赛车</value>
<value>高尔夫</value>
</set>
</property>
</bean>
Map
package com.spring.model; import java.util.HashMap;
import java.util.Map; public class Boss {
private Map jobs=new HashMap(); public Map getJobs() {
return jobs;
} public void setJobs(Map jobs) {
this.jobs = jobs;
} }
在配置文件中,可以通过如下方式为jobs属性提供配置值:
<bean id="boss" class="com.spring.model.Boss">
<property name="jobs">
<map>
<!-- Map 第一个元素 -->
<entry>
<key><value>AM</value></key>
<value>会见客户</value>
</entry>
<!-- Map第二个元素 -->
<entry>
<key><value>PM</value></key>
<value>公司内部会议</value>
</entry>
</map>
</property>
</bean>
假设某一Map元素的键和值都是对象,则可以采取以下的配置方式:
<entry>
<key><ref bean="keyBean"/> </key>
<ref bean="valueBean"/>
</entry>
Properties
package com.spring.model; import java.util.Properties; public class Boss {
private Properties mails=new Properties(); public Properties getMails() {
return mails;
} public void setMails(Properties mails) {
this.mails = mails;
} }
下面的配置片段为mails提供了配置:
<bean id="boss" class="com.spring.model.Boss">
<property name="mails">
<props>
<prop key="jobMail">john-office@taobao.com</prop>
<prop key="lifeMail">john-life@taobao.com</prop>
</props>
</property>
</bean>
因为Properties键值对只能是字符串,因此其配置比Map的配置要简单一些,注意值的配置没有<value>子元素标签.
强类型集合
package com.spring.model; import java.util.HashMap;
import java.util.Map; public class Boss {
private Map<String,Integer> jobTime =new HashMap<String, Integer>(); public Map<String, Integer> getJobTime() {
return jobTime;
} public void setJobTime(Map<String, Integer> jobTime) {
this.jobTime = jobTime;
}
}
在Spring中的配置和非强类型集合相同:
<bean id="boss" class="com.spring.model.Boss">
<property name="jobTime">
<map>
<entry>
<key> <value>会见客户</value></key>
<value>124</value>
</entry>
</map>
</property>
</bean>
但Spring 容器在注入强类型集合时,会判断元素的类型,将设置值转换为对应的数据类型。如上面代码中的设置项124将被转换为Integer类型.
集合合并
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 父bean -->
<bean id="parentBoss" abstract="true" class="com.spring.model.Boss">
<property name="favorites">
<set>
<value>看报</value>
<value>赛车</value>
<value>高尔夫</value>
</set>
</property>
</bean> <!-- 子bean -->
<bean id="childBoss" class="com.spring.model.Boss" parent="parentBoss">
<property name="favorites">
<set merge="true">
<value>爬山</value>
<value>游泳</value>
</set>
</property>
</bean>
</beans>
在代码清单中,通过merge="true"属性指定子<bean>和父<bean>中的同名属性值进行合并,即子<bean>的favourites元素将最终拥有5个元素。如果设置merge="false",则不会和父<bean>同名集合属性进行合并,即子Bean的favourites属性集合只有两个元素.
@Test
public void test2(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans2.xml");
Boss boss=(Boss) ctx.getBean("childBoss");
Set set=boss.getFavorites();
System.out.println(set);
}
运行后结果如下:
[看报, 赛车, 高尔夫, 爬山, 游泳]
Spring 学习笔记 ----依赖注入的更多相关文章
- Spring学习笔记--依赖注入
依赖注入和控制反转:http://baitai.iteye.com/blog/792980出自李刚<轻量级 Java EE 企业应用实战> Java应用是一种典型的依赖型应用,它就是由一些 ...
- Spring 学习之依赖注入
什么是依赖关系? 纵观所有的Java 应用,从基于Applet的小应用到多层次结构的企业级别的应用,他们都是一种典型的依赖性应用,也就是由一些互相协作的对象构成的,Spring把这种互相协作的关系称之 ...
- Spring学习(一)---依赖注入和控制反转
Spring Spring是一个从实际开发中抽出来的框架,因此它完成了大量开发中的通用步骤,留给开发者的仅仅是与特定应用相关的部分,从而大大提高了企业应用的开发效率. Spring为企业应用的开发提供 ...
- spring学习之依赖注入DI与控制反转IOC
一 Ioc基础 1.什么是Ioc? Ioc(Inversion of Control)既控制反转,Ioc不是一种技术,而是一种思想,在Java开发中意味着将设计好的对象交给容器来进行控制,并不是像传统 ...
- net5学习笔记---依赖注入
小王的故事 小王去上班 小王是个程序员,每个工作日他都要去上班,诸多交通工具他最喜欢的交通工具是骑电车.在骑行的过程中放空自己使他很快. 突然有一天天气预报说近期有很大的雨,小王再想骑电车去上 ...
- spring学习 五 依赖注入的方式
依赖注入有两种方式: 1 构造注入,如果<bean>标签下使用<contructor-arg>,则是构造注入 2 setter注入,就是调用setter方法注入,如果<b ...
- Spring学习——DI(依赖注入)
IOC容器,处理对象依赖关系 IOC与DI: IOC :是一个容器,创建对象的容器 DI :在容器创建对象后,处理对象的依赖关系,也叫依赖注入! 方式1:通过set方法注入值 可以给普通属性.集合属性 ...
- Spring学习笔记--构造器注入
之前讲到的名为"duke"的bean有一个私有成员变量beanBags代表这个杂技师bean的一次性能够抛出的最多的数量,Juggler有一个构造函数,构造函数的第一个参数(这里只 ...
- Spring学习--泛型依赖注入
暂时没有搞懂.
随机推荐
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依 ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- 关于android app签名文件获取sha1和MD值
最近在做百度地图的嵌入,因为从同事接手的android app,所以第一次接触android的签名. 总的来说签名还比较简单,我用的是eclipse ADT自带的签名工具来做的签名,方法如下: 选择项 ...
- Linux Debugging(四): 使用GDB来理解C++ 对象的内存布局(多重继承,虚继承)
前一段时间再次拜读<Inside the C++ Object Model> 深入探索C++对象模型,有了进一步的理解,因此我也写了四篇博文算是读书笔记: Program Transfor ...
- mysql的基本使用命令
启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库:show datab ...
- Linux IPC实践(1) -- 概述
进程的同步与互斥 进程同步: 多个进程需要相互配合共同完成一项任务. 进程互斥: 由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥;系统中某些 ...
- (五十六)iOS多线程之NSOperation
NSOpertation是一套OC的API,是对GCD进行的Cocoa抽象. NSOperation有两种不同类型的队列,主队列和自定义队列. 主队列运行于主线程上,自定义队列在后台运行. [NSBl ...
- python的sorted
读入后,要进行组内排序,按groupseq字段排序后,然后统计前后两个项的个数,累加到全局. sorted函数使用如下: def sortlist(alllist): sorted_key1_ ...
- Android scrollview嵌套webview滑动冲突的解决方案
在Android开发中有时我们需要在scrollview中嵌套webview这时你会发现这两者的滑动事件产生了冲突导致:webview很难被滑动,即使被滑动了一点也非常不顺畅.解决方案也比较简单只需要 ...
- 新手自定义view练习实例之(一) 泡泡弹窗
转载请注明出处:http://blog.csdn.net/wingichoy/article/details/50455412 本系列是为新手准备的自定义view练习项目(大牛请无视),相信在学习过程 ...