<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--    把对象的创建交给spring来管理--><!--    spring对bean的管理细节1.创建bean的三种方式2.bean对象的作用范围3.bean对象的生命周期--><!--    创建bean的三种方式--><!--    第一种方式:使用默认构造函数创建在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。采用的就是默认构造函数创建对象,此时如果没有默认构造函数,则对象无法创建<bean id="accountService" class="com.ithema.service.impl.AccountServiceImpl"></bean>--><!--    第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)<bean id="instanceFactory" class="com.ithema.factory.InstanceFactory"></bean><bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>--><!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)<bean id="accountService" class="com.ithema.factory.StaticFactory" factory-method="getAccountService"></bean>--><!--    bean的作用范围调整bean标签的scope属性:作用:用于指定bean的作用范围取值:  常用的单例的和多例的singleton:单例的(默认值)prototype: 多例的request: 作用于web应用的请求范围session: 作用于web应用的会话范围global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,就是session<bean id="accountService" class="com.ithema.service.impl.AccountServiceImpl" scope="singleton"></bean>--><!--    bean的生命周期单例对象:出生:当容器创建时对象出生活着:当容器还在,对象一直活着死亡:容器销毁,对象消亡总结:单例对象的生命周期和容器相同多例对象:出生:当我们使用对象时spring框架为我们创建活着:对象只要是在使用过程中就一直活着。死亡:当对象长时间不用,且没有别的对象引用时,由java的垃圾回收机制回收--><bean id="accountService" class="com.ithema.service.impl.AccountServiceImpl" scope="singleton"></bean>
</beans>

InstanceFactory.java

package com.ithema.factory;import com.ithema.service.IAccountService;
import com.ithema.service.impl.AccountServiceImpl;/*** 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式提供默认构造函数)*/public class InstanceFactory {public IAccountService getAccountService(){return new AccountServiceImpl();}
}

StaticFactory.java

package com.ithema.factory;import com.ithema.service.IAccountService;
import com.ithema.service.impl.AccountServiceImpl;/*** 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式提供默认构造函数)*/public class StaticFactory {public static IAccountService getAccountService() {return new AccountServiceImpl();}
}

AccountServiceImpl.java

package com.ithema.service.impl;import com.ithema.service.IAccountService;/*** 账户的业务层实现类*/
public class AccountServiceImpl implements IAccountService {public AccountServiceImpl() {System.out.println("对象创建了");}public void saveAccount() {System.out.println("service中的saveAccount执行了");}
}

Spring xml文件配置——创建bean的三种方式及作用范围、生命周期相关推荐

  1. 把对象的创建交给spring来管理——  1.创建bean的三种方式     2.bean对象的作用范围     3.bean对象的生命周期

    把对象的创建交给spring来管理 spring对bean的管理细节     1.创建bean的三种方式     2.bean对象的作用范围     3.bean对象的生命周期 创建Bean的三种方式 ...

  2. java bean 工厂模式_Spring框架通过工厂创建Bean的三种方式实现

    工厂模式 Spring中bean的创建,默认是框架利用反射new出来的bean实例.有时候也会有一些复杂的情况. 假设有一个飞机,属性如下,现在需要造很多同型号的飞机,那么唯一需要改变的属性只有Dri ...

  3. Spring创建Bean的三种方式的使用和区别

    在学习Spring的时候,发现Spring的IOC(控制反转)为我们提供的三种创建Bean的方式. 1.Spring创建Bean的三种方式 这里采用XML配置,分别演示三种创建Bean的方式和代码. ...

  4. Spring 创建Bean的三种方式

    创建Bean的三种方式 第一种方式:使用默认构造函数创建. 在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时.采用的就是默认构造函数创建bean对象,此 ...

  5. 五、创建Bean的三种方式

    五.创建Bean的三种方式 转载于:https://www.cnblogs.com/ljiwej/p/7280614.html

  6. Spring创建Bean的3种方式

    1. Spring创建Bean的三种方式 1. 调用构造器(因为常用就不讲) 2. 调用静态工厂方法 3. 调用实例工厂方法 2. 使用静态工厂方法 1.此时<bean.../>元素要指定 ...

  7. 【Spring杂烩】探讨Spring向容器注册Bean的三种方式

    探讨Spring向容器注册Bean的三种方式 重点了解@Import实现的三种子方式 前提概要 Spring向容器注册Bean的三种方式 通过@ComponentScan.@Componet 通过@B ...

  8. Spring 使用@Import注入Bean的三种方式

    一.准备工作 1.导入spring-context依赖 <dependency><groupId>org.springframework</groupId>< ...

  9. Spring注解创建Bean的几种方式

    Spring注解创建Bean的几种方式 1.@Component系列 @Component @Service @Repository @Controller @Configuration 2. 依附于 ...

  10. Spring读取配置文件,获取bean的几种方式

    Spring读取配置文件,获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemX ...

最新文章

  1. 国产AI全面崛起!盘点11款2019年热门国产AI芯片
  2. 【Linux 内核】线程调度示例一 ② ( 获取指定调度策略的最大和最小优先级 | 代码示例 )
  3. EditText的 焦点事件 setOnFocusChangeListener
  4. SpringBoot 集成 Nacos
  5. 二、scrapy爬虫框架——scrapy构造并发送请求
  6. axi时序图_深入 AXI4总线(E3)实战:制作一个 AXI 接口 IP
  7. G - Hard problem CodeForces - 706C DP
  8. PHP APM 对比评测:OneAPM, New Relic, 听云
  9. 《java程序员全攻略:从小工到专家》连载一:外行人眼中的IT人
  10. VXLAN配置实例(二)——VXLAN跨子网互通
  11. 数学模型——人口增长模型(基于python)
  12. php怎么seo,怎样学习seo
  13. Flink CheckpointCoordinator 步骤 流程 源码
  14. vue 项目内vue指令常用
  15. App推广:ASO策略篇,清榜频发,优化热情不减
  16. 威联通nas怎么更换大硬盘_QNAP 篇一:记一次换硬盘引发的折腾
  17. 爱奇艺本地实时Cache方案
  18. 常见的SQL注入类型
  19. 大盘是超跌反弹还是彻底反转?
  20. Python数据展示 - 生成表格图片

热门文章

  1. Html页面增加返回顶部图标和隐藏出现
  2. react实现聊天界面_React-Redux 100行代码简易版探究原理
  3. STC学习:导航按键
  4. syslog日志转换器_图解将windows日志转成syslog格式并发送
  5. linux 占用缓存前10_Linux查看内存使用情况应该使用什么命令
  6. jdbc c3p0 mysql_JDBC+C3P0+DBCP 基本使用
  7. 遗传算法之:地图着色
  8. Jupyter Notebook从入门到精通
  9. linux虚拟内存和win,linux下的vm(虚拟内存)和windows下的作用是一样的,均是防止真实内存资源不足准备的. linux的vm相关参数...
  10. Python数据结构,线性结构:栈、队列、双端队列、列表