Spring框架DI

一、DI是什么

  DI是Dependency Injection,即依赖注入,DI实际上和IOC是一回事,只是观察的角度不同而已。

  bean对象的创建依赖于容器,bean对象的创建依赖于资源(常量、对象、数组......)

什么是注入?

  注入:创建对象时所需要的资源由容器进行配置和注入,注入的过程也称为装配。

二、注入的资源可以有哪些?

      注入的资源可以是常量、对象、数组、集合等,下面就用java程序来进行详细的讲解。

      这是一个java的bean类Student,这里有私有成员常量,对象,数组,List、Set、Map集合,可空的常量以及properties文件的内容,注入必须配置成员的setter访问器。

 
  
package cn.hl.bean;

import java.util.Arrays;
package cn.hl.bean;


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


public class Student {
	//字符串成员属性
	private String name;
	//对象成员属性
	private Address addr;
	//数组成员属性
	private String[] friends;
	//List集合成员属性
	private List<String> hobby;
	//Set集合成员属性
	private Set<String> games;
	//Map集合成员属性
	private Map<String,Integer> scores;
	//可控字符串成员属性
	private String wife;
	//注入Properties文件的内容
	private Properties pro;
	


	public void show(){
		System.out.println("name = "+name);
		System.out.println("addr = "+addr);
		//Array数组转为字符串
		System.out.println("friends = "+Arrays.toString(friends));
		System.out.println("hobby = "+hobby);
		System.out.println("games = "+games);
		System.out.println("scores = "+scores);
		System.out.println("wife = "+wife);
		System.out.println("pro = "+pro);
	}


	public void setGames(Set<String> games) {
		this.games = games;
	}


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


	public void setAddr(Address addr) {
		this.addr = addr;
	}
	
	public void setScores(Map<String, Integer> scores) {
		this.scores = scores;
	}


	public void setHobby(List<String> hobby) {
		this.hobby = hobby;
	}


	public void setFriends(String[] friends) {
		this.friends = friends;
	}
	
	public void setWife(String wife) {
		this.wife = wife;
	}


	public void setPro(Properties pro) {
		this.pro = pro;
	}


	public Student() {
	}
	
}
Student类中引用的Address类
package cn.hl.bean;

public class Address {
	private String city;
	private String street;
	
	public Address() {
	}

	public Address(String city, String street) {
		this.city = city;
		this.street = street;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	@Override
	public String toString() {
		return "Address [city=" + city + ", street=" + street + "]";
	}
	
	
}

beans.xml配置文件的配置

<?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:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang
		http://www.springframework.org/schema/lang/spring-lang.xsd">
	
	
	<bean name="addr" class="cn.hl.bean.Address">
		<property name="city" value="北京"></property>
		<property name="street" value="大榆树街"></property>
	</bean>
	<bean name="stu1" class="cn.hl.bean.Student">
		<!-- 字符串成员属性 -->	
		<property name="name" value="Tom"></property>
		
		<!-- 对象成员属性 -->
		<property name="addr" ref="addr"></property>
		
		<!-- 数组成员属性 -->
		<property name="friends">
			<array>
				<value>Jerry</value>
				<value>Lucy</value>
				<value>Mike</value>
			</array>
		</property>
		
		<!-- List集合成员属性 -->
		<property name="hobby">
			<list>
				<value>BasketBall</value>
				<value>PingPong</value>
				<value>Swimming</value>
			</list>
		</property>
		
		<!-- Set集合成员属性 -->
		<property name="games">
			<set>
				<value>DNF</value>
				<value>CF</value>
				<value>LOL</value>
			</set>
		</property>
		
		<!-- Map集合成员属性 -->
		<property name="scores">
			<map>
				<entry key="Math" value="99"></entry>
				<entry key="Java" value="88"></entry>
			</map>
		</property>
		
		<!-- 可为null -->
		<property name="wife"><null></null></property>
		
		<!-- properties文件内容 -->
		<property name="pro">
			<props>
				<prop key="admin">123456</prop>
				<prop key="root">mysql</prop>
			</props>
		</property>
	</bean>
</beans>

测试类代码

package cn.hl.test;

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

import cn.hl.bean.Student;
import cn.hl.bean.Users;

public class Tester {
	@Test
	public void test1(){
		ApplicationContext ctx= new ClassPathXmlApplicationContext("beans.xml");
		Student stu = (Student) ctx.getBean("stu1");
		stu.show();
	}
}


猜你喜欢

转载自blog.csdn.net/weixin_40828706/article/details/80085883