本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

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

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean --> <!--配置bean
class: bean 的全类名,通过反射的方式在IOC 容器中创建bean ,所以要求Bean 中必须有无参数的构造器
id:标示容器中的bean,id唯一
--> <bean id="helloWord" class="com.jason.spring.beans.HelloWord">
<property name="name" value="Spring"></property> </bean> </beans>

  

2.Spring 容器

  

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

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

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

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

    ③ BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身ApplicationContext 面向使用 Spring 框架的开发者几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory

    ④无论使用何种方式, 配置文件时相同的.

  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> 子节点指定属性值

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

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

  2)构造器注入

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

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

    

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

  

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

5.属性配置细节

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

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

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

 

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

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

   4)引用其它 Bean

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

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

    

 <!--  引用其他bean-->
<bean id="person" class="com.jason.spring.beans.Person">
<property name="name" value="Jason"></property>
<property name="age" value="24"></property>
<property name="car" ref="car2"></property> <!-- <property name="car">
<ref bean="car2"/>
</property> -->
</bean>

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

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

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

 <!--  引用其他bean-->
<bean id="person" class="com.jason.spring.beans.Person">
<property name="name" value="Jason"></property>
<property name="age" value="24"></property> <!-- 内部bean, 不能被外部使用,只能在内部使用-->
<property name="car">
<bean class="com.jason.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="changan"></constructor-arg>
<constructor-arg value="20" type="double"></constructor-arg> </bean>
</property>
</bean>

    

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

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

    ⑤ 和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置:为级联属性赋值, 注意:属性需要先初始化后才可以为级联属性赋值,否则有异常,

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

</bean>

    ⑥ 集合属性(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 一样.

 <!-- 测试如何配置集合属性 -->

     <bean id="person3" class="com.jason.spring.collection.Person">
<property name="name" value="Mike"></property>
<property name="age" value="30"></property>
<property name="cars">
<list>
<ref bean="car"/>
<ref bean="car2"/>
</list>
</property>
</bean>

  

 public class Person {

     private String name;
private int age; private List<Car> cars; ///
}

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

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

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

   > 可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义

 <bean id="person4" class="com.jason.spring.collection.Person2">
<property name="name" value="jason"></property>
<property name="age" value="50"></property>
<property name="cars">
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean>
 public class Person2 {

     private String name;
private int age; private Map<String, Car> cars; ///
}

    

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

 <!-- 配置properties属性值 -->
<bean id="dataSource" class="com.jason.spring.collection.DataSource">
<property name="properties">
<!-- 使用props 和 prop 子节点来为Properties 属性赋值 -->
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.myql.jdbc.Driver</prop>
</props>
</property>
</bean>
 package com.jason.spring.collection;

 import java.util.Properties;

 public class DataSource {

     private Properties properties;

     public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} @Override
public String toString() {
return "DataSource [properties=" + properties + "]";
} }

  

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

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

  >  使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.

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

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

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

 <!-- 通过p 命名空间为bean 的属性赋值,首先导入p命名空间,简化配置方式 -->
<bean id="person6" class="com.jason.spring.collection.Person" p:age="30" p:name="jason" p:cars-ref="cars"></bean>

[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间的更多相关文章

  1. [原创]java WEB学习笔记95:Hibernate 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. [原创]java WEB学习笔记86:Hibernate学习之路-- -映射 n-n 关系,单向n-n,双向n-n

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. 学习笔记:CentOS7学习之二十:shell脚本的基础

    目录 学习笔记:CentOS7学习之二十:shell脚本的基础 20.1 shell 基本语法 20.1.1 什么是shell? 20.1.2 编程语言分类 20.1.3 什么是shell脚本 20. ...

  6. 学习笔记:CentOS7学习之二十五:shell中色彩处理和awk使用技巧

    目录 学习笔记:CentOS7学习之二十五:shell中色彩处理和awk使用技巧 25.1 Shell中的色彩处理 25.2 awk基本应用 25.2.1 概念 25.2.2实例演示 25.3 awk ...

  7. 学习笔记:CentOS7学习之二十四:expect-正则表达式-sed-cut的使用

    目录 学习笔记:CentOS7学习之二十四:expect-正则表达式-sed-cut的使用 24.1 expect实现无交互登录 24.1.1 安装和使用expect 24.2 正则表达式的使用 24 ...

  8. 学习笔记:CentOS7学习之二十三: 跳出循环-shift参数左移-函数的使用

    目录 学习笔记:CentOS7学习之二十三: 跳出循环-shift参数左移-函数的使用 23.1 跳出循环 23.1.1 break和continue 23.2 Shift参数左移指令 23.3 函数 ...

  9. 学习笔记:CentOS7学习之十九:Linux网络管理技术

    目录 学习笔记:CentOS7学习之十九:Linux网络管理技术 本文用于记录学习体会.心得,兼做笔记使用,方便以后复习总结.内容基本完全参考学神教育教材,图片大多取材自学神教育资料,在此非常感谢MK ...

  10. 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用

    目录 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用 16.1 LVM的工作原理 16.1.1 LVM常用术语 16.1.2 LVM优点 16.2 创建LVM的基本步骤 16.2 ...

随机推荐

  1. Unity5.x版本AssetBundle加载研究

    之前说了 “Unity5.x版本AssetBundle打包研究”,没看过的请先看一下:http://www.shihuanjue.com/?p=57 再来看本文,有一定的连接性. 先梳理一下思路: 要 ...

  2. BZOJ1391: [Ceoi2008]order

    Description 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成. 现在给出这些参数,求最大利润 Inpu ...

  3. iOS 开发小结

    一,经历 1> 在编写以前有过的类似的新功能时,如果以前的开发人员没有写明明确的注释和开发需求,一定要仔细阅读所有代码,每一句代码都有它存在的意义. 2> 例如,只以为是[self.ful ...

  4. Ohana Cleans Up

    Ohana Cleans Up Description Ohana Matsumae is trying to clean a room, which is divided up into an n  ...

  5. Android使用Fragment定义弹出数字键盘

    fragment主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...

  6. odoo XMLRPC 新库 OdooRPC 尝鲜

    无意中发现了python居然有了OdoRPC的库,惊喜之下赶紧尝试一番,比XMLRPC简洁了不少,机制看样子是利用的JsonRPC. #原文出自KevinKong的博客http://www.cnblo ...

  7. HTML--JS练习小游戏(别踩白块儿)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 导入excle数据将excle数据插入到数据库

    实现功能是,用户可以直接导入对应数据,或者用户下载模板,填写数据,导入模板数据.easyui实现 前台页面 { text : '日清导入', iconCls : 'icon-print', handl ...

  9. Spring中的单例一二

    Spring框架很好的帮助我们创建和管理dao.bean.service.action等对象, 但是它创建的对象是单例呢还是多例,又有哪些区别以及为什么 1.在Spring中默认创建的是单例模式,简单 ...

  10. 上传读取Excel文件数据

    /// <summary> /// 上传读取Excel文件数据 /// 来自http://www.cnblogs.com/cielwater /// </summary> // ...