spring笔记之二:控制反转注入参数详解
2013-12-28 21:36
429 查看
在Spring配置文件中,用户不但可以将String,int等字面值注入到Bean中,还可以将集合、Map等类型的数据注入到Bean中。
等类型都是可以采用字面值注入方式。
针对<bean>进行属性说明:
id和name:都是用来指定bean的标示符,必须唯一;id必须唯一;name可以使用多个标示符。
class:全类名。
parent:指定父类的bean名称。
scope:用来设置bean的创建模式,其中包含singleton,prototype,request、session、global session。默认为singleton。
lazy-init:设置该bean是否迟延加载。除非数据量非常大,一般不用。
init-method:设置bean的初始化方法。
destory-method:设置bean销毁的方法。
针对<constructor-arg>进行属性说明:
index:指定参数的序号。
type:指定参数的类型。
ref:用于定义对其他bean实体的引用。
value:包含的是一个属性值得字符串值。
针对<property>进行属性说明:
name:指定javabean的属性名称。
ref:用于指定对其他bean的引用。
value:包含的是一个属性值得字符串值。
bean:指定所要引用的bean的实例名称。
local:指定所要引用的bean的实例名称。
parent:指定所要引用的bean的实例名称,可以引用外部的factory,就如引用的bean不在同一个配置文件中。
区别:bean和local:
bean:必须引用实例id的属性,并且引用的bean必须在同一个文件中;
local:可以引用bean实例的id和name,并且引用的bean可以在同一个文件中,也可以不在同一个文件。
6.2、set:merge 是否合并父/子bean、value-type 用于指定集合中指的类型。
6.3、map:merge 是否合并父/子bean、key-type:map对象中中key的类型、value-type:map对象中中value的类型。有一个包含标签<entry>
6.4、props:与map不同的是,他指定的值必须是字符串。
7.2、配置文件:
7.3、测试类:
//加载配置文件
ClassPathResource resource=new ClassPathResource("applicationContext.xml");
//创建bean工厂
BeanFactory factory=new XmlBeanFactory(resource);
//获得对应的实体类
System.out.println("------------------------------------------------");
CollectionBean collection=(CollectionBean) factory.getBean("collectionBean");
System.out.println("打印学生的信息");
for (int i = 0; i < collection.getStudent().size(); i++) {
System.out.println("学生:"+collection.getStudent().get(i));
}
System.out.println("------------------------------------------------");
System.out.println("打印学校的信息");
for (int i = 0; i < collection.getSchools().size(); i++) {
System.out.println("学校:"+collection.getSchools().toArray()[i]);
}
System.out.println("------------------------------------------------");
System.out.println("输出消息");
System.out.println(collection.getMessages().get("success"));
System.out.println(collection.getMessages().get("fail"));
System.out.println("------------------------------------------------");
System.out.println("输出配置信息");
System.out.println(collection.getConfigurations().getProperty("temp_1"));
System.out.println("------------------------------------------------");
7.4、结果显示:
1、字面值:
“字面值”一般是指可以用字符串表示的值,这些值可以通过<value>元素标签进行注入。在默认情况下,基本的数据类型以及封装类,String等类型都是可以采用字面值注入方式。
2、<bean>元素:
<bean>可以嵌套<meta> <constructor-arg> <property> <lookup-method> <replaced-method>针对<bean>进行属性说明:
id和name:都是用来指定bean的标示符,必须唯一;id必须唯一;name可以使用多个标示符。
class:全类名。
parent:指定父类的bean名称。
scope:用来设置bean的创建模式,其中包含singleton,prototype,request、session、global session。默认为singleton。
lazy-init:设置该bean是否迟延加载。除非数据量非常大,一般不用。
init-method:设置bean的初始化方法。
destory-method:设置bean销毁的方法。
3、<constructor-arg>元素:
<constructor-arg>元素可以嵌套的标签有:<bean>、<ref> <idref> <value> <null> <list> <set> <map> <props>等元素针对<constructor-arg>进行属性说明:
index:指定参数的序号。
type:指定参数的类型。
ref:用于定义对其他bean实体的引用。
value:包含的是一个属性值得字符串值。
4、<property>元素:
<property>可以嵌套的标签有:<bean>、<ref> <idref> <value> <null> <list> <set> <map> <props>等元素针对<property>进行属性说明:
name:指定javabean的属性名称。
ref:用于指定对其他bean的引用。
value:包含的是一个属性值得字符串值。
5、<ref>元素:
针对<ref>进行属性说明:bean:指定所要引用的bean的实例名称。
local:指定所要引用的bean的实例名称。
parent:指定所要引用的bean的实例名称,可以引用外部的factory,就如引用的bean不在同一个配置文件中。
区别:bean和local:
bean:必须引用实例id的属性,并且引用的bean必须在同一个文件中;
local:可以引用bean实例的id和name,并且引用的bean可以在同一个文件中,也可以不在同一个文件。
6、集合元素:
6.1、list:只有一个属性 value-type:用于指定集合中指的类型。6.2、set:merge 是否合并父/子bean、value-type 用于指定集合中指的类型。
6.3、map:merge 是否合并父/子bean、key-type:map对象中中key的类型、value-type:map对象中中value的类型。有一个包含标签<entry>
6.4、props:与map不同的是,他指定的值必须是字符串。
7、案例程序代码
7.1、实体类:package com.liu.spring; public class CollectionBean { private List student; private Set schools; private Map messages; private Properties configurations; public List getStudent() { return student; } public void setStudent(List student) { this.student = student; } public Set getSchools() { return schools; } public void setSchools(Set schools) { this.schools = schools; } public Map getMessages() { return messages; } public void setMessages(Map messages) { this.messages = messages; } public Properties getConfigurations() { return configurations; } public void setConfigurations(Properties configurations) { this.configurations = configurations; } }
7.2、配置文件:
<!-- collectionBean类的配置如下 --> <bean id="collectionBean" class="com.liu.spring.CollectionBean"> <property name="student"> <!-- 使用list配置 --> <list> <value>浩浩学</value> <value>甜甜涛</value> <value>佐佐笔</value> </list> </property> <property name="schools"> <set> <value>西安文理学院</value> <value>贵州大学</value> <value>北京交通大学</value> </set> </property> <property name="messages"> <map> <entry key="success"> <value>我是好学生</value> </entry> <entry key="fail"> <value>我是坏学生</value> </entry> </map> </property> <property name="configurations"> <props> <prop key="temp_1"> 我来自地球 </prop> </props> </property> </bean>
7.3、测试类:
//加载配置文件
ClassPathResource resource=new ClassPathResource("applicationContext.xml");
//创建bean工厂
BeanFactory factory=new XmlBeanFactory(resource);
//获得对应的实体类
System.out.println("------------------------------------------------");
CollectionBean collection=(CollectionBean) factory.getBean("collectionBean");
System.out.println("打印学生的信息");
for (int i = 0; i < collection.getStudent().size(); i++) {
System.out.println("学生:"+collection.getStudent().get(i));
}
System.out.println("------------------------------------------------");
System.out.println("打印学校的信息");
for (int i = 0; i < collection.getSchools().size(); i++) {
System.out.println("学校:"+collection.getSchools().toArray()[i]);
}
System.out.println("------------------------------------------------");
System.out.println("输出消息");
System.out.println(collection.getMessages().get("success"));
System.out.println(collection.getMessages().get("fail"));
System.out.println("------------------------------------------------");
System.out.println("输出配置信息");
System.out.println(collection.getConfigurations().getProperty("temp_1"));
System.out.println("------------------------------------------------");
7.4、结果显示:
相关文章推荐
- java学习笔记之Spring依赖注入和控制反转
- Spring 4 学习笔记2:控制反转(IoC)和依赖注入(DI)
- spring学习笔记03--依赖注入与控制反转
- Spring笔记(6)-----------注入参数详解
- spring 控制反转与依赖注入原理-学习笔记
- spring 控制反转与依赖注入原理-学习笔记
- Spring学习(1):控制反转(IoC)和依赖注入(DI)的详解以及注解(annotation)开发入门案例
- Spring2.5学习笔记1-控制反转-依赖注入类型
- Spring依赖注入和控制反转详解
- Spring的控制反转(IOC)和依赖注入(DI)详解
- Spring笔记之二 -- SpringIOC(控制反转)
- Spring高级程序设计 3 spring 控制反转(笔记)
- Spring依赖注入与控制反转
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
- Spring 依赖注入控制反转实现,及编码解析(自制容器)
- Spring入门Blog[二、Ioc控制反转集合注入和Scope]
- spring中的控制反转和依赖注入
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
- 【Spring】Spring依赖注入与控制反转理解
- Spring的控制反转与依赖注入