(三)Spring XML 配置 之Bean 属性
2016-02-26 15:05
495 查看
Bean 的属性注入方式,分为属性注入 和 构造器注入。 属性注入包含如下几种
1. java 基本类型
2. List
3. Set
4. Map
5. Property
6. Java Bean
【1. 构造器注入】
【构造器注入一】
ComputerBean 必须有以下构造方法
【构造器注入二】
【构造器三】
【2.1 属性注入-- 基本类型注入】
【2.2 属性注入-- 数组注入】
【2.3 属性注入--List类型注入】
【2.4 属性注入-- Set类型注入】
【2.5 属性注入--Map 类型注入】
entry 还有属性 key-ref, value-ref , 对应于key 和 value 为非java 基本类型的情况。
【2.6 属性注入--Property注入】
【2.7 属性注入 -- JavaBean属性 注入】
【2.7.1 引入已定义的Bean】
【2.7.2 内部Bean】
【2.7.3 自动装配】
【2.8 属性注入 -- 外部属性文件】
属性文件:computer.properties
【2.9 属性注入--使用P命名空间】
1. java 基本类型
2. List
3. Set
4. Map
5. Property
6. Java Bean
【1. 构造器注入】
【构造器注入一】
<!-- 1.1 构造器注入: constructor-arg 没有name属性, spring 会自动进行类型转换--> <bean id="computerBean" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <constructor-arg value="lenovo-Y470" /> <constructor-arg value="5000.0" /> </bean>
ComputerBean 必须有以下构造方法
public ComputerBean(String name, double price) { this.name = name; this.price = price; }
【构造器注入二】
<!-- 1.2构造器注入: constructor-arg 可以通过index 属性指定参数位置, 从0 开始 --> <bean id="computerBean2" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <constructor-arg index="0" value="Lenovo-Y470"/> <constructor-arg index="1" value="5000.0"/> </bean>ComputerBean 必须有以下构造方法
public ComputerBean(String name, double price) { this.name = name; this.price = price; }
【构造器三】
<bean id="computerBean" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <constructor-arg value="lenovo-Y470" /> <constructor-arg value="5000.0" /> </bean> <!-- 1.3 构造器注入: 注入其他Bean --> <bean id="computerBean3" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <constructor-arg ref="computerBean"/> </bean>ComputerBean 必须有以下构造方法
public ComputerBean(String name, double price) { this.name = name; this.price = price; }
【2.1 属性注入-- 基本类型注入】
<!-- 2.1属性注入:name 为属性名称. 注意:此种情况必须有无参构造函数, 和属性相应的set 方法 --> <bean id="computerBean4" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="name" value="lenovo-Y470"/> <property name="price" value="5000.0"/> </bean>ComputerBean 必须有以下set方法和无参构造函数,因为spring 是根据反射来注入属性值的
public void setName(String name) { this.name = name; } public void setPrice(double price) { this.price = price; }
【2.2 属性注入-- 数组注入】
<!-- 2.2属性注入:name 为属性名称. 注意:此种情况必须有无参构造函数, 和属性相应的set 方法 --> <bean id="computerBean22" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="listKeys"> <array> <value>F1</value> <value>F2</value> <value>F3</value> </array> </property> </bean>ComputerBean 必须有以下set方法和无参构造函数
public void setArrayKeys(String[] arrayKeys) { this.arrayKeys = arrayKeys; }
【2.3 属性注入--List类型注入】
<!-- 2.1属性注入:List --> <bean id="computerBean5" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="listKeys"> <list> <value>F1</value> <value>F2</value> <value>F3</value> <value>F..</value> </list> </property> </bean>ComputerBean 必须有以下set方法和无参构造函数
public void setListKeys(List<String> listKeys) { this.listKeys = listKeys; }
【2.4 属性注入-- Set类型注入】
<!-- 2.2属性注入:Set 需要有需要有无参构造方法--> <bean id="computerBean6" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="setKeys"> <set> <value>F1</value> <value>F2</value> <value>F3</value> <value>F..</value> </set> </property> </bean>ComputerBean 必须有以下set方法和无参构造函数
public void setSetKeys(Set<String> setKeys) { this.setKeys = setKeys; }
【2.5 属性注入--Map 类型注入】
entry 还有属性 key-ref, value-ref , 对应于key 和 value 为非java 基本类型的情况。
<!-- 2.3属性注入:Map 需要有需要有无参构造方法--> <bean id="computerBean7" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="mapKey"> <map> <entry key="A" value="aaa"/> <entry key="B" value="bbb"/> <entry key="C" value="ccc"/> </map> </property> </bean>ComputerBean 必须有以下set方法和无参构造函数
public void setMapKey(Map<String, String> mapKey) { this.mapKey = mapKey; }
【2.6 属性注入--Property注入】
<!-- 2.5属性注入:Properties 需要有需要有无参构造方法--> <bean id="computerBean8" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="properties"> <props> <prop key="A">aaaa</prop> <prop key="B">bbbb</prop> <prop key="C">cccc</prop> </props> </property> </bean>ComputerBean 必须有以下set方法和无参构造函数
public void setProperties(Properties properties) { this.properties = properties; }
【2.7 属性注入 -- JavaBean属性 注入】
【2.7.1 引入已定义的Bean】
<!-- ref 引用已定义Bean --> <bean id="computerBean27" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="monitor" ref="monitorBean"/> </bean>
【2.7.2 内部Bean】
<!-- 2.8 内部Bean , 内部bean 无id ,所以不能被获取到或者被其它bean 引用--> <bean id="computerBean28" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="monitor" > <bean class="org.zgf.spring.ioc.beanpro.MonitorBean"> <property name="name" value="Lenovo"/> <property name="price" value="1000"/> </bean> </property> </bean>
【2.7.3 自动装配】
<!-- 2.9 自动注入: autowire: byName 根据变量名称注入 , byType:根据变量类型注入 缺点:如果有多个JavaBean属性, 那么只能指定一种方式,不能为不同的属性指定不同的注入方式 --> <bean id="computerBean29" class="org.zgf.spring.ioc.beanpro.ComputerBean" autowire="byType"/>
【2.8 属性注入 -- 外部属性文件】
<!-- 3. Spring 提供了一个PropertyPlaceholderConfigurer的BeanFactory 后置处理器, 这个后置处理器允许用户将Bean 配置的内容外移到属性文件中 location: 指定外部配置文件位置, 使用方式 ${var} --> <context:property-placeholder location="classpath:org/zgf/spring/ioc/beanpro/computer.properties"/> <bean id="computerBean3" class="org.zgf.spring.ioc.beanpro.ComputerBean"> <property name="name" value="${computer.name}"/> <property name="price" value="${computer.price}"/> </bean>
属性文件:computer.properties
computer.properties#computer.properties
computer.name=lenovo-Y470 computer.price=1000
【2.9 属性注入--使用P命名空间】
<!-- 4. P命名空间的使用 :Spring 提供了P命名空间,目的是简化Bean 的属性配置 , 注意需要引入P的命名空间--> <bean id="computerBean4" class="org.zgf.spring.ioc.beanpro.ComputerBean" p:name="lenovo-Y470" p:price="1000.0"/>
相关文章推荐
- 基于dom4j,XML和java bean互转实例
- (三)Spring AOP
- (二)Spring XML 配置 之Bean 配置
- java.lang.NoClassDefFoundError: org/hibernate/Session
- Spring的基本用法
- spring中的context:include-filter和context:exclude-filter的区别
- (一)Spring 简介
- 关于Dragger2与RXjava集合框架的项目随笔
- spring整合quartz任务调度框架使用
- Java中的Timer源码分析及缺陷
- window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法[转]
- MAC地址,使用java获取IP地址和MAC地址。
- java设计模式(三)--抽象工厂模式
- 如何使用intelij IDEA 显示一个类的所有方法?与eclipse的outline视图类似
- JAVA线程池
- Java操作Ant压缩和解压文件及批量打包Anroid应用
- Spring-05-整合Struts2
- java enum(枚举)使用详解 + 总结
- Spring中的@Scheduled
- java Web单点登录(SSO)原理及简单实现