码迷,mamicode.com
首页 > 编程语言 > 详细

Spring初学之bean之间的关系和bean的作用域

时间:2017-04-12 23:25:55      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:xsd   more   value   res   oca   schema   http   address   utf-8   

一、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">
<bean id="car" class="spring.beans.autowire.Car" 
p:carName="奥迪^" p:price="300000"></bean>

<!-- 继承 -->
<bean id="car2" class="spring.beans.autowire.Car"
 p:price="100000" parent="car"></bean>

<!-- 抽象类 -->
<bean id="address1" p:city="重庆" abstract="true"></bean>

<bean id="address" class="spring.beans.autowire.Address"
p:more="梁平" parent="address1"></bean>

<!-- 依赖 -->
<bean id="person" class="spring.beans.relation.Person" p:name="张三" p:car-ref="car"
depends-on="address" p:address-ref="address">
</bean>

</beans>

二、bean的作用域

  常用的两个作用域就是scope="singleton"和scope="prototype"

<?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的作用域 scope-->
    <bean id="car" class="spring.beans.scope.Car" scope="prototype">
        <property name="name" value="奥迪"></property>
        <property name="price" value="300000"></property>
    </bean>

</beans>

 

Spring初学之bean之间的关系和bean的作用域

标签:xsd   more   value   res   oca   schema   http   address   utf-8   

原文地址:http://www.cnblogs.com/hyyq/p/6701632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!