组成应用程序的Bean经常需要相互协作以完成应用程序的功能,要使Bean能够相互访问,就必须在Bean配置文件中指定对Bean的引用。在Bean的配置文件中,可以通过<ref>元素或ref属性为Bean的属性或构造器参数指定对Bean的引用。也可以在属性或构造器里包含Bean的声明,这样的Bean称为内部Bean.
首先:简单地举一个例子:代码如下:
使用<ref>元素对bean的引用的Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="helloWord" class="HelloWord">
<property name="name" value="小米"></property>
</bean>-->
<bean id="car" class="Car">
<constructor-arg value="baoma" index="1"></constructor-arg>
<constructor-arg value="12" index="0"></constructor-arg>
<constructor-arg value="10000" index="2" type="double"></constructor-arg>
</bean>
<bean id="person" class="Person">
<property name="name" value="for"></property>
<property name="age" value="56"></property>
<!--引用bean时,也可以使用元素ref去引用-->
<property name="car">
<ref bean="car"/>
</property>
</bean>
</bean>
使用 ref属性对bean的引用的Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="helloWord" class="HelloWord">
<property name="name" value="小米"></property>
</bean>-->
<bean id="car" class="Car">
<constructor-arg value="baoma" index="1"></constructor-arg>
<constructor-arg value="12" index="0"></constructor-arg>
<constructor-arg value="10000" index="2" type="double"></constructor-arg>
</bean>
<bean id="person" class="Person">
<property name="name" value="for"></property>
<property name="age" value="56"></property>
<!--引用bean时,使用属性ref去引用-->
<property name="car" ref="car"></property>
</bean>
<!-- <bean id="helloWord1" class="HelloWord">
<property name="name" value="小车"></property>
</bean>-->
</beans>
使用内部Bean对Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="Person">
<property name="name" value="for"></property>
<property name="age" value="56"></property>
<property name="car">
<bean id="car" class="Car">
<constructor-arg value="baoma" index="1"></constructor-arg>
<constructor-arg value="12" index="0"></constructor-arg>
<constructor-arg value="10000" index="2" type="double"></constructor-arg>
</bean>
</property>
</bean>
</beans>
person类文件:
public class Person { public String name; public int age; public Car car; 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 Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } }
car类的文件:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CarMain { public static void main(String [] mains){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml"); Person person = (Person) applicationContext.getBean ("person"); System.out.println (person); } }
public class Car { public String name; public int sum; public int num; public double price; public Car(int sum, String name, double price) { this.name = name; this.sum = sum; this.price = price; } public Car(int sum,String name,int num){ this.sum = sum; this.name = name; this.num = num; } public Car(String name,int sum,int num){ this.name = name; this.sum = sum; this.num = num; } public String toString(){ return "车名:" + name + "数量:" + sum + "单价:" + price + "余数:"+ num; } }
main方法:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CarMain {
public static void main(String [] mains){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
Person person = (Person) applicationContext.getBean ("person");
System.out.println (person);
}
}
输出结果:
注意点:内部Bean不能被外部Bean去引用的,只能在内部使用。
以上的引用功能,如果用构造注入去配置属性时,原理
public class Person { public String name; public int age; public Car car; 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 Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Person(String name,int age,Car car){ this.name = name; this.age = age; this.car = car; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } }
也是一样的。
Spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--用构造器去配置属性--> <bean id="person" class="Person"> <constructor-arg value="for"></constructor-arg> <constructor-arg value="56"></constructor-arg> <constructor-arg> <bean id="car" class="Car"> <constructor-arg value="baoma" index="1"></constructor-arg> <constructor-arg value="12" index="0"></constructor-arg> <constructor-arg value="10000" index="2" type="double"></constructor-arg> </bean> </constructor-arg> </bean> </beans>
Person类的配置:
public class Person { public String name; public int age; public Car car; 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 Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Person(String name,int age,Car car){ this.name = name; this.age = age; this.car = car; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } }
car类文件:
public class Car { public String name; public int sum; public int num; public double price; public Car(int sum, String name, double price) { this.name = name; this.sum = sum; this.price = price; } public Car(int sum,String name,int num){ this.sum = sum; this.name = name; this.num = num; } public Car(String name,int sum,int num){ this.name = name; this.sum = sum; this.num = num; } public String toString(){ return "车名:" + name + "数量:" + sum + "单价:" + price + "余数:"+ num; } }
main文件
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CarMain { public static void main(String [] mains){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml"); Person person = (Person) applicationContext.getBean ("person"); System.out.println (person); } }
输出结果:
其实原理都是一样的,只是注入的方式不同而已。