spring02

练习了关于spring bean的继承、依赖、自动配置。

具体的练习的ioc容器如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 关于bean的自动匹配 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
    <bean id="address" class="lib3.Address" p:city="beijing" p:street="bdjsdvj"></bean>
     <bean id="car" class="lib3.Car" p:brand="asckj" p:price="200"></bean>
     <!--可以使用autowrite属性指定自动装配方式。byname根据bean的名字和当前的bean的setter风格的属性名进行装配  
     弱有匹配的自动装配,若没有则不装配
     bytype根据bean的类型的当前的bean的属性的类型进行自动的装配-->
     <!-- 使用gettype就要必须保证ioc中只能有一个类型匹配的bean对象。否则会报错 -->
     <bean id="person" class="lib3.Person" p:name="ehfi" autowire="byName"></bean>
</beans>
<?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="address" class="lib3.Address" p:city="cdni" p:street="cncc"></bean>
    <!-- 加上abstract="true"就表示这个bean实例只能作为模板而不能被引用只能用来做继承 -->
    <!--     <bean id="address" class="lib3.Address" p:city="cdni" p:street="cncc" abstract="true"></bean> -->
    <!--  <bean id="address2" class="lib3.Address" p:city="sss" p:street="sssss"></bean>-->
    <!-- bean 配置的继承,使用bean的parent属性指定继承哪个bean配置 -->
    <!-- 若某一个class没有。那就是一个模板,而且必须有abstract -->
    <bean id="address2" p:street="cdbsj" parent="address"></bean>
    <bean id="car" class="lib3.Car"
    p:brand="aodei" p:price="300"></bean>
    <bean id="person" class="lib3.Person"
    p:name="tom" p:address-ref="address2" depends-on="car"></bean>
    <!-- 要求在配置person时必须有一个关联的car。也就是person依赖于car -->

</beans>
原文地址:https://www.cnblogs.com/dazhi151/p/12584740.html