Spring中IOC注入方法

博客主要介绍了Spring的注入方式及属性配置。包括普通注入,在applicationContext.xml的property中设置属性值;构造注入,通过construtor - arg标签标记属性位置;传值注入,与构造注入类似,只是将index换成name属性。

(1)普通注入及属性:
Person类:

package bean;

public class Person {
	private String name;
	private int age;
	private String address;
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Person(String name, int age, String address) {
		super();
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", address=" + address + "]";
	}
	

}

applicationContext.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:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
	<bean id="person" class="bean.Person">
		<property name="name" value="mcmspring"></property>
	</bean>
</beans>

在这个xml中的property中name写入属性值,在value中输入内容

(2)传值注入和构造注入
实体类

public interface IAxe {	
	public void chop();	
}

public interface IPerson {	
	public void userAxe();	
}

public class Chinese implements IPerson {
	private IAxe axe;
	public void useAxe() {		
		axe.chop();
	}
}

public class American implements IPerson {
	private IAxe axe;
	public void useAxe() {		
		axe.chop();
	}
}

public class SteelAxe implements IAxe {
	public void chop() {
		System.out.println("钢斧砍柴非常快!!!");
	}
}


public class StoneAxe implements IAxe {
	public void chop() {
		System.out.println("石斧砍柴慢!!!");
	}
}

1.构造注入

ApplecationContext.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:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
	<!-- 构造注入 -->
	<bean id="American" class="bean2.American">
		<constructor-arg index="0" ref="StoneAxe"/>
	</bean>
			<bean id="StoneAxe" class="bean2.StoneAxe"></bean>
	<bean id="Chinese" class="bean2.Chinese">
		<constructor-arg index="0" ref="SteelAxe"/>
	</bean>
	<bean id="SteelAxe" class="bean2.SteelAxe"></bean>
</beans>

构造注入中的construtor-arg标签中index是用来标记在构造方法中属性位置的,紧随其后的bean标签是为了写入引用数据类型,id和ref对应,class是数据类型的位置.

2.传值注入
ApplecationContext.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:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

	<!-- 传值注入 -->
	<bean id="American" class="bean2.American"> 
    	<property name="axe" ref="SteelAxe" /> 
	</bean> 
	<bean id="Chinese" class="bean2.Chinese"> 
	    <property name="axe" ref="StoneAxe" /> 
	</bean> 
	<bean id="SteelAxe" class="bean2.SteelAxe" />
	<bean id="StoneAxe" class="bean2.StoneAxe " />
</beans>

传值注入中和构造注入基本一样,但是index直接换成name属性,在后来的bean中也是和上面一样对应

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月光如春风拂面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值