版权声明:仅供学习交流使用 https://blog.csdn.net/drxRose/article/details/84933601
官方xsd文档中关于ref的参考说明
原文链接:http://www.springframework.org/schema/beans/spring-beans.xsd
1.标签ref:
Defines a reference to another bean in this factory or an external factory (parent or included factory).
2.bean标签内的属性ref:
A short-cut alternative to a nested "<ref bean='...'/>" element.
3.也可使用内部bean的方式,引入其他的bean.
使用的类对象
package siye;
public class Person
{
}
package siye;
public class User
{
Person person;
public void setPerson(Person person)
{
this.person = person;
}
}
xml配置文件,文件名`config.xml`
<bean id="thePerson" class="siye.Person" />
<!--使用ref标签或属性 -->
<bean id="obj0" class="siye.User">
<property name="person">
<ref bean="thePerson" />
</property>
</bean>
<bean id="obj1" class="siye.User">
<property name="person" ref="thePerson" />
</bean>
<!--使用inner-bean -->
<bean id="obj2" class="siye.User">
<property name="person">
<bean class="siye.Person" />
</property>
</bean>
测试类
package siye;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTest
{
public static void main(String[] args)
{
String path = "classpath:/siye/config.xml";
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(path);
System.out.println(context.getBean("obj0", User.class));
System.out.println(context.getBean("obj1", User.class));
System.out.println(context.getBean("obj2", User.class));
context.close();
}
}