4.spring di

Posted Kyhoon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.spring di相关的知识,希望对你有一定的参考价值。

spring di,即依赖注入,从应用的浅显意义来讲就是对属性赋值

1.用setter赋值,在spring的applicationContext.xml配置文件的bean下的property标签

  属性name指定属性名,属性value指定值,一般用于基本数据 类型的包装类型

 属性ref指定值,一般用于引用类型,还有list标签,下面value标签,

set标签下面value标签,map标签下面entry,下面分别有key,value,

还有props,下面prop,key

public class Student {
	public void say(){
		System.out.println("student");
	}
}
public class Person {
	private Long pid;//包装类型
	private String pname;//String类型
	private Student student;//引用类型
	
	private List lists;
	
	private Set sets;
	
	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public List getLists() {
		return lists;
	}

	public void setLists(List lists) {
		this.lists = lists;
	}

	public Set getSets() {
		return sets;
	}

	public void setSets(Set sets) {
		this.sets = sets;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	private Map map;
	
	private Properties properties;
}

  

<?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-2.5.xsd">
    <bean id="person" class="cn.di.xml.set.Person">
    	<!-- 
    		property就是代表属性
    		  在spring中基本类型(包装类型和String)都可以用value来赋值
    		                         引用类型用ref赋值
    	 -->
    	<property name="pid" value="5"></property>
    	<property name="pname" value="王五"></property>
    	<property name="student">
    		<ref bean="student"/>
    	</property>
    	<property name="lists">
    		<list>
    			<value>list1</value>
    			<value>list2</value>
    			<ref bean="student"/>
    		</list>
    	</property>
    	<property name="sets">
    		<set>
    			<value>set1</value>
    			<value>set2</value>
    			<ref bean="student"/>
    		</set>
    	</property>
    	<property name="map">
    		<map>
    			<entry key="map1">
    				<value>map1</value>
    			</entry>
    			<entry key="map2">
    				<value>map2</value>
    			</entry>
    			<entry key="map3">
    				<ref bean="student"/>
    			</entry>
    		</map>
    	</property>
    	<property name="properties">
    		<props>
    			<prop key="prop1">
    				prop1
    			</prop>
    		</props>
    	</property>
    </bean>
	<bean id="student" class="cn.di.xml.set.Student"></bean>
</beans>

  

@Test
	public void test(){
               ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)context.getBean("person");
		person.getStudent().say();
		System.out.println(person.getPid());
		System.out.println(person.getPname());
		List lists = person.getLists();
		for(int i=0;i<lists.size();i++){
			System.out.println(lists.get(i).toString());
		}
	}

  

2.利用构造函数赋值

在bean下有constructor-arg标签,里面有index,type,ref,value属性

public class Person {
	private Long pid;
	public Long getPid() {
		return pid;
	}

	public String getPname() {
		return pname;
	}

	public Student getStudent() {
		return student;
	}

	private String pname;
	private Student student;
	
	public Person(Long pid,String pname){
		this.pid = pid;
		this.pname = pname;
	}
	
	public Person(String pname,Student student){
		this.pname = pname;
		this.student = student;
	}
}

  

<?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-2.5.xsd">
    <bean id="person" class="cn.di.xml.constructor.Person">
    	<!-- 
    		构造函数的参数
    		  index  第几个参数,下标从0开始
    		  type   参数的类型
    		  ref    如果类型是引用类型,赋值
    		  value  如果类型是基本类型,赋值
    		 说明:
    		    只能指定一个构造函数
    	 -->
    	<constructor-arg index="0"  type="java.lang.String" value="王五"></constructor-arg>
    	<constructor-arg index="1"  ref="student"></constructor-arg>
    </bean>
    
    <bean id="student" class="cn.di.xml.constructor.Student"></bean>
</beans>

  

以上是关于4.spring di的主要内容,如果未能解决你的问题,请参考以下文章

Java之Spring入门到精通IDEA版Spring的IoC和DI(一篇文章精通系列)

Java之Spring入门到精通IDEA版Spring的IoC和DI(一篇文章精通系列)

text 此片段用于以下知识库文章 - http://kb.wpbeaverbuilder.com/article/591-create-a-filter-to-customize-the-di

Spring概述

Spring的优点

Spring介绍