首页 / JAVA / [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9788字,纯文字阅读大概需要14分钟。
内容图文
![[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间](/upload/InfoBanner/zyjiaocheng/1119/1b212576b34d4f55ac1aba5fe8abbd3d.jpg)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.在 Spring 的 IOC 容器里配置 Bean
1)在 xml 文件中通过 bean 节点来配置 bean
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 容器
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> 子节点指定属性值
③ 属性注入是实际应用中最常用的注入方式
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;
78///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;
78///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 }
1617 @Override
18public String toString() {
19return "DataSource [properties=" + properties + "]";
20 }
212223 }
注意:当多个对象同时引用同一个集合的时候。如何让集合提高重用性?
解决方法:使用 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
>
原文:http://www.cnblogs.com/jasonHome/p/5950837.html
内容总结
以上是互联网集市为您收集整理的[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间全部内容,希望文章能够帮你解决[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。