[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),

本博客的目的:①总结本身的学习过程,至关于学习笔记 ②将本身的经验分享给你们,相互学习,互相交流,不可商用

内容不免出现问题,欢迎指正,交流,探讨,能够留言,也能够经过如下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------java

1.在 Spring 的 IOC 容器里配置 Beanmysql

   1)在 xml 文件中经过 bean 节点来配置 beanspring

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6 
 7     <!-- 配置bean -->
 8     
 9     <!--配置bean 10  class: bean 的全类名,经过反射的方式在IOC 容器中建立bean ,因此要求Bean 中必须有无参数的构造器 11  id:标示容器中的bean,id惟一 12      -->
13     
14     <bean id="helloWord" class="com.jason.spring.beans.HelloWord">
15         <property name="name" value="Spring"></property>
16         
17     </bean>
18 
19 </beans>

 

  

 

2.Spring 容器sql

  

  1)在 Spring IOC 容器读取 Bean 配置建立 Bean 实例以前, 必须对它进行实例化. 只有在容器实例化后, 才能够从 IOC 容器里获取 Bean 实例并使用.数组

  2)Spring 提供了两种类型的 IOC 容器实现. 框架

    ① BeanFactory: IOC 容器的基本实现.ide

    ② ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口学习

    ③ BeanFactory 是 Spring 框架的基础设施,面向 Spring 自己ApplicationContext 面向使用 Spring 框架的开发者几乎全部的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory测试

    ④不管使用何种方式, 配置文件时相同的.this

  3)ApplicationContext 接口

    ①  ApplicationContext 的主要实现类:

      ClassPathXmlApplicationContext:从 类路径下加载配置文件

      > FileSystemXmlApplicationContext: 从文件系统中加载配置文件

    ②  ConfigurableApplicationContext 扩展于 ApplicationContext,新增长两个主要方法:refresh() 和 close(), 让 ApplicationContext 具备启动、刷新和关闭上下文的能力

     ③  ApplicationContext 在初始化上下文时就实例化全部单例的 Bean

     ④  WebApplicationContext 是专门为 WEB 应用而准备的,它容许从相对于 WEB 根目录的路径中完成初始化工做

 

3.从 IOC 容器中获取 Bean(ApplicationContext API)

   

  1)getBean(String) :经过bean中的id 获取容器中的一个实例  推荐

  2)getBean(Class<T>):经过实例的类型从容器中获取一个实例

 

4.依赖注入的方式(属性注入方式)

  1)属性注入

    ① 属性注入即经过 setter 方法注入Bean 的属性值或依赖的对象

    ② 属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值

    ③ 属性注入是实际应用中最经常使用的注入方式

1 <bean id="helloWord" class="com.jason.spring.beans.HelloWord">
2         <property name="name" value="Spring"></property>
3     </bean>

 

  2)构造器注入

    ① 经过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可使用。

    ② 构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性

    

 1 <!-- 经过构造方法来配置bean 的属性 -->
 2     <bean id="car" class="com.jason.spring.beans.Car">
 3         <constructor-arg value="Audi" index="0"></constructor-arg>
 4         <constructor-arg value="xian" index="1"></constructor-arg>
 5         <constructor-arg value="50" index="2" type="int"></constructor-arg>
 6     </bean>
 7     
 8     <!-- 使用构造器注入属性值能够指定参数的位置和参数的类型! 以区分重载的构造器 -->
 9     <bean id="car2" class="com.jason.spring.beans.Car">
10         <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
11         <constructor-arg value="xian" type="java.lang.String"></constructor-arg>
12         <constructor-arg value="50" type="double"></constructor-arg>
13     </bean>

 

 

  

  3)工厂方法注入(不多使用,不推荐)

 

5.属性配置细节

  1)字面值:可用字符串表示的值能够经过 <value> 元素标签或 value 属性进行注入

  2)基本数据类型及其封装类、String 等类型均可以采起字面值注入的方式

<constructor-arg type="double">
            <value>50</value>
        </constructor-arg>

 

 

  3)若字面值中包含特殊字符,可使用 <![CDATA[]]> 把字面值包裹起来

1 <constructor-arg type="java.lang.String">
2             <value><![CDATA[<XIAN>]]></value>
3         </constructor-arg>

 

 

   4)引用其它 Bean

    ① 组成应用程序的 Bean 常常须要相互协做以完成应用程序的功能. 要使 Bean 可以相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用

    ② 在 Bean 的配置文件中, 能够经过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用

    

 1 <!-- 引用其余bean-->
 2     <bean id="person" class="com.jason.spring.beans.Person">
 3         <property name="name" value="Jason"></property>
 4         <property name="age" value="24"></property>
 5         <property name="car" ref="car2"></property>
 6         
 7         <!-- <property name="car">  8  <ref bean="car2"/>  9  </property> -->
10     </bean>

 

 

         ③ 也能够在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

      >当 Bean 实例仅仅给一个特定的属性使用时, 能够将其声明为内部 Bean. 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不须要设置任何 id 或 name 属性

      >内部 Bean 不能使用在任何其余地方

 1 <!-- 引用其余bean-->
 2     <bean id="person" class="com.jason.spring.beans.Person">
 3         <property name="name" value="Jason"></property>
 4         <property name="age" value="24"></property>
 5     
 6         <!-- 内部bean, 不能被外部使用,只能在内部使用-->
 7         <property name="car">
 8             <bean class="com.jason.spring.beans.Car">
 9                 <constructor-arg value="Ford"></constructor-arg>
10                 <constructor-arg value="changan"></constructor-arg>
11                 <constructor-arg value="20" type="double"></constructor-arg>
12  
13             </bean>
14         </property>
15     </bean>

     

    ④ 可使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值

1 <bean id="person2" class="com.jason.spring.beans.Person">
2         <constructor-arg value="jason"></constructor-arg>
3         <constructor-arg value="25"></constructor-arg>
4       <constructor-arg><null/></constructor-arg>
5     </bean>

 

 

    ⑤ 和 Struts、Hiberante 等框架同样,Spring 支持级联属性的配置:为级联属性赋值, 注意:属性须要先初始化后才能够为级联属性赋值,不然有异常,

1 <bean id="person2" class="com.jason.spring.beans.Person">
2         <constructor-arg value="jason"></constructor-arg>
3         <constructor-arg value="25"></constructor-arg>
4         <constructor-arg ref="car"></constructor-arg> 5 <property name="car.brand" value="xxxxxx"></property>
6     </bean>
7     

 

 

    ⑥ 集合属性(list,map,properties)

    >在 Spring中能够经过一组内置的 xml 标签(例如: <list>, <set><map>) 来配置集合属性.

    >配置 java.util.List 类型的属性, 须要指定 <list> 标签, 在标签里包含一些元素. 这些标签能够经过 <value> 指定简单的常量值,

         经过 <ref> 指定对其余 Bean 的引用. 经过<bean> 指定内置 Bean 定义. 经过 <null/> 指定空元素. 甚至能够内嵌其余集合.

    >数组的定义和 List 同样, 都使用 <list>

    >配置 java.util.Set 须要使用 <set> 标签, 定义元素的方法与 List 同样.

 

 

 1 <!-- 测试如何配置集合属性 -->
 2     
 3     <bean id="person3" class="com.jason.spring.collection.Person">
 4         <property name="name" value="Mike"></property>
 5         <property name="age" value="30"></property>
 6         <property name="cars">
 7             <list>
 8                 <ref bean="car"/>
 9                 <ref bean="car2"/>
10             </list>
11         </property>
12     </bean>

 

 

 

      

1 public class Person { 2 
3     private String name; 4     private int age; 5 
6     private List<Car> cars; 7 
8 ///
9 }

 

 

 

   > Java.util.Map 经过 <map> 标签订义, <map> 标签里可使用多个 <entry> 做为子标签. 每一个条目包含一个键和一个值

   > 必须在 <key> 标签里定义

 

   > 由于键和值的类型没有限制, 因此能够自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素.

   > 能够将 Map 的键和值做为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用经过 key-ref 和 value-ref 属性定义

 1 <bean id="person4" class="com.jason.spring.collection.Person2">
 2         <property name="name" value="jason"></property>
 3         <property name="age" value="50"></property>
 4         <property name="cars">
 5             <map>
 6                 <entry key="AA" value-ref="car"></entry>
 7                 <entry key="BB" value-ref="car2"></entry>
 8             </map>
 9         </property>
10     </bean>

 

1 public class Person2 { 2 
3     private String name; 4     private int age; 5 
6     private Map<String, Car> cars; 7 
8 ///
9 }

 

    

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

 1 <!-- 配置properties属性值 -->
 2     <bean id="dataSource" class="com.jason.spring.collection.DataSource">
 3         <property name="properties">
 4         <!-- 使用props 和 prop 子节点来为Properties 属性赋值 -->
 5             <props>
 6                 <prop key="user">root</prop>
 7                 <prop key="password">1234</prop>
 8                 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
 9                 <prop key="driverClass">com.myql.jdbc.Driver</prop>
10             </props>
11         </property>
12     </bean>

 

 1 package com.jason.spring.collection;  2 
 3 import java.util.Properties;  4 
 5 public class DataSource {  6 
 7     private Properties properties;  8 
 9     public Properties getProperties() { 10         return properties; 11  } 12 
13     public void setProperties(Properties properties) { 14         this.properties = properties; 15  } 16 
17  @Override 18     public String toString() { 19         return "DataSource [properties=" + properties + "]"; 20  } 21     
22     
23 }

 

   

  注意:当多个对象同时引用同一个集合的时候。如何让集合提升重用性?

  解决方法:使用 utility scheme 定义集合

  >  使用基本的集合标签订义集合时, 不能将集合做为独立的 Bean 定义, 致使其余 Bean 没法引用该集合, 因此没法在不一样 Bean 之间共享集合.

  >  可使用 util schema 里的集合标签订义独立的集合 Bean. 须要注意的是, 必须在 <beans> 根元素里添加 util schema 定义

 1 <!-- 配置单例的集合bean,以供多个bean 进行引用,须要导入util 命名空间 -->
 2     <util:list id="cars">
 3         <ref bean="car"/>
 4         <ref bean="car2"/>
 5     </util:list>
 6     
 7     <bean id="person5" class="com.jason.spring.collection.Person">
 8         <property name="name" value="jason"></property>
 9         <property name="age" value="30"></property>
10         <property name="cars" ref="cars"></property>
11     </bean>

 

 

 

  ⑦ 经过p命名空间,简化为bean的属性的赋值过程

 

1 <!-- 经过p 命名空间为bean 的属性赋值,首先导入p命名空间,简化配置方式 -->
2     <bean id="person6" class="com.jason.spring.collection.Person" p:age="30" p:name="jason" p:cars-ref="cars"></bean>
相关文章
相关标签/搜索