1. 程式人生 > >spring裝配bean及引數注入

spring裝配bean及引數注入

個人使用建議:不要使用3.2.4版本  因為在進行xml檔案載入的時候會出錯。

首先搭建環境

Student.java

package com.demo.entity;

public class Student {
	
	private String name;
	private char gender;
	private int age;
	private School school;

	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Student(String name, char gender, int age, School school) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.school = school;
	}

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	public String getName() {
		return name;
	}

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

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", gender=" + gender + ", age=" + age + ", "+this.school.toString() + "]";
	}	
}

School.java

package com.demo.entity;

public class School {
	private String name;
	private String address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "School [name=" + name + ", address=" + address + "]";
	}
	
}

Junit測試

(注意:FileSystemXmlApplicationContext("applicationContext.xml")找配置檔案是從專案根目錄下找的,也可以是絕對路徑)

package com.demo.entity;

import static org.junit.Assert.*;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class MyTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}


	@Test
	public void test_ClassPath() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
			
		Student student = (Student)ac.getBean("student");
		System.out.println(student.toString());
	
	}
	@Test
	public void test_FileSystem() {
		
		ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
			
		Student student = (Student)ac.getBean("student");
		
		System.out.println(student.toString());
	
	}
	@Test
	public void test_BeanFactory() {
		
		BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
		
		Student student = (Student)bf.getBean("student");
		
		System.out.println(student.toString());
	
	}
}

結果

注入分 設值注入、構造注入、p名稱空間注入、c名稱空間注入

(1)設值注入

注意:資料型別為School的注入是開闢了myschool容器的,而注入的時候是引用(ref)不是(value)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- bean definitions here -->
	
	<!-- 相當於 new Student() -->
	<bean id="student" class="com.demo.entity.Student" >
		<!-- 相當於setName("enrico") -->
		<property name="name" value="enrico"></property>
		<property name="gender" value="男"></property>
		<property name="age" value="20"></property>
		<property name="school" ref="myschool"></property>
		
	</bean>
	<bean id="myschool" class="com.demo.entity.School">
		<property name="name" value="武漢大學"></property>
		<property name="address" value="湖北"></property>	
	</bean>
</beans>

(2)構造注入

注意:構造注入需要有參構造方法的支援,使用構造注入又包含(型別,索引,型別+索引)

【型別注入】

<!-- 型別 -->
	<bean id="student" class="com.demo.entity.Student">
		<constructor-arg type="String"  value="enrico"></constructor-arg>
		<constructor-arg type="char" value='男'></constructor-arg>
		<constructor-arg type="int"  value="20"></constructor-arg>
		<constructor-arg type="School" index="3" ref="myschool"></constructor-arg>
	</bean>	

【索引注入】

<!-- 索引 -->
	 <bean id="student" class="com.demo.entity.Student">
		<constructor-arg  index="0" value="enrico"></constructor-arg>
		<constructor-arg  index="1" value='男'></constructor-arg>
		<constructor-arg  index="2" value="20"></constructor-arg>
		<constructor-arg type="School" index="3" ref="myschool"></constructor-arg>
	</bean> 

【型別+索引注入】

<!-- 型別+索引 -->
	<bean id="student" class="com.demo.entity.Student">
		<constructor-arg type="String" index="0" value="enrico"></constructor-arg>
		<constructor-arg type="char" index="1" value='男'></constructor-arg>
		<constructor-arg type="int" index="2" value="20"></constructor-arg>
		<constructor-arg type="School" index="3" ref="myschool"></constructor-arg>
	</bean>

(3)p名稱空間注入

注意:p名稱空間注入是通過set方法注入資料,又叫p名稱空間設值注入。

配置檔案中引入:xmlns:p="http://www.springframework.org/schema/p"

<?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:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- bean definitions here -->

	<bean id="student" class="com.demo.entity.Student" 
		p:name="enrico" p:gender="男" p:age="20" p:school-ref="myschool">
	</bean>

</beans>

(4)c名稱空間注入

注意:c名稱空間注入是通過有參構造方法注入,又叫c名稱空間構造注入。

配置檔案中引入:xmlns:c="http://www.springframework.org/schema/c"

<?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:c="http://www.springframework.org/schema/c"

       xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- bean definitions here -->

	<bean id="student" class="com.demo.entity.Student" 
		c:name="enrico" c:gender="男" c:age="20" c:school-ref="myschool">
	</bean>
</beans>