Spring学习笔记05——Bean中属性注入:基本数据类型、对象类型(setter)、集合类型(setter)
程序员文章站
2022-03-25 12:13:27
...
继续沿用工程代码,结束。
1. 基本数据类型注入
<bean id="user" class="org.example.bean.User">
<property name="userId" value="1"/>
<property name="userName" value="张三"/>
<property name="address" value="北京"/>
</bean>
2. 对象类型(setter)
<bean id="userDao" class="org.example.dao.impl.MysqlUserDaoImpl"></bean>
<bean id="userService" class="org.example.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
3. 集合类型(setter)
集合属性的注入:
·········list:添加list节点,然后如果集合中的数据是引用数据类型需要使用ref节点,如果是基本数据类型用value
·········set:添加set节点,然后如果集合中的数据是引用数据类型需要使用ref节点,如果是基本数据类型用value
·········map:添加map节点,由于map中存储的是key和value键值对,需要添加一个entry节点
··············对应key,如果数据类型引用数据类型需要使用ref节点,如果是基本数据类型用value
··············对应value,如果数据类型引用数据类型需要使用ref节点,如果是基本数据类型用value
·········properties:添加props节点,然后再添加prop节点
-
在
bean
包中添加Demo.java
类public class Demo { private List<String> list; private Set<String> set; private Map<String,String> 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, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
-
在
applicationContext.xml
添加如下代码<bean id="demo" class="org.example.bean.Demo"> <property name="list"> <list> <value>list1</value> <value>list2</value> </list> </property> <property name="set"> <set> <value>set1</value> <value>set2</value> </set> </property> <property name="map"> <map> <entry> <key><value>mapkey1</value></key> <value>mapvalue1</value> </entry> <entry> <key><value>mapkey2</value></key> <value>mapvalue2</value> </entry> </map> </property> <property name="properties"> <props> <prop key="prop1">propVal1</prop> <prop key="prop2">propVal2</prop> </props> </property> </bean>
-
测试方法,主要是熟悉一下各种集合类型的遍历过程
@Test public void testDemo(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Demo demo = (Demo)context.getBean("demo"); logger.info("list-----------------------------"); // List集合类型遍历 List<String> list = demo.getList(); for (String s : list) { logger.info(s); } logger.info("set-----------------------------"); // Set集合类型遍历 Set<String> set = demo.getSet(); for (String s : set) { logger.info(s); } logger.info("map-----------------------------"); // Map集合类型遍历 Map<String, String> map = demo.getMap(); // 获取map集合中键 Set<String> keySet = map.keySet(); // 迭代器 Iterator<String> iterator = keySet.iterator(); while(iterator.hasNext()){ String key = iterator.next(); String value = map.get(key); logger.info(key + " " + value); } logger.info("properties-----------------------------"); Properties properties = demo.getProperties(); String userId = properties.getProperty("userId"); String username = properties.getProperty("username"); String password = properties.getProperty("password"); logger.info(userId); logger.info(username); logger.info(password); }
上一篇: 学习人脸识别的例子-测试18/04/06