(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中也是和上面一样对应