spring bean与local之区别

Spring中的ref属性指定依赖的3种方式2008-07-09 10:16Spring中,ref属性有三种指定依赖的方式,分别时:local,bean,parent.
下面分别看一下代码:
Java代码
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"  
"http://www.springframework.org/dtd/spring-beans.dtd">   
<beans>   
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" depends="date">   
<property name ="message">   
    <value>HelloWorld</value>   
</property>   
<property name ="date">   
    <ref local="date">   
</property>   
<bean>   
<bean id="date" class="java.util.Date">   
</beans>  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" depends="date">
<property name ="message">
   <value>HelloWorld</value>
</property>
<property name ="date">
   <ref local="date">
</property>
<bean>
<bean id="date" class="java.util.Date">
</beans>


Java代码
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"  
"http://www.springframework.org/dtd/spring-beans.dtd">   
<beans>   
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" depends="date">   
<property name ="message">   
    <value>HelloWorld</value>   
</property>   
<property name ="date">   
    <ref bean="date">   
</property>   
<bean>   
<bean id="date" class="java.util.Date">   
</beans>  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" depends="date">
<property name ="message">
   <value>HelloWorld</value>
</property>
<property name ="date">
   <ref bean="date">
</property>
<bean>
<bean id="date" class="java.util.Date">
</beans>


Java代码
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"  
"http://www.springframework.org/dtd/spring-beans.dtd">   
<beans>   
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" depends="date">   
<property name ="message">   
    <value>HelloWorld</value>   
</property>   
<property name ="date">   
    <ref parent="date">   
</property>   
<bean>   
<bean id="date" class="java.util.Date">   
</beans>  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" depends="date">
<property name ="message">
   <value>HelloWorld</value>
</property>
<property name ="date">
   <ref parent="date">
</property>
<bean>
<bean id="date" class="java.util.Date">
</beans>

下面看看它们之间的区别:
local用于本地,即同文档中的bean
bean可以跨xml文档,同时bean属性的值可以和依赖的Bean的id属性相同,也可以和name属性相同。
parent:容许应用当前BeanFactory或ApplicationContext的父BeanFactory或者ApplicationContext中的Bean,同时bean属性的值可以和依赖的Bean的id属性相同,也可以和name属性相同。

你可能感兴趣的:(spring,xml,bean)