spring bean的三种管理方式·
1.无参构造函数
1.xml文件配置内容
<!--无参构造函数-->
<bean id="bean1" class="com.imooc.ioc.demo2.Bean1"></bean>
2.无参的构造函数
public class Bean1 {
public Bean1(){
System.out.println("bean1被实例化");
}
}
3.测试方法
public void demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean1 bean1=(Bean1)applicationContext.getBean("bean1");
}
//会输出结果: bean1被实例化
2.静态工厂方法
1.Bean方法
public class Bean2 { } 2.静态工厂方法 public class Bean2Factory { public static Bean2 createBean2(){
System.out.println("已经执行");
return new Bean2();
}
} 3.测试方法
public void demo2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean2 bean2=(Bean2)applicationContext.getBean("bean2");
}
4.配置文件
<bean id="bean2" class="com.imooc.ioc.demo2.Bean2Factory" factory-method="createBean2"></bean>
3.实例化工厂
1.Bean方法
public class Bean3 { } 2.Bean工厂方法
public class Bean3Factory {
public Bean3 createBean3(){
System.out.println("实例工厂");
return new Bean3();
} } 3.测试方法 public void demo3(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean3 bean3=(Bean3)applicationContext.getBean("bean3");
}
4.配置文件
<bean id="bean3Factory" class="com.imooc.ioc.demo2.Bean3Factory"></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"></bean>
//先配置工厂类的bean 再调用
实例化工厂和静态工厂的区别: 静态工厂必须提供静态方法
spring bean的三种管理方式·的更多相关文章
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- spring Bean的三种注入方式
1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...
- Spring三 Bean的三种创建方式
创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
- Bean的三种实例化方式
在面向对象程序中,如要使用某个对象,就需要先实例化这个对象.同样的,在Spring中,要想使用容器中的Bean,也需要实例化Bean.实例化Bean有三种方式,分别是:构造器实例化.静态工厂实例化.实 ...
- Java开发学习(四)----bean的三种实例化方式
一.环境准备 准备开发环境 创建一个Maven项目 pom.xml添加依赖 resources下添加spring的配置文件applicationContext.xml 最终项目的结构如下: 二. ...
- Spring IOC 中三种注入方式
项目错误知识点记录 正文 最近在项目的时候,用到Spring框架,Spring框架提供了一种IOC的自动注入功能,可以很轻松的帮助我们创建一个Bean,这样就省的我们四处写new Object()这样 ...
- Spring boot 集成三种拦截方式
三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...
- Spring boot 集成三种定时任务方式
三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...
随机推荐
- WebGIS之MapBox篇
前面在Arcgis的基础上玩了玩,这不最近又去摸索了一下Web上开源的GIS;这次选择了基于MapBox来实现一些效果: 1.加载自己发布的本地瓦片效果 2.加载热力图.Echarts.三位建筑.路况 ...
- win7搭建本地SonarQube环境进行c#代码分析
1.SonarQube需要正常运行,首先需要安装Java环境,我这里安装的是jdk-8u181版本,可以在下面网站找适的版本去下载安装 https://www.oracle.com/technetwo ...
- [Tomcat源码分析] Eclipse中搭建Apache Tomcat源码调试环境
网上很多文章都推荐使用Ant下载编译,但本地实践中屡屡失败,无法下载. 后来参考 https://blog.csdn.net/xiongyouqiang/article/details/7894107 ...
- 手写instanceof (详解原型链) 和 实现绑定解绑和派发的事件类
A instanceof B 是判断 A 是否继承自B,是返回true, 否返回false 再精确点就是判断B 是否 再 A 的 原型链上, 什么是原型链,举个例子: 我们定 ...
- Web点击链接调起手机系统自带短信发短信
实现代码如下: 一.Html代码 <a href="javascript:;" class="xq-sms">发送短信</a> 二.jQ ...
- 读《TCP/IP详解》:TCP
TCP(Transmission Control Protocol,传输控制协议),位于传输层,提供一种面向连接.可靠的字节流服务. 字节流服务(Byte Stream Service)是指,为了方便 ...
- 4.InfluxDB-InfluxQL基础语法教程--基本select语句
本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) 基本语法如下: SE ...
- 解决IDEA springBoot读取*.properties文件中文内容乱码的问题
1. 配置 properties 文件 2. 读取 sex 属性输出到页面, 中文乱码 3. file --> settings 4. Editor --> File Encodings ...
- CSS3 边框 border-image
border-image:xx xx xx 是一系列参数的简写,该属性将图片作为边框修饰 border-image-source:url(border.png); 图片url地址 border-ima ...
- linux 的 expect 自动交互
https://www.jianshu.com/p/0194cbd70d39 https://www.cnblogs.com/saneri/p/10819348.html 参考 expect是一个自 ...