Spring详解二:属性注入方式介绍

一:spring
1:User.java
package cn.itcast.bean;
public class User {
	public User() {
			System.out.println("User对象空参构造方法!!!!");
	}
	private String name;
	private Integer age;
	private Car car;
	public User(String name, Car car) {
		System.out.println("User(String name, Car car)!!");
		this.name = name;
		this.car = car;
	}
	public User(Car car,String name) {
		System.out.println("User(Car car,String name)!!");
		this.name = name;
		this.car = car;
	}
	public User(Integer name, Car car) {
		System.out.println("User(Integer name, Car car)!!");
		this.name = name+"";
		this.car = car;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void init(){
		System.out.println("我是初始化方法!");
	}
	public void destory(){
		System.out.println("我是销毁方法!");
	}
}
---------------------------------------------
--------------------------------------------
package cn.itcast.bean;
public class Car {
	private String  name;
	private String color;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}
-----------------------------------------------
-----------------------------------------------
package cn.itcast.c_injection;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class CollectionBean {
	private Object[] arr;//数组类型注入
	private List list;//list/set 类型注入
	private Map map;//map类型注入
	private Properties prop;//properties类型注入
	
	public Object[] getArr() {
		return arr;
	}
	public void setArr(Object[] arr) {
		this.arr = arr;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	@Override
	public String toString() {
		return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", prop=" + prop
				+ "]";
	}
}
2:配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://www.springframework.org/schema/beans"
		xmlns:p="http://www.springframework.org/schema/p"
		 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
	<!-- 将User对象交给spring容器管理 -->
	<!-- Bean元素:使用该元素描述需要spring容器管理的对象
			class属性:被管理对象的完整类名.
			name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.  
					可以重复.可以使用特殊字符.
			id属性: 与name属性一模一样. 
					名称不可重复.不能使用特殊字符.
			结论: 尽量使用name属性.
	  -->
	<!-- set方式注入: -->
	<!-- init-method:创建对象后调用init()方法初始化对象
		destroy-method:销毁对象前调用destory()方法 -->
	<bean  name="user" class="cn.itcast.bean.User"
	 init-method="init" destroy-method="destory"  >
		<!--值类型注入: 为User对象中名为name的属性注入tom作为值 -->
		<property name="name" value="tom" ></property>
		<property name="age"  value="18" ></property>
		<!-- 引用类型注入: 为car属性注入下方配置的car对象 -->
		<property name="car"  ref="car" ></property>
	</bean>
	
	<!-- 将car对象配置到容器中 -->
	<bean name="car" class="cn.itcast.bean.Car" >
		<property name="name" value="兰博基尼" ></property>
		<property name="color" value="黄色" ></property>
	</bean>
<!-- ============================================================ -->
	<!-- 构造函数注入 -->
<bean name="user2" class="cn.itcast.bean.User" >
	<!-- name属性: 构造函数的参数名 -->
	<!-- index属性: 构造函数的参数索引 意思是构造参数的第几个参数-->
	<!-- type属性: 构造函数的参数类型-->
	<constructor-arg name="name" index="0" type="java.lang.Integer" value="999"  ></constructor-arg>
	<constructor-arg name="car" ref="car" index="1" ></constructor-arg>
</bean>

<!-- ============================================================= -->

<!-- p名称空间注入, 走set方法  (了解)
	1.导入P名称空间  xmlns:p="http://www.springframework.org/schema/p"
	2.使用p:属性完成注入
		|-值类型: p:属性名="值"
		|-对象类型: p:属性名-ref="bean名称"
 -->
	<bean  name="user3" class="cn.itcast.bean.User" p:name="jack" p:age="20" p:car-ref="car"  >
	</bean>
	
<!-- ============================================================= -->
<!-- 
	spel注入: spring Expression Language sping表达式语言 (了解)
 -->
<bean  name="user4" class="cn.itcast.bean.User" >
		<property name="name" value="#{user.name}" ></property>
		<property name="age" value="#{user3.age}" ></property>
		<property name="car" ref="car" ></property>
</bean>
<!-- ============================================================= -->
<!-- 复杂类型注入 -->
<bean name="cb" class="cn.itcast.c_injection.CollectionBean" >
	<!-- 如果数组中只准备注入一个值(对象),直接使用value|ref即可 
	<property name="arr" value="tom" ></property>
	-->
	<!-- array注入,多个元素注入 -->
	<property name="arr">
		<array>
			<value>tom</value>
			<value>jerry</value>
			<ref bean="user4" />
		</array>
	</property>
	
	<!-- 如果List中只准备注入一个值(对象),直接使用value|ref即可 
	<property name="list" value="jack" ></property>-->
	<property name="list"  >
		<list>
			<value>jack</value>
			<value>rose</value>
			<ref bean="user3" />
		</list>
	</property>
	<!-- map类型注入 -->
	<property name="map"  >
		<map>
			<entry key="url" value="jdbc:mysql:///crm" ></entry>
			<entry key="user" value-ref="user4"  ></entry>
			<entry key-ref="user3" value-ref="user2"  ></entry>
		</map> 
	</property>
	<!-- prperties 类型注入 -->
	<property name="prop"  >
		<props>
			<prop key="driverClass">com.jdbc.mysql.Driver</prop>
			<prop key="userName">root</prop>
			<prop key="password">1234</prop>
		</props>
	</property>
</bean>
</beans>
测试用例
package cn.itcast.c_injection;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.bean.User;

public class Demo {
	@Test
	public void fun1(){
		
		//1 创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/c_injection/applicationContext.xml");
		//2 向容器"要"user对象
		User u = (User) ac.getBean("user");
		//3 打印user对象
		System.out.println(u);
		
	}
	@Test
	public void fun2(){
		
		//1 创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/c_injection/applicationContext.xml");
		//2 向容器"要"user对象
		User u = (User) ac.getBean("user2");
		//3 打印user对象
		System.out.println(u);
		
	}
	
	@Test
	public void fun3(){
		
		//1 创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/c_injection/applicationContext.xml");
		//2 向容器"要"user对象
		User u = (User) ac.getBean("user3");
		//3 打印user对象
		System.out.println(u);
		
	}
	
	@Test
	public void fun4(){
		
		//1 创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/c_injection/applicationContext.xml");
		//2 向容器"要"user对象
		User u = (User) ac.getBean("user4");
		//3 打印user对象
		System.out.println(u);
		
	}
	
	@Test
	public void fun5(){
		
		//1 创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/c_injection/applicationContext.xml");
		//2 向容器"要"user对象
		CollectionBean cb = (CollectionBean) ac.getBean("cb");
		//3 打印user对象
		System.out.println(cb);
		
	}
}

你可能感兴趣的:(Spring)