zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring框架学习之第7节

2023-09-14 09:07:42 时间

配置Bean的细节

☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大

 ②如何给集合类型注入值

Java中主要的map,set,list / 数组

Collection col = new ArrayList();

col能够使用的方法(点出来的方法)取决于左边的数据类型

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:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="department" class="com.litao.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="emp1"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
	</list>
</property>
<!-- 给set注入值 set中不能有相同的对象 -->
<property name="empsets">
	<set>
		<ref bean="emp1"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
	</set>
</property>
<!-- 给map注入值 map中也不能有相同的对象,后面的会把前面的覆盖,map只要key一样就可以装配value对应的bean -->
<property name="empMaps">
	<map>
		<entry key="1" value-ref="emp1"></entry>
		<entry key="2" value-ref="emp2"></entry>
		<entry key="2" value-ref="emp2"></entry>
	</map>
</property>
</bean>
<bean id="emp1" class="com.litao.collection.Employee">
<property name="name" value="北京" />
<property name="id" value="1" />
</bean>
<bean id="emp2" class="com.litao.collection.Employee">
<property name="name" value="天津" />
<property name="id" value="2" />
</bean>
</beans>

Employee.java

package com.litao.collection;

public class Employee {
	
	private String name;
	private int id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

Department.java

package com.litao.collection;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Department {

	private String name;
	private String[] empName;
	private List<Employee> empList;
	private Set<Employee> empsets;
	private Map<String,Employee> empMaps;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String[] getEmpName() {
		return empName;
	}
	public void setEmpName(String[] empName) {
		this.empName = empName;
	}
	
	public List<Employee> getEmpList() {
		return empList;
	}
	public void setEmpList(List<Employee> empList) {
		this.empList = empList;
	}
	public Set<Employee> getEmpsets() {
		return empsets;
	}
	public void setEmpsets(Set<Employee> empsets) {
		this.empsets = empsets;
	}
	public Map<String, Employee> getEmpMaps() {
		return empMaps;
	}
	public void setEmpMaps(Map<String, Employee> empMaps) {
		this.empMaps = empMaps;
	}
	
	
}

App1.java

package com.litao.collection;

import java.util.Iterator;
import java.util.Map;
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/litao/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());
			
		}
		
		//set取得时候不能保证顺序,list取时可以保证顺序
		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();
			//System.out.println(key);
			Employee emp = empmaps.get(key);
			System.out.println("key="+key+" "+emp.getName());
		}
		
		//2.最简洁的方法
		for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){
			System.out.println(entry1.getKey() + " " + entry1.getValue().getName());
			
		}
		
	}

}