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学习--泛型依赖注入
暂时没有搞懂.
随机推荐
- Spark内存管理-UnifiedMemoryManager和StaticMemoryManager
在Spark-1.6.0中,引入了一个新的参数spark.memory.userLegacyMode(默认值为false),表示不使用Spark-1.6.0之前的内存管理机制,而是使用1.6.0中引入 ...
- Xcode中为何要为设置bundle和App分别设置两份一样的图片资源
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在App设置的bundle中有时也会用到图片资源,而在 ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- love~LBJ,奥布莱恩神杯3
时间:2016年6月20日8:00:地点:美国金州甲骨文(Oracle)球馆:事件:G7大战,又一次见证伟大赛季奥布莱恩神杯得主--金州勇士VS克利夫兰骑士...... 可以说,这是NBA以来 ...
- Hessian源码分析--HessianProxyFactory
HessianProxyFactory是HessianProxy的工厂类,其通过HessianProxy来生成代理类. 如下面代码: HessianProxyFactory factory = new ...
- Nginx+PHP-FPM的域Socket配置方法
1什么是域Socket "Unix domain socket 或者 IPCsocket 是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信.与管道相比,Unix domain ...
- 使用Mediaplay类写一个播放器
我们知道android本身播放视频的的能力是有限的..先来一个Demo 另附我的一个还未成熟的播放器,下载地址:http://www.eoemarket.com/soft/370334.html,正在 ...
- Mybatis执行Executor(一)
在DefaultSqlSession中我们可以看到一系列的增删改查操作的其实都是在调用Executor的接口,Mybatis对外统一提供了一个操作接口类Executor,提供的接口方法有update. ...
- WIP完工入库及完工退回的几个重要问题
1.必须向CST_COMP_SNAP_INTERFACE表中插入此工单所有工序的数据(也就是说同样的工单插入多条,只是工序号不一样) 标准文档: Note: If there are multiple ...
- 尚学堂马士兵struts2 课堂笔记(二)
14通配符问题 其实这个问题看一个例子就ok <package name="actions" extends="struts-default" names ...