spring中的配置文件中的property标签中的ref属性和value属性有什么不同?

请问spring中的配置文件中的property标签中的ref属性和value属性有什么不同?

2个回答


<!-- insert dependencies as required as here -->


class="org.springframework.aop.framework.ProxyFactoryBean">

<!-- notice how we refer to the parent bean -->

<!-- insert other configuration and dependencies as required here -->

下面是关于ref的说明,ref是Here you set the value of the specified property of a bean to be a reference to another bean (a collaborator) managed by the container
The ref element is the final element inside a or definition element. Here you set the value of the specified property of a bean to be a reference to another bean (a collaborator) managed by the container. The referenced bean is a dependency of the bean whose property will be set, and it is initialized on demand as needed before the property is set. (If the collaborator is a singleton bean, it may be initialized already by the container.) All references are ultimately a reference to another object. Scoping and validation depend on whether you specify the id/name of the other object through the bean, local, or parent attributes.
Specifying the target bean through the bean attribute of the tag is the most general form, and allows creation of a reference to any bean in the same container or parent container, regardless of whether it is in the same XML file. The value of the bean attribute may be the same as the id attribute of the target bean, or as one of the values in the name attribute of the target bean.
而value已办是设定一些固定值的参数,希望对你有所帮助。可以参考下面文档
http://docs.spring.io/spring/docs/4.3.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-ref-element

 <bean id="accountService" class="com.foo.SimpleAccountService">
</bean>
<bean id="accountService" 
    class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
        <ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
    </property>   
</bean>

下面是关于ref的说明,ref是Here you set the value of the specified property of a bean to be a reference to another bean (a collaborator) managed by the container
The ref element is the final element inside a or definition element. Here you set the value of the specified property of a bean to be a reference to another bean (a collaborator) managed by the container. The referenced bean is a dependency of the bean whose property will be set, and it is initialized on demand as needed before the property is set. (If the collaborator is a singleton bean, it may be initialized already by the container.) All references are ultimately a reference to another object. Scoping and validation depend on whether you specify the id/name of the other object through the bean, local, or parent attributes.
Specifying the target bean through the bean attribute of the tag is the most general form, and allows creation of a reference to any bean in the same container or parent container, regardless of whether it is in the same XML file. The value of the bean attribute may be the same as the id attribute of the target bean, or as one of the values in the name attribute of the target bean.
而value已办是设定一些固定值的参数,希望对你有所帮助。可以参考下面文档
http://docs.spring.io/spring/docs/4.3.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-ref-element

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐