美文网首页
spring bean 配置

spring bean 配置

作者: 七宝qb | 来源:发表于2017-04-05 15:29 被阅读39次

bean属性: set注入、构造器注入

beans 作用域 :

springBean生命周期
1.Bean的作用域可以通过Bean标签的scope属性进行设置,Bean的作用域包括:默认情况下scope="singleton",那么该Bean是单例,任何人获取该Bean实例的都为同一个实例;scope="prototype",任何一个实例都是新的实例;scope="request",在WEB应用程序中,每一个实例的作用域都为request范围;scope="session",在WEB应用程序中,每一个实例的作用域都为session范围

属性注入

<property name="properties" value="zhangsan" ></property>
<!--在这种引入方式使用之前要引入p命名空间 -->
<bean id="car" class="com.springTest.Car" p:name="zhangsan" p:price="23"/>

构造器注入

 <bean id="person" class="com.springTest.Person">
        <constructor-arg ref="car"></constructor-arg>  //类引用类型的注入
        <constructor-arg index="1" value="18"></constructor-arg>
</bean>  

集合注入

 <!--配置独立的集合配置,以更多的集合引用  在这之前要引入util命名空间-->
    <util:list id="listCar">
        <value>zhang</value>
        <ref bean="car"/>
        <bean  class="com.springTest.Person">
            <constructor-arg ref="car"></constructor-arg>
            <constructor-arg index="1" value="18"></constructor-arg>
            <constructor-arg index="2" value="张三"></constructor-arg>
        </bean>
    </util:list>
    <!--集合bean-->
    <bean id="customer" class="com.springTest.Customer">
        <property name="list">
            <list>
        <!-- 引用之前配置好的 集合配置 -->
                <ref bean="listCar"></ref>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="key1" value="只能高三" value-type="java.lang.String"></entry>
                <entry key="key2">
                    <bean class="com.springTest.Car" p:name="自定义" p:price="23"/>
                </entry>
                <entry key="ket3" value-ref="car"></entry>
            </map>
        </property>
        <!--使用props 和 prop  为propeties 赋值-->
        <property name="properties" >
            <props>
                <prop key="admin" >admin</prop>
                <prop key="psw" >password</prop>
            </props>
        </property>
    </bean>

bean factory

静态工厂

静态工厂类.png bean静态工厂方法.png

实例工厂

实例工厂类.png bean实例工厂方法配置.png

命名空间

// p 的
xmlns:p="http://www.springframework.org/schema/p"
// util 的见 util
xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-4.0.xsd
       http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

自动装配

    <!--自动装配多用于整合第三方框架-->
    <!--可以使用autowire 属性指定装配的方式,-->
    <!--byName 根据bean的名字 和当前 bean的setter属性名字进行装配,没有匹配的不进行装配-->
    <!--bytype 根据 bean的类型进行装配 若同类型有1个以上的则异常-->
    <bean id="person" class="com.springTest.Person" p:name="zhangsna" p:age="23" autowire="byType"></bean>

bean 间关系

1 继承关系
2 依赖关系
    <!--模板父类,abstract 不进行实例化 可以没有class-->
    <bean id="person" p:name="name" p:age="18"  abstract="true"/>
    <!--depends-on 依赖多个bean可用‘,’或空格隔开  parents 继承 person父类-->
    <bean id="oldMan" class="com.springTest.Person" parent="person" p:name="zhangsan" depends-on="car"></bean>

引入外部的配置文件

命名空间:

GUQH7ZC4J4@4Z(FXL}OHPWU.jpg
导入属性文件
![P]VJ7HB6M`HAHO%Z%EZ29.jpg](https://img.haomeiwen.com/i3607687/198398758954033c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
配置中用 ${xxxx} 进行取值。

相关文章

网友评论

      本文标题:spring bean 配置

      本文链接:https://www.haomeiwen.com/subject/tttxattx.html