spring配置bean的细节一
来源:互联网 发布:淘宝访问量怎么提高 编辑:程序博客网 时间:2024/06/06 02:18
一 scope的说明
尽量使用 scope=”singleton” ,不要使用prototype,因为这样对我们的性能影响较大。
二 如何给集合类型注入值
java中主要的集合有几种:map、set、list、数组
三 代码
1 Department
package com.hsp.collection;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class Department { private String name; private String [] empName;//数组 private List<Employee> empList;//list集合 private Set<Employee> empsets;//set集合 private Map<String,Employee> empMaps;//map集合 private Properties pp;//Properties的使用 public Set<Employee> getEmpsets() { return empsets; } public void setEmpsets(Set<Employee> empsets) { this.empsets = empsets; } public String[] getEmpName() { return empName; } public void setEmpName(String[] empName) { this.empName = empName; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Employee> getEmpList() { return empList; } public void setEmpList(List<Employee> empList) { this.empList = empList; } public Map<String, Employee> getEmpMaps() { return empMaps; } public void setEmpMaps(Map<String, Employee> empMaps) { this.empMaps = empMaps; } public Properties getPp() { return pp; } public void setPp(Properties pp) { this.pp = pp; }}
2 Employee
package com.hsp.collection;public class Employee { private String name; private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
3 App1
package com.hsp.collection;import java.util.Enumeration;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Map.Entry;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/collection/beans.xml"); Department department=(Department) ac.getBean("department"); System.out.println(department.getName()); for(String emName:department.getEmpName()){ System.out.println(emName); } System.out.println("**********通过list集合取出数据*****"); for(Employee e : department.getEmpList()){ System.out.println("name="+e.getName()+" "+e.getId()); } System.out.println("**********通过set集合取出数据*****"); for(Employee e : department.getEmpsets()){ System.out.println("name="+e.getName()); } System.out.println("*******通过map集合取出数据 迭代器****"); //1.迭代器 Map<String,Employee> empmaps=department.getEmpMaps(); Iterator it=empmaps.keySet().iterator(); while(it.hasNext()){ String key=(String) it.next(); Employee emp=empmaps.get(key); System.out.println("key="+key+" "+emp.getName()); } System.out.println("*******通过map集合取出数据 简洁方法****"); //2.简洁方法 for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){ System.out.println(entry1.getKey()+" "+entry1.getValue().getName()); } System.out.println("*****通过Propertis取出数据*****"); Properties pp=department.getPp(); //System.out.println(pp.get("pp1").toString()); for(Entry<Object,Object> entry:pp.entrySet()){ System.out.println(entry.getKey().toString()+" "+entry.getValue().toString()); } System.out.println("*****通过Enumeration取出*****"); Enumeration en= pp.keys(); while(en.hasMoreElements()){ // Entry<Object,Object> elment= (Entry<Object, Object>) en.nextElement(); // System.out.println(elment.getKey()+" "+elment.getValue()); String key=(String) en.nextElement(); System.out.println(key+" "+pp.getProperty(key)); } }}
4 beans.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:context="http://www.springframework.org/schema/context"; xmlns:tx="http://www.springframework.org/schema/tx"; xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";><bean id="department" class="com.hsp.collection.Department"><property name="name" value="财务部"/><!-- 给数组注入值 --><property name="empName"> <list> <value>小明</value> <value>小明小明</value> <value>小明小明小明小明</value> </list></property><!-- 给list注入值 list 中可以有相当的对象 --><property name="empList"> <list> <ref bean="emp2" /> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> <ref bean="emp1"/> </list></property><!-- 给set注入值 set不能有相同的对象 --><property name="empsets"> <set> <ref bean="emp1" /> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> <ref bean="emp2"/> </set></property><!-- 给map注入值 map只有key不一样,就可以装配value --><property name="empMaps"> <map> <entry key="11" value-ref="emp1" /> <entry key="22" value-ref="emp2"/> <entry key="22" value-ref="emp1"/> </map></property><!-- 给属性集合配置 --><property name="pp"> <props> <prop key="pp1">abcd</prop> <prop key="pp2">hello</prop> </props></property></bean><bean id="emp1" class="com.hsp.collection.Employee"><property name="name" value="北京"/><property name="id" value="1"/></bean><bean id="emp2" class="com.hsp.collection.Employee"><property name="name" value="天津"/><property name="id" value="2"/></bean></beans>
四 测试结果
财务部
小明
小明小明
小明小明小明小明
**********通过list集合取出数据*****
name=天津 2
name=北京 1
name=北京 1
name=北京 1
name=北京 1
name=北京 1
name=北京 1
**********通过set集合取出数据*****
name=北京
name=天津
*******通过map集合取出数据 迭代器****
key=11 北京
key=22 北京
*******通过map集合取出数据 简洁方法****
11 北京
22 北京
*****通过Propertis取出数据*****
pp2 hello
pp1 abcd
*****通过Enumeration取出*****
pp2 hello
pp1 abcd
阅读全文
0 0
- spring配置bean的细节一
- Spring Bean配置细节
- Spring-3:bean的属性配置细节
- spring之bean的配置细节
- spring配置bean的细节二
- spring Bean属性配置细节
- Spring中使用xml配置bean的细节
- spring之bean的配置细节(二)
- J2EE之Spring中Bean的配置细节
- Spring框架的Bean细节
- spring bean的配置(一)
- Spring Bean配置细节author: 惠惠
- spring的配置一(bean的配置)
- Spring知识一(bean配置)
- spring bean的配置
- Spring bean的配置
- spring-bean的配置
- spring 的 bean 配置
- 计算机视觉_Opencv
- 目前主流的四大浏览器内核Trident、Gecko、WebKit以及Presto
- 从今天开始自学PYTHON啦!
- Play with lkvm
- stack的使用
- spring配置bean的细节一
- 怎么高效学习
- [DP-LIS] POJ 2533
- 给定一棵二叉树,找到它的最小深度。最小深度是从根节点到最近叶节点的最短路径上的节点数量
- nexus安装
- 数据结构->单链表的操作
- CSS3 width:fit-content属性
- HTML笔记3
- 从一道题看为什么你的工作年限不值钱