4、深入理解Bean
1. Bean 的自己主动装配(了解)2. bean 之间的关系:继承;依赖3.Bean 的作用域:能够在 <bean> 元素的 scope 属性里设置 Bean 的作用域4.使用外部属性文件5. SpEL:Spring 3.x 引入的新特性。用的不多,了解。
0). Bean:
public class Dao {
private String database;
public void setDatabase(String database) {
this.database = database;
}
public void save(){
System.out.println("save to " + database);
}
}
public class Service {
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
public Dao getDao() {
return dao;
}
public void add(){
System.out.println("add...");
dao.save();
}
}
public class Action {
private Service service = null;
public void setService(Service service) {
this.service = service;
}
public Service getService() {
return service;
}
public void execute(){
System.out.println("execute...");
service.add();
}
}
1). Spring 能够自己主动的装配 bean 的属性值。
比如,以下的 bean 没有进行属性引用的配置,其装配能够交给 Spring 的 IOC 容器
<bean id="dao" class="com.atguigu.spring.autowire.Dao">
<property name="database" value="DB2"/>
</bean>
在实际的项目中非常少使用自己主动装配功能,由于和自己主动装配功能所带来的优点比起来。明白清晰的配置文档更有说服力一些
<bean id="service" class="com.atguigu.spring.autowire.Service" />
<bean id="action" class="com.atguigu.spring.autowire.Action" />
2). 在 <bean> 的 autowire 属性里指定自己主动装配的模式
①. byType:依据类型自己主动装配
<bean id="dao" class="com.atguigu.spring.autowire.Dao">
<property name="database" value="DB2"/>
</bean>
<bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byType"/>
<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byType"/>
注意:若 IOC 容器中有多个与目标 Bean 类型一致的 Bean。Spring 将无法判定哪个 Bean 最合适该属性, 所以不能运行自己主动装配.
<bean id="service2" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
<bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byType"/>
将会抛出:NoUniqueBeanDefinitionException异常。
②. byName(依据名称自己主动装配):必须将目标 Bean 的名称和属性名设置的全然同样.
<bean id="service" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
<bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byName"/>
3). 在实际的项目中非常少使用自己主动装配功能。由于和自己主动装配功能所带来的优点比起来。明白清晰的配置文档更有说服力一些
-----------------------------------------------------------------------------------------------------------------------------------
2. Bean 之间的关系:继承。依赖
1). 继承:并非指 面向对象的继承,而是指 bean 的配置的继承。
①. 能够通过 bean 的 parent 属性继承其它 bean 的配置,在当前 bean 中能够覆盖继承的 bean 的属性
<bean id="car" class="com.atguigu.spring.relation.Car" abstract="true">
<property name="brand" value="DasAuto"></property>
<property name="corp" value="ShangHai"></property>
<property name="maxSpeed" value="200"></property>
<property name="price" value="100000"></property>
</bean>
<!-- 配置一个 car 的 bean, 要求 brand, corp 和 class 和上面的 car 的配置都同样 -->
<bean id="car2" parent="car">
<property name="corp" value="YiQi"></property>
<property name="maxSpeed" value="220"></property>
<property name="price" value="110000"></property>
</bean>
2). 若仅仅想把父 Bean 作为模板, 能够设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean
<bean id="car" class="com.atguigu.spring.relation.Car" abstract="true">
3). bean 的依赖关系:Spring 同意用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好
<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byName" depends-on="service"/>
-----------------------------------------------------------------------------------------------------------------------------------
3. Bean 的作用域:能够在 <bean> 元素的 scope 属性里设置 Bean 的作用域.
1). 默认情况下, Spring 仅仅为每一个在 IOC 容器里声明的 Bean 创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例:
全部兴许的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton, 它是全部 Bean 的默认作用域.
2). 若把 scope 的值设置为 prototype 原型,则意为每次 getBean() 方法都会返回一个新的实例。
<bean id="car2" parent="car" scope="prototype">
<property name="corp" value="YiQi"></property>
<property name="maxSpeed" value="220"></property>
<property name="price" value="110000"></property>
</bean>
3). 注意:若 scope 为 singleton, 则 IOC 容器创建时,IOC 容器即创建 Bean 的实例;
若 scope 为 prototype。则在 IOC 容器创建时,不再创建 Bean 的实例。而是每次调用 getBean 方法时才创建 Bean 的实例
4). 内部 Bean 默认情况下也是单例的。
-----------------------------------------------------------------------------------------------------------------------------------
4. 使用外部属性文件
1). 必要性:在配置文件中配置 Bean 时, 有时须要在 Bean 的配置里混入系统部署的细节信息(比如: 文件路径, 数据源配置信息等).
而这些部署细节实际上须要和 Bean 配置相分离
2). 详细步骤:
①. 编写属性文件:在 classpath 下新建 db.properties。并键入例如以下键值对:
user=rootpassword=1230jdbcurl=jdbc:mysql:///testdriverclass=com.mysql.jdbc.Driverminsize=10maxsize=20initsize=10
②. 在 bean 的配置文件里导入 properties 文件
◆增加 context 命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
◆使用 <context:property-placeholder location=""/> 导入属性文件
<context:property-placeholder location="classpath:db.properties"/>
③. 在 bean 的配置文件里通过 ${} 来使用属性文件里的值:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
<property name="minPoolSize" value="${minsize}"></property>
<property name="maxPoolSize" value="${maxsize}"></property>
<property name="initialPoolSize" value="${initsize}"></property>
</bean>
-----------------------------------------------------------------------------------------------------------------------------------
5. SpEL:Spring 3.x 引入的新特性,用的不多,了解。
1). Spring 表达式语言(简称SpEL):是一个支持执行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用 #{…} 作为定界符,全部在大框号中的字符都将被觉得是 SpEL
2). 样例:
<bean id="car2" parent="car" scope="prototype">
<property name="corp" value="YiQi"></property>
<property name="maxSpeed" value="220"></property>
<property name="price" value="110000"></property>
</bean>
<bean id="dao2" class="com.atguigu.spring.autowire.Dao">
<property name="database" value="#{car2.toString().toLowerCase()}"></property>
</bean>
<bean id="service2" class="com.atguigu.spring.autowire.Service">
<property name="dao" value="#{dao2}"></property>
</bean>
<bean id="action2" class="com.atguigu.spring.autowire.Action">
<property name="service" value="#{service2}"></property>
</bean>
4、深入理解Bean的更多相关文章
- 【SpringBoot】SpringBoot的基础,全面理解bean的生命周期
前言 前段时间直接上手使用springboot开发了一个数据平台的后台部分,但是自身对于springboot的原理和过程还不是很清晰,所以反过来学习下springboot的基础. 大家都知道sprin ...
- 理解Spring的Bean工厂
一提到工厂,我们先来回顾前面学习过的工厂方法和抽象工厂模式: 工厂方法:针对产品维度,能够产生新的产品,也能够产生新的产品工厂,既能够扩展产品维度.可是假设我们想在普通工厂上生产产品系列,就会特别麻烦 ...
- Spring中Bean及@Bean的理解
Spring中Bean及@Bean的理解 Bean在Spring和SpringMVC中无所不在,将这个概念内化很重要,下面分享一下我的想法: 一.Bean是啥 1.Java面向对象,对象有方法和属性, ...
- Spring重点—— IOC 容器中 Bean 的生命周期
一.理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助. 二.在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理. 三.在没有添加后置处理器的情况 ...
- IoC容器装配Bean(xml配置方式)(Bean的生命周期)
1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...
- 《精通Spring 4.X企业应用开发实战》读书笔记1-1(IoC容器和Bean)
很长一段时间关注在Java Web开发的方向上,提及到Jave Web开发就绕不开Spring全家桶系列,使用面向百度,谷歌的编程方法能够完成大部分的工作.但是这种不系统的了解总觉得自己的知识有所欠缺 ...
- Spring 学习笔记---Bean的生命周期
生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...
- Spring 了解Bean的一生(生命周期)
转载 https://blog.csdn.net/w_linux/article/details/80086950 该篇博客就来了解IoC容器下Bean的一生吧,也可以理解为bean的生命周期. ## ...
- Spring中Bean生命周期
Spring中的bean生命周期是一个重要的点,只有理解Bean的生命周期,在开发中会对你理解代码是非常有用的.对于Bean的周期,个人认为可以分为四个阶段.第一阶段:Bean的实例化,在该阶段主要是 ...
随机推荐
- android录音功能的实现
这个录音实现是我在Bus上看到并下载的,他那个源码不完整,再次把我整理完整的代码贴出,源码地址在这:http://download.csdn.net/detail/chaozhung/5618649 ...
- sql: oracle, for update和for update nowait的区别
1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/276222 ...
- 1352 - Colored Cubes (枚举方法)
There are several colored cubes. All of them are of the same size but they may be colored differentl ...
- Qt for Android 部署流程分析
原地址:http://blog.csdn.net/foruok/article/details/17796017 今天为了测试使用 Qt Creator 3.0.0 开发的纯 C 工程,利用了 Win ...
- MyBatis深入理解一
Mybatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...
- 基于visual Studio2013解决C语言竞赛题之1087数字变换
题目 解决代码及点评 /************************************************************************/ /* ...
- 基于visual Studio2013解决C语言竞赛题之1077大数相加
题目 解决代码及点评 /************************************************************************/ /* ...
- 浅谈数据库技术,磁盘冗余阵列,IP分配,ECC内存,ADO,DAO,JDBC
整理-----数据库技术,磁盘冗余阵列,IP分配, ECC内存,ADO, DAO,JDBC 1.MySQL MySQL是最受欢迎的开源SQL数据库管理系统,它由 MySQL AB开发.发布和支持.My ...
- Android调用系统相机、自己定义相机、处理大图片
Android调用系统相机和自己定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,而且因为涉及到要把拍到的照片显示出来,该样例也会涉及到Android载入大图片时候的处 ...
- App 运营 推广相关
基本要素 1.定位和产品 2.取个好名字,一目了然+下载冲动 3.设计一个好图标,有感性和直观的认识 4.做好产品的说明.关键字,截图(前1-2行是重点) 5.做市场的排名(相关因素如下) (1) ...