鸿 网 互 联 www.68idc.cn

当前位置 : 主页 > 编程语言开发 > java > >

4.spring di

来源:互联网 作者:佚名 时间:2017-09-20 09:28
spring di,即依赖注入,从应用的浅显意义来讲就是对属性赋值 1.用setter赋值,在spring的applicationContext.xml配置文件的bean下的property标签 属性name指定属性名,属性value指定值,一般用于基本数据 类型的包装类型 属性ref指定值,一般用于引用类型,还

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>

  

网友评论
<