一、Spring IOC参数值注入
1.注入基本值
1.1)<value></value>元素可以通过字符串指定属性或构造参数的值。容器将字符串从java.lang.String类型转换为实际的属性或者参数类型后给bean对象注入。
<bean id="dataSource" class="com.spring.dao.JDBCDataSource">
<property name="username" value="root">
<value>root</value>
</property> <property name="password">
<value>123456</value>
</property> </bean>
1.2)也可以通过value属性指定基本值
<property name="username" value="root"></property> <property name="password" value=""></property>
1.3)新建一个类MessageBean
package com.spring.dao; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class MessageBean { private String name; private String password; private String phone; private String gender; private List<String> list; private Set<String> set; private Map<String,Object>map; private Properties properties; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public void excute(){ System.out.println("name="+name); System.out.println("password="+password); System.out.println("phone="+phone); System.out.println("gender="+gender); } }
1.4)在配置文件添加
<!-- 参数值注入 --> <bean id="messageBean" class="com.spring.dao.MessageBean"> <property name="name" value="zhangsan"></property> <property name="password" value="123456"></property> <property name="phone" value="110"></property> <property name="gender" value="男"></property> </bean>
1.5)在测试方法添加testMessageBean()方法
@Test public void testMessageBean(){ String con ="applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(con); MessageBean mb = ac.getBean("messageBean",MessageBean.class); mb.excute(); }
1.6)控制台输出
name=zhansgan
password=123456
phone=110
gender=男
说明值注入成功
2.注入集合
通过<list></list>,<set></set>,<map></map>,<properties></properties>元素可以定义和设置与java类型中
对应的list,set,map,properties属性值
2.1)在MessageBean中添加代码
private List<String> list; private Set<String> set; private Map<String,Object>map; private Properties properties; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } System.out.println("----------------list信息-----------------"); /* * list集合 */ for(String s:list){ System.out.println(s); } System.out.println("-----------------set信息------------------"); /* * set集合 */ for(String s:set){ System.out.println(s); } System.out.println("-------------------map信息------------------"); /* * 循环key取出map value值 */ Set<String> key = map.keySet(); for(String s:key){ System.out.println(s+"="+map.get(s)); } System.out.println("--------------------properties信息--------------"); /* * properties */ Set<Object> set = properties.keySet(); for(Object o:set){ System.out.println(o+"="+properties.getProperty(o.toString())); }
2.2)在配置文件中添加
<!-- 参数值注入 --> <bean id="messageBean" class="com.spring.dao.MessageBean"> <property name="name" value="lizhang"></property> <property name="password" value="123456"></property> <property name="phone" value="15256597951"></property> <property name="gender" value="男"></property> <property name="list"> <list> <value>zhangsan</value> <value>lisi</value> <value>wangwu</value> </list> </property> <property name="set"> <set> <value>java</value> <value>php</value> <value>c</value> </set> </property> <property name="map"> <map> <entry key="100" value="张三"></entry> <entry key="101" value="李四"></entry> <entry key="102" value="王五"></entry> </map> </property> <property name="properties"> <props> <prop key="name">admin</prop> <prop key="password">123456</prop> </props> </property> </bean>
2.3)在测试方法执行testMesageBean方法,控制台输出
name=zhansgan
password=123456
phone=110
gender=男
----------------list信息-----------------
zhangsan
lisi
wangwu
-----------------set信息------------------
java
php
c
-------------------map信息------------------
100=张三
101=李四
102=王五
--------------------properties信息--------------
name=admin
password=123456
说明注入集合成功
3.引用方式集合注入
3.1在配置文件添加下面配置
引用方式集合注入配置格式:
<util:list id=""><value></value></util:list>,
<util:set id=""><value></value></util:set>
<util:map id=""><value></value><util:map>
<util:properties id=""><value></value></util:properties>
<!-- 定义集合bean --> <util:list id="list"> <value>华为</value> <value>苹果</value> <value>三星</value> </util:list> <util:set id="set"> <value>华硕</value> <value>联想</value> <value>微星</value> </util:set> <util:map id="map"> <entry key="1" value="中国"></entry> <entry key="2" value="美国"></entry> <entry key="3" value="德国"></entry> </util:map> <util:properties id="properties"> <prop key="name">root</prop> <prop key="password">123456</prop> </util:properties> <!-- 引用方式注入集合属性 --> <bean id="message2" class="com.spring.dao.MessageBean"> <property name="list" ref="list"></property> <property name="set" ref="set"></property> <property name="map" ref="map"></property> <property name="properties" ref="properties"></property> </bean>
3.2)在测试方法中添加testMessageBean2()方法
@Test public void testMessageBean2(){ String con = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(con); MessageBean mb = ac.getBean("message2",MessageBean.class); mb.excute(); }
3.3)控制台输出一下信息,引用方式集合注入成功
----------------list信息-----------------
华为
苹果
三星
-----------------set信息------------------
华硕
联想
微星
-------------------map信息------------------
1=中国
2=美国
3=德国
--------------------properties信息--------------
name=root
password=123456
4.利用Spring表达式注入属性值
Spring引入了一种表达式语言,语法上和EL表达式很相似,这种表达式语言可以用于定义基于xml和注解配置的bean,注入一个properties文件信息
4.1) src下新建一个db.properties文件,在文件里写入
name=root123
password=123456
4.2)修改配置文件为以下(name="name" ,"name"要和messageBean里属性名一致 private String name;)
<util:properties id="properties" location="classpath:db.properties"> </util:properties> <!-- 引用方式注入集合属性 --> <bean id="message2" class="com.spring.dao.MessageBean"> <property name="name" value="#{properties.name}"></property> <property name="password" value="#{properties.password}"></property> </bean>
4.3)在测试类中执行testMessageBean2方法,控制台输出
说明利用表达式注入属性值成功