zoukankan      html  css  js  c++  java
  • Spring的Bean配置

    <bean id="helloSpring" class="yang.mybatis.test.HelloSpring">
      <property name="name" value="Spring"></property>
    </bean>
    • 标签<property>的name属性值和Javabean的Setter方法名对应
    • 获取IOC容器的同时,Spring调用它所管理的bean的构造器和Setter方法,对bean进行初始化。
    • 如果采用<property>注入的方式时,必须提供一个无参的构造器
    //1.从classpath路径下的applicationContext.xml文件中获取IOC容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从IOC容器中获取bean对象
    //利用ID定位到IOC容器中的Bean
    HelloSpring helloSpring = (HelloSpring) applicationContext.getBean("helloSpring");
    //利用类型返回IOC容器中的Bean,但要求IOC容器中必须只能有一个该类型的Bean
    HelloSpring helloSpring = (HelloSpring) applicationContext.getBean(HelloSpring.class);
    helloSpring.hello();

    3.配置bean

    • class:bean的全类名,通过反射的方式在IOC容器中创建Bean,要求Bean必须有无参的构造器。
    • id: 标识容器中的Bean,在 IOC 容器中必须是唯一的

    4.ApplicationContext 代表Spring IOC容器

    ClassPathXmlApplicationContext:从类路径下加载配置文件
    FileSystemXmlApplicationContext: 从文件系统中加载配置文件

    5.Spring 支持 3 种依赖注入的方式

    • 属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
    • 构造器注入在 <constructor-arg> 元素里声明属性
    //默认通过构造器参数顺序注入
    <bean id="car" class="yang.mybatis.test.Car">
        <constructor-arg value="x89d"></constructor-arg>
        <constructor-arg value="奥迪"></constructor-arg>
        <constructor-arg value="312"></constructor-arg>
    </bean>

    构造器的重载靠参数列表区分不同的方法,所以可以通过类型匹配注入,区分重载的构造器

    <bean id="car" class="yang.mybatis.test.Car">
        <constructor-arg value="x89d" type="java.lang.String"></constructor-arg>
        <constructor-arg value="奥迪" type="java.lang.String"></constructor-arg>
      <constructor-arg value="312" type="double"></constructor-arg>
    </bean>
    • 工厂方法注入()

    6.注入属性值的细节

    --字面值:

    • 可用字符串表示的值,可以通过 <value> 元素子标签标签或 value 属性进行注入。
    <!--标签体可以有空格,整型等其他非字符串类型不能加双引号,String类型可以加双引号,也可以不加-->

    <constructor-arg type="double">
      <value>312</value> 
    </constructor-arg>
    • 基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
    • 若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。例如<value><![CDATA][<三国演义>]></value

    --bean引用值

    组成应用程序的 Bean经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
    在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用.

    <property name="car">
        <ref bean="car"></ref>
    </property>

    如果当前配置文件中配了了一个car,而在配置person时,未配置对car的引用,SpringIOC容器会自动的把配置了的car注入到person对象中。

    Navigate to the autowired dependencies
    也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

    <bean id="person" class="yang.mybatis.test.Person">
        <property name="name" value="yang"></property>
        <property name="age">
            <value>1212</value>
        </property>
        
    <!--内部bean-->
        <property name="car">
         <bean class="yang.mybatis.test.Car">
            <constructor-arg index="0" value="09dx"/>
            <constructor-arg index="1" value="奔驰"/>
            <constructor-arg index="2" value="1212"/>
         </bean>
        </property>
    </bean>       

    内部bean不能被外部引用

    7.级联属性

    可以为当前bean对象的引用对象的属性赋值

    <bean id="person2" class="yang.mybatis.test.Person">
        <constructor-arg value="xiaoming" type="java.lang.String"></constructor-arg>
      <constructor-arg value="12" type="int"></constructor-arg>
      <constructor-arg ref="car"></constructor-arg>
      <property name="car.price" value="44444"></property>
    </bean>

    <bean id="person" class="yang.mybatis.test.Person">   <property name="name" value="yang"></property>   <property name="age" value="1212"></property>   <property name="car" ref="car"></property>   <property name="car.price" value="33333"></property> </bean>

    在多个对象引用了该bean对象的场景下,因为bean交给Sping IOC容器来管理,所以如果对该bean对象的属性进行了更改的话,全部引用了该bean对象的对象中的这个bean对象的属性都被更改了。

    8.配置集合属性

    在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性

    ①.配置 java.util.List 类型的属性, 需要指定 <list> 标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.

    <bean id="person" class="yang.mybatis.test.Person">
      <property name="age" value="12"></property>
      <property name="name" value="xiaoming"></property>
      <property name="cars" >
        <list>
          <ref bean="car"></ref>
          <ref bean="car2"></ref>
          <bean class="yang.mybatis.test.Car">
            <constructor-arg index="0" value="09dx"/>
            <constructor-arg index="1" value="奔驰"/>
            <constructor-arg index="2" value="1212"/>
          </bean>
        </list>
      </property>
    </bean>

    ②.Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值.

    <!--使用map节点及map的entry子节点配置Map类型的成员变量-->
    <property name="mapCars">
      <map>
        <entry key="AA" value-ref="car"></entry>
        <entry key="BB" value-ref="car2"></entry>
      </map>
    </property>

    ③.使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.

    <bean id="ddataSource" class="yang.mybatis.test.DataSource">
      <property name="properties">
        <props>
          <prop key="username">root</prop>
          <prop key="password">1234</prop>
          <prop key="driver">com.mysql.jdbc.Driver</prop>
          <prop key="url">jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8</prop>
        </props>
      </property>
    </bean>

    ④.使用 utility scheme 定义集合;添加约束,util命名空间.

    xmlns:util="http://www.springframework.org/schema/util"

    可以使用 util schema 里的集合标签定义独立的集合 Bean

    <util:list id="cars">
      <ref bean="car"></ref>
      <ref bean="car2"></ref>
    </util:list>
    
    
    <util:map id="mapCars">
      <entry key="AA" value-ref="car"></entry>
      <entry key="BB" value-ref="car2"></entry>
    </util:map>
    
    
    <bean id="person" class="yang.mybatis.test.Person">
      <property name="age" value="12"></property>
      <property name="name" value="daming"></property>
      <property name="cars" ref="cars"></property>
      <property name="mapCars" ref="mapCars"></property>
    </bean>

    ⑤.Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性。使用 p 命名空间后,基于 XML 的配置方式将进一步简化

    xmlns:p="http://www.springframework.org/schema/p" 
    <bean id="person5" class="yang.mybatis.test.Person" p:age="12" p:name="xiaoming" p:cars-ref="cars" p:mapCars-ref="mapCars"></bean>
  • 相关阅读:
    asp.net 页面定时跳转的小技巧
    获得 Windows phone 设备的信息
    如何自定义ToggleSwitch控件样式(转)
    云推送注意(MSDN链接)
    回顾:线程和进程的区别
    WebGL
    13种提升基于MVVM模式的WP7程序性能的方法(转)
    sample_code
    网址收藏
    Net中de日期格式
  • 原文地址:https://www.cnblogs.com/realshijing/p/7920440.html
Copyright © 2011-2022 走看看