基于XML的di(注入)

有三种 1 设值注入 2 构造注入 3 实现特定接口注入(因为要实现特定接口 是侵入式编程 污染代码 基本不用)

设值注入:通过setter方法注入 value 基本数据类型和字符串 ref 传引用

<?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">

		<!-- 注册School-->
		<bean id="mySchool" class="com.gqc.di01.School">
			<property name="name" value="清华大学" />
		</bean>
		
		
		<!-- 注册Student-->
		<bean id="myStudent" class="com.gqc.di01.Student">
			<property name="name" value="张三"/>
			<property name="age" value="11" />
			<property name="school" ref="mySchool"/>
		</bean>
</beans>
package com.gqc.di01;

public class Student {
	private String name;
	private int age;
	private School school;//对象属性 域属性
	public void setName(String name) {
		System.out.println("执行setName()");
		this.name = name;
	}
	public void setAge(int age) {
		System.out.println("执行setAge()");
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school
				+ "]";
	}
	public void setSchool(School school) {
		this.school = school;
	}
	
}
package com.gqc.di01;

public class School {
	private String name;

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}


package com.gqc.di01;

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



public class MyTest {

		@Test
		public void test01() {
			//创建容器对象 加载Spring 配置文件
			String resource = "com/gqc/di01/appliactionContext.xml";
			ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
			Student student=(Student) ac.getBean("myStudent");
			System.out.println(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.xsd">

		<!-- 注册School-->
		<bean id="mySchool" class="com.gqc.di02.School">
			<property name="name" value="清华大学" />
		</bean>
		 
		
		
		<!-- 注册Student-->
		<bean id="myStudent" class="com.gqc.di02.Student">
		
		 <constructor-arg name="name" value="李四"/>
		<constructor-arg name="age" value="24"/>
		<constructor-arg name="school" ref="mySchool" />
		
		
		<!-- <constructor-arg index="0" value="李四"/>
		<constructor-arg index="1" value="24"/>
		<constructor-arg index="2" ref="mySchool" />
 -->		
		
		<!-- <constructor-arg  value="李四"/>
		<constructor-arg value="24"/>
		<constructor-arg ref="mySchool" />
 		-->
 				
 	</bean>
</beans>


package com.gqc.di02;

public class Student {
	private String name;
	private int age;
	private School school;//
	
	
	
	public Student(String name, int age, School school) {
		super();
		this.name = name;
		this.age = age;
		this.school = school;
	}
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}



	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school
				+ "]";
	}

	
}
package com.gqc.di02;

public class School {
	private String name;

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}
package com.gqc.di02;

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



public class MyTest {

		@Test
		public void test01() {
			//创建容器对象 加载Spring 配置文件
			String resource = "com/gqc/di02/appliactionContext.xml";
			ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
			Student student=(Student) ac.getBean("myStudent");
			System.out.println(student);
		}
		
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值