spring学习笔记-xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"

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

    <!--lazy-init 延迟加载 默认为false 容器初始化完成之后就会构造实例 如果为true的话在我获取该bean的时候才会构造-->
    <!--scope 作用域 用来控制容器中bean的产生机制 默认为singleton 单利模式  整个容器中只有一个实例-->
    <bean id="user" class="com.java12.spring.demo.User" lazy-init="true" scope="prototype" >
        <property name="id" value="1"></property>
        <property name="name" value="tom"></property>
    </bean>

    <util:list id="bo">
        <value>this is java</value>
        <value>java 核心</value>
        <value>java 实战</value>
    </util:list>

    <!--对于集合list属性的注入方式-->
    <bean id="bookstore" class="com.java12.spring.entity.BookStop" p:id="123" p:books-ref="bo"></bean>

    <!--对于集合set属性的注入 list与set方式是通用的-->
    <bean id="math" class="com.java12.spring.entity.Math">
        <property name="name" value="质数"></property>
        <property name="nums">
            <set>
                <value>1</value>
                <value>5</value>
                <value>10</value>
                <value>3</value>
            </set>
        </property>
    </bean>

    <!--对于集合map属性的注入-->
    <bean id="person" class="com.java12.spring.entity.Person">
        <property name="age" value="18"></property>
        <property name="idCards">
            <map>
                <entry key="2121613213" value="213213213"></entry>
                <entry key="54215" value="4564652313"></entry>
            </map>
        </property>
    </bean>

    <!--构造器注入 根据构造器的索引进行注入(无法确认注入的值是否是自己想要的)-->
    <bean id="construct" class="com.java12.spring.entity.Contruct">
        <constructor-arg index="0" value="101"></constructor-arg>
        <constructor-arg index="1" value="tom"></constructor-arg>
        <constructor-arg index="2" value="10.2"></constructor-arg>
    </bean>

    <!--用名字来指定构造器的设值-->
    <bean id="con" class="com.java12.spring.entity.Contruct">
        <constructor-arg name="id" value="101"></constructor-arg>
        <constructor-arg name="name" value="guo"></constructor-arg>
        <constructor-arg name="weight" value="91.5"></constructor-arg>
    </bean>

    <!--数组形式-->
    <bean id="arr" class="com.java12.spring.entity.Arrays" p:id="10" p:arr="abc,asd,dsa"></bean>
</beans>

xml里面配置bean的各种属性

id="xxx" spring是通过id来获取实例的
class="类的全路径"  spring根据class路径反射创建实例
lazy-init="true" 延迟加载 指定类的实例创建时间 默认为false  在加载配置文件的时候就创建类 

scope="prototype" 作用域 用来控制容器中bean的产生机制 默认为singleton 单例模式 整个容器中只有一个实例(单利的好处 节省内存)
<util:list></util:list><util:map></util:map> 使用util标签来对引用对象赋值
<map>
    <entry key="2121613213" value="213213213"></entry> map集合的注入
</map>
<constructor-arg index="0" value="101"></constructor-arg> 构造器注入 根据下标有时候回无法区分
<constructor-arg name="id" value="101"></constructor-arg>根据name可以区分

猜你喜欢

转载自blog.csdn.net/xiaosuanmiao123/article/details/82389190