引用其它 Bean
组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用.
也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean
<?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.xsd">
<!-- 配置Car, 属性注入(set方法注入) -->
<bean id="car" class="com.learn.spring.beans.Car">
<property name="brand" >
<value>Audi</value>
</property>
<property name="crop" value="yiqi"></property>
<property name="price" value="400000"></property>
</bean>
<bean id="person" class="com.learn.spring.beans.Person">
<property name="name" value="莫小贝"></property>
<property name="age" value="12"></property>
<!-- 引用其他的bean ref:指定要引用的bean -->
<property name="car" ref="car">
<!-- <ref bean="car"/> -->
</property>
<!-- <constructor-arg value="" ref=""></constructor-arg> -->
</bean>
</beans>
package com.learn.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.spring.beans.Book;
import com.learn.spring.beans.Car;
import com.learn.spring.beans.HelloWorld;
import com.learn.spring.beans.Person;
import com.learn.spring.beans.PersonList;
import com.learn.spring.beans.PersonMap;
public class Main {
public static void main(String[] args) {
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}