设值注入
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean id="icupblue" class="com.practice.entity.PaperCup">
<property name="color" value="blue"></property>
</bean>
<bean id="icupblack" class="com.practice.entity.PaperCup">
<property name="color" value="black"></property>
</bean>
<bean id="Ipeople" class="com.practice.entity.PeopleImp">
<property name="cup" ref="icupblue"></property>
<property name="cup1" ref="icupblack"></property>
</bean>
</beans>
package com.practice.entity;
public interface ICup {
public void fillWater();
}
package com.practice.entity;
public interface Ipeople {
public void drink();
}
package com.practice.entity;
public class PaperCup implements ICup{
String color;
public void setColor(String color) {
this.color = color;
}
@Override
public void fillWater() {
System.out.println("纸杯里的酒是"+this.color+"的");
}
}
package com.practice.entity;
public class PeopleImp implements Ipeople{
private ICup cup;
private ICup cup1;
public void setCup1(ICup cup1) {
this.cup1 = cup1;
}
public void setCup(ICup cup) {
//可理解为this.cup=ClassPathXmlApplicationContext("applicationContext.xml").getBean("icupblue");
this.cup = cup;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("你好!!");
}
@Override
public void drink() {
// TODO Auto-generated method stub
this.cup.fillWater();
this.cup1.fillWater();
}
}
package com.practice.test;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.practice.entity.ICup;
import com.practice.entity.Ipeople;
import com.sun.glass.ui.Application;
public class mn {
public static void main(String[]args){
ClassPathXmlApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Ipeople ipeople=(Ipeople) aContext.getBean("Ipeople");
ipeople.drink();
}
}
总结: 1. 被依赖的属性需要定义set()方法; 2. 配置文件中使用<property>标签。如果是简单属性,使用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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean id="icupblue" class="com.practice.entity.PaperCup">
<property name="color" value="blue"></property>
</bean>
<bean id="icupblack" class="com.practice.entity.PaperCup">
<property name="color" value="black"></property>
</bean>
<bean id="Ipeople" class="com.practice.entity.PeopleImp">
<!-- <property name="cup" ref="icupblue"></property>
<property name="cup1" ref="icupblack"></property> -->
<constructor-arg ref="icupblue" index="0"></constructor-arg>
<constructor-arg ref="icupblack" index="1"></constructor-arg>
<constructor-arg value="ye" index="2"></constructor-arg>
</bean>
</beans>
package com.practice.entity;
public class PeopleImp implements Ipeople{
private ICup cup;
private ICup cup1;
private String name;
public PeopleImp() {
}
public PeopleImp(ICup cup, ICup cup1,String name) {
super();
this.cup = cup;
this.cup1 = cup1;
this.name=name;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("你好!!");
}
@Override
public void drink() {
// TODO Auto-generated method stub
this.cup.fillWater();
this.cup1.fillWater();
System.out.println(name+"喝茶!!!");
}
}