Spring中常用配置

Spring中配置集合属性

编写两个java文件

package com.cloud.peizhi;
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;
	}
}

package com.cloud.peizhi;
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;
	private Set<Employee> empSet;
	private Map<String, Employee> empMap;
	private Properties pp;
	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> getEmpSet() {
		return empSet;
	}
	public void setEmpSet(Set<Employee> empSet) {
		this.empSet = empSet;
	}
	public Map<String, Employee> getEmpMap() {
		return empMap;
	}
	public void setEmpMap(Map<String, Employee> empMap) {
		this.empMap = empMap;
	}
	public Properties getPp() {
		return pp;
	}
	public void setPp(Properties pp) {
		this.pp = pp;
	}
}

编写配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<bean id="department" class="com.cloud.peizhi.Department">
		<property name="name" value="财务部"></property>
		<!-- 给数组注入值 -->
		<property name="empName">
			<list>
				<value>empName1</value>
				<value>empName2</value>
				<value>empName3</value>
			</list>
		</property>
		<!-- 给List注入值,可以存放相同的对象 -->
		<property name="empList">
			<list>
				<ref bean="emp1"/>
				<ref bean="emp2"/>
				<ref bean="emp1"/>
			</list>
		</property>
		<!-- 配置Set属性,相同对象会被覆盖 -->
		<property name="empSet">
			<set>
				<ref bean="emp1"/>
				<ref bean="emp2"/>
				<ref bean="emp1"/>
			</set>
		</property>
		<!-- 配置Map属性,key值相同,后面的覆盖前面的属性 -->
		<property name="empMap">
			<map>
				<entry key="1" value-ref="emp1"></entry>
				<entry key="2" value-ref="emp2"></entry>
				<entry key="2" value-ref="emp1"></entry>
			</map>
		</property>
		<!-- 配置属性集合 -->
		<property name="pp">
			<props>
				<prop key="pp1">HELLO</prop>
				<prop key="pp2">WORLD</prop>
			</props>
		</property>
	</bean>
	<!-- 配置Employee公共属性 -->
	<bean id="emp1" class="com.cloud.peizhi.Employee">
		<property name="name"><value>Spring</value></property>
		<property name="id"><value>1</value></property>
	</bean>
	<bean id="emp2" class="com.cloud.peizhi.Employee">
		<property name="name"><value>Summer</value></property>
		<property name="id"><value>2</value></property>
	</bean>
</beans>

编写测试文件

package com.cloud.peizhi;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
	/**
	 * 测试Spring中配置的常用属性
	 */
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Department department = (Department) ac.getBean("department");
		System.out.println(department.getName());
		System.out.println("--------------------->String数组");
		for(String empName:department.getEmpName()){
			System.out.println(empName);
		}
		System.out.println("--------------------->List集合");
		for(Employee e:department.getEmpList()){
			System.out.println(e.getName()+";"+e.getId());
		}
		System.out.println("--------------------->Set集合");
		for(Employee e:department.getEmpSet()){
			System.out.println(e.getName()+";"+e.getId());
		}
		System.out.println("--------------------->Map集合");
		for(Entry<String,Employee> entry:department.getEmpMap().entrySet()){
			System.out.println(entry.getKey()+";"+entry.getValue().getName());
		}
		System.out.println("--------------------->Properties");
		Properties pp = department.getPp();
		System.out.println(pp.get("pp1"));
		System.out.println(pp.get("pp2"));
	}
}

配置构造函数属性

编写java类

package com.cloud.peizhi;
public class Employee {
	private String name;
	private int id;
	public Employee(){
		System.out.println("无参构造函数!");
	}
	public Employee(String name){
		this.name = name;
		System.out.println("带参数构造函数!");
	}
	public Employee(String name,int id){  
		System.out.println("两个参数的构造函数");  
		this.name=name;  
		this.id=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;
	}
}

配置文件

<!-- 这里配置2个参数,所有调用2个参数的构造函数 -->
<bean id="employee" class="com.cloud.peizhi.Employee">
	<constructor-arg index="0" type="java.lang.String" value="Spring"></constructor-arg>
	<constructor-arg index="1" type="int" value="23"></constructor-arg>
</bean>

测试文件

package com.cloud.peizhi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
	/**
	 * 测试Spring中配置的常用属性
	 */
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Employee employee = (Employee) ac.getBean("employee");
		System.out.println(employee.getName());
	}
}

配置继承属性

编写java文件

package com.cloud.peizhi;
public class Student {
	private String name;
	private 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.cloud.peizhi;
public class Gradate extends Student{
	private String degree;
	public String getDegree() {
		return degree;
	}
	public void setDegree(String degree) {
		this.degree = degree;
	}
}

编写配置文件

<!-- 配置一个学生 -->
<bean id="student" class="com.cloud.peizhi.Student">
	<property name="name" value="Spring"></property>
	<property name="age" value="22"></property>
</bean>
<!-- 配置Gradate -->
<bean id="gradate" parent="student" class="com.cloud.peizhi.Gradate">
	<property name="name" value="summer"></property>
	<property name="degree" value="研究生"></property>
</bean>

编写测试类

package com.cloud.peizhi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
	/**
	 * 测试Spring中配置的常用属性
	 */
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Gradate gradate = (Gradate) ac.getBean("gradate");
		System.out.println(gradate.getName()+";"+gradate.getDegree());
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值