spring教程笔记3

内部bean

<bean id=”foo” class=”....Foo”>
	<property name=”属性”>
	<!—第一方法引用-->
	<ref bean=’bean对象名’/>
	<!—内部bean-->
	<bean> 
	<properyt></property>
</bean>
</property>
</bean>

给集合类型注入值

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;
	}
}
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;
	}

}
<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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.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>
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));
		}
	}

}

运行结果
2020-3-1 22:31:43 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Sun Mar 01 22:31:43 CST 2020]; root of context hierarchy 2020-3-1 22:31:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [com/hsp/collection/beans.xml] 2020-3-1 22:31:43 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1833eca 2020-3-1 22:31:43 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1833eca: defining beans [department,emp1,emp2]; root of factory hierarchy

财务部
小明
小明小明
小明小明小明小明
**********通过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

④ 继承配置

package com.hsp.inherit;

public class Student {
	
	protected String name;
	protected int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}


package com.hsp.inherit;

public class Gradate extends Student {

	private String degree;

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}
	
}
<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置一个学生对象 -->
<bean id="student" class="com.hsp.inherit.Student">
	<property name="name" value="顺平" />
	<property name="age" value="30"/>
</bean>
<!-- 配置Grdate对象 -->
<bean id="grdate" parent="student" class="com.hsp.inherit.Gradate">
	<!-- 如果自己配置属性name,age,则会替换从父对象继承的数据  -->
	<property name="name" value="小明"/>
	<property name="degree" value="学士"/>
</bean>

</beans>

package com.hsp.inherit;

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/inherit/beans.xml");
	
		Gradate gradate=(Gradate) ac.getBean("grdate");
		System.out.println(gradate.getName()+" "+gradate.getAge()+" "+gradate.getDegree());
	}

}

运行结果

2020-3-1 22:37:56 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Sun Mar 01 22:37:56 CST 2020]; root of context hierarchy
2020-3-1 22:37:56 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/hsp/inherit/beans.xml]
2020-3-1 22:37:56 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7
2020-3-1 22:37:56 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7: defining beans [student,grdate]; root of factory hierarchy
小明 30 学士

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值