spring装配bean及参数注入

spring所需jar包各个版本可以在 http://repo.springsource.org/libs-release-local/org/springframework/spring/下载

个人使用建议:不要使用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>

猜你喜欢

转载自blog.csdn.net/yuan52007298/article/details/81220403