Spring学习笔记——(2)Spring中的Bean(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shaohe18362202126/article/details/81055149

一、IOC与DI

1、IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式

2、DI(Dependency Injection) — IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接

3、IOC(Inversion of Control)控制反转,表示原本由开发人员对JavaBean的生命周期管理以及组件与组件的装配(组件间的依赖关系)控制权转交给Spring容器来完成,称为IOC

IOC的实现通过DI(Dependency Inversion)来实现

二、Spring配置Bean

1、HelloWord:

javaBean:

xml配置:

Main启动:

2、Spring IOC容器

(1)Spring提供两种类型的IOC容器:

  • BeanFacFactory:IOC容器的基本实现。
  • ApplicationContext:提供更多的高级特性,是BeanFactory的子接口。

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

(2)ApplicationContext

实现类结构图:(Ctrl+T)

  • ClassPathXmlApplicationContext:从 类路径下加载配置文件
  • FileSystemXmlApplicationContext: 从文件系统中加载配置文件
  • ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力
  • WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作

(3)ApplicationContext的getBean方法

// 利用id定位到IOC容器中的bean
HelloWorld hello = (HelloWorld) ctx.getBean("helloWorld");
// 利用类型返回IOC容器中 的Bean,但要求IOC容器中必须只配置一个该类型的Bean
HelloWorld hello = ctx.getBean(HelloWorld.class);

3、依赖注入方式

(1)属性注入

  • 属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
  • 属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值 

(2)构造方法注入

  • 通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
  • 构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性

按索引匹配入参

按类型匹配入参

*字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入。
基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。使用<value>子节点配置

<constructor-arg >
    <value><![CDATA[<特殊字符为尖括号>]]></value>
</constructor-arg>

(3)工厂方法注入

暂无

4、注入属性的细节

(1)引用其他的bean

  • 通过 <ref> 元素或 ref  属性

  • Spring 支持级联属性的配置
<bean id="action" class="com.atguigu.spring.ref.Action">
    <property name="service" ref="service2"></property>
	<!-- 设置级联属性 ,service属性要先进行注入(上一行)-->
	<property name="service.dao.dataSource" value="DBCP2"></property>
</bean>

(2)集合属性

(1)使用<list>、<set>、<map>标签配置集合属性

<!-- 配置list属性 -->
<bean id="user" class="com.atguigu.spring.helloworld.User">
	<property name="userName" value="Jack"></property>
	<property name="cars">
		<!-- 使用 list 元素来装配集合属性 -->
		<list>
			<ref bean="car"/>
			<ref bean="car2"/>
		</list>
	</property>
</bean>
<!-- 配置map属性-->
<bean id="mapPerson" class="com.shaohe.spring.beans.MapPerson">
	<property name="name" value="mapPerson"></property>
	<property name="carMap">
		<map>
			<entry key="car1" value-ref="car"></entry>
			<entry key="car2" value-ref="car2"></entry>
		</map>
	</property>
</bean>

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

<!-- 配置Properties属性 -->
<bean id="dataSource" class="com.shaohe.spring.beans.DataSource">
	<property name="properties">
		<props>
			<prop key="user">root</prop>
			<prop key="password">root</prop>
			<prop key="jdbcURL">jdbc:mysql//mysql</prop>
			<prop key="driverClass">com.mysql.jdbc.Driver</prop>
		</props>
	</property>
</bean>

(3)使用 utility scheme 定义集合,将集合作为独立的 Bean 定义, 使其他 Bean可以引用该集合, 可以在不同 Bean 之间共享集合

<!--配置单例集合bean,方便其他bean引用。需要导入util命名空间 -->
<util:list id="cars">
	<ref bean="car"/>
	<ref bean="car2"/>
</util:list>

<bean id="user2" class="com.atguigu.spring.helloworld.User">
	<property name="userName" value="Rose"></property>
	<!-- 引用外部声明的 list -->
	<property name="cars" ref="cars"></property>
</bean>

(4)使用p命名空间

<bean id="user3" class="com.atguigu.spring.helloworld.User"
		p:cars-ref="cars" p:userName="Titannic"></bean>

(3)自动装配bean的属性

  1. byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配
  2. byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同
  3. constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用

JavaBean结构:

自动装配

<!-- 自动装配 
		byType: 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配.
		byName: 若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配
		在使用 XML 配置时, 自动转配用的不多. 但在基于 注解 的配置时, 自动装配使用的较多
	-->
<bean id="carAuto" class="com.shaohe.spring.beans.autowire.CarAuto"
		p:brand="baoMa" p:name="baomaX5"></bean>
<bean id="addressAuto" class="com.shaohe.spring.beans.autowire.AddressAuto"
	 p:address="上海"></bean>
<!-- 自动装配carAuto、addressAuto属性 -->
<bean id="personAuto" class="com.shaohe.spring.beans.autowire.PersonAuto"
	 autowire="byName"></bean>
  • 在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了. 
  • autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.
  • 一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些

5、bean之间的关系

(1)继承

  • 子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置
  • 子 Bean 也可以覆盖从父 Bean 继承过来的配置
  • 父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean
  • 并不是 <bean> 元素里的所有属性都会被继承. 比如: autowire, abstract 等.
  • 也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true
<!-- 继承,使用 parent 来完成继承  -->
<bean id="carAuto2" class="com.shaohe.spring.beans.autowire.CarAuto"
	  parent="carAuto"></bean>

<!-- 抽象的bean,属性abstract为true,不被实例化,只用来作为模板继承。class属性可以忽略 -->
<bean id="carAutoAbstract" class="com.shaohe.spring.beans.autowire.CarAuto"
		p:brand="baoMa" p:name="baomaX5" abstract="true"></bean>

(2)依赖

  • Spring 允许用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好
  • 如果前置依赖于多个 Bean,则可以通过逗号,空格或的方式配置 Bean 的名称
<bean id="carAuto" class="com.shaohe.spring.beans.autowire.CarAuto"
		p:brand="baoMa" p:name="baomaX5"></bean>

<!--personAuto2依赖于carAuto -->
<bean id="personAuto2" class="com.shaohe.spring.beans.autowire.PersonAuto"
		depends-on="carAuto">
</bean>

6、Bean的作用域

默认情况下 bean 是单例的

<!-- 
prototype: 原型的. 每次调用 getBean 方法都会返回一个新的 bean. 且在第一次调用 getBean 方法时才创建实例
singleton: 单例的. 每次调用 getBean 方法都会返回同一个 bean. 且在 IOC 容器初始化时即创建 bean 的实例. 默认值
-->
<bean id="dao2" class="com.atguigu.spring.ref.Dao" scope="prototype"></bean>

7、使用外部属性文件

Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量

db.properties文件内容

jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test

jdbc.initPoolSize=5
jdbc.maxPoolSize=10

配置文件中引用:

  • <beans> 中添加 context Schema 定义
  • Spring 2.5 之后: 通过 <context:property-placeholder>导入外部的资源文件
<!-- 导入外部的资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
	
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${jdbc.user}"></property>
	<property name="password" value="${jdbc.password}"></property>
	<property name="driverClass" value="${jdbc.driverClass}"></property>
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	
	<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

8、Spring表达式语言:SpEL

  • 语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
  • SpEL 为 bean 的属性进行动态赋值提供了便利
  • 通过 SpEL 可以实现:通过 bean 的 id 对 bean 进行引用、调用方法以及引用对象中的属性、计算表达式的值、正则表达式的匹配

(1)字面量

  1. 整数:<property name="count" value="#{5}"/>
  2. 小数:<property name="frequency" value="#{89.7}"/>
  3. 科学计数法:<property name="capacity" value="#{1e4}"/>
  4. String:可以使用单引号或者双引号作为字符串的定界符号:<property name=“name” value="#{'Chuck'}"/> 或 <property name='name' value='#{"Chuck"}'/>
  5. Boolean:<property name="enabled" value="#{false}"/>

(2)引用 Bean、属性和方法

  • 引用其他对象:

  • 引用其他对象的属性

  • 调用其他方法,还可以链式操作

  • 调用静态方法或静态属性:通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性: 

(3)支持的运算符号

  • 算数运算符:+, -, *, /, %, ^

  • 加号还可以用作字符串连接

  • 比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge

  • 逻辑运算符号: and, or, not, |

  • if-else 运算符:?: (ternary), ?: (Elvis)

  • if-else 的变体

  • 正则表达式:matches

*接下篇:Spring学习笔记——(3)Spring中的Bean(二)

猜你喜欢

转载自blog.csdn.net/shaohe18362202126/article/details/81055149