Springでよく使われる構成

9300 ワード

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