Spring创建Bean的三种方式及Bean的生命周期
Spring创建Bean的三种方式及Bean的生命周期
Spring创建Bean的三种方式
第一种方式:使用默认构造函数创建
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
Spring配置文件
<bean id="accountServiceOne" class="com.zjw.service.impl.AccountServiceOneImpl" />
Java类
package com.zjw.service.impl;
import com.zjw.service.IAccountServiceOne;
/**
* 账户的业务层实现类
* 对象创建的三种方式一:通过构造方法创建对象
*/
public class AccountServiceOneImpl implements IAccountServiceOne {
public AccountServiceOneImpl() {
System.out.println("AccountServiceOneImpl……我创建了。。");
}
@Override
public void saveAccount() {
System.out.println("AccountServiceOneImpl中的saveAccount方法执行了");
}
}
测试
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//对象创建的三种方式一:通过构造方法创建对象
IAccountServiceOne accountServiceOne = (IAccountServiceOne) ac.getBean("accountServiceOne");
System.out.println(accountServiceOne);
accountServiceOne.saveAccount();
System.out.println("main 方法结束了。。。。");
}
}
结果
AccountServiceOneImpl……我创建了。。。
com.zjw.service.impl.AccountServiceOneImpl@258e2e41
AccountServiceOneImpl中的saveAccount方法执行了
main 方法结束了。。。。
第二种方式:使用普通工厂中的方法创建对象
使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
Spring配置
<!--第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)-->
<bean id="instanceFactory" class="com.zjw.factory.InstanceFactory"/>
<bean id="accountServiceTwo" factory-bean="instanceFactory" factory-method="getAccountService" />
工厂类
/**
* @author zjw
*/
public class InstanceFactory {
public IAccountServiceTwo getAccountService(){
return new AccountServiceTwoImpl();
}
}
Java类
/**
* 账户的业务层实现类
* 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
*/
public class AccountServiceTwoImpl implements IAccountServiceTwo {
public AccountServiceTwoImpl() {
System.out.println("AccountServiceTwoImpl……我创建了。。");
}
@Override
public void saveAccount() {
System.out.println("AccountServiceTwoImpl中的saveAccount方法执行了");
}
}
测试
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//第二种方式:使用普通工厂中的方法创建对象
IAccountServiceTwo accountServiceTwo = ac.getBean("accountServiceTwo", IAccountServiceTwo.class);
System.out.println(accountServiceTwo);
accountServiceTwo.saveAccount();
System.out.println("main 方法结束了。。。。");
}
}
结果
AccountServiceTwoImpl……我创建了。。
com.zjw.service.impl.AccountServiceTwoImpl@258e2e41
AccountServiceTwoImpl中的saveAccount方法执行了
main 方法结束了。。。。
第三种方式:使用工厂中的静态方法创建对象
Spring配置
<!--第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring)-->
<bean id="accountServiceThree" class="com.zjw.factory.StaticFactory" factory-method="getAccountService" />
工厂类
public class StaticFactory {
public static IAccountServiceThree getAccountService(){
return new AccountServiceThreeImpl();
}
}
Java类
/**
* 账户的业务层实现类
* 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring)
*/
public class AccountServiceThreeImpl implements IAccountServiceThree {
public AccountServiceThreeImpl() {
System.out.println("AccountServiceThreeImpl……我创建了。。");
}
@Override
public void saveAccount() {
System.out.println("AccountServiceThreeImpl中的saveAccount方法执行了");
}
}
测试
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//第二种方式:使用工厂中的静态方法创建对象
IAccountServiceThree accountServiceThree= ac.getBean("accountServiceThree", IAccountServiceThree.class);
System.out.println(accountServiceThree);
accountServiceThree.saveAccount();
System.out.println("main 方法结束了。。。。");
}
}
结果
AccountServiceThreeImpl……我创建了。。
com.zjw.service.impl.AccountServiceThreeImpl@258e2e41
AccountServiceThreeImpl中的saveAccount方法执行了
main 方法结束了。。。。
Bean的生命周期
- 单例对象
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象给你消亡
总结:单例对象的生命周期和容器相同
- 多例对象
出生:当我们使用对象时spring框架为我们创建
活着:对象只要是在使用过程中就一直活着
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
Spring配置
<!--
init-method:对象创建后执行的方法
destroy-method:对象销毁时执行的方法
单例对象,容器关闭后执行
多例模式,java虚拟机决定
scope:单例还是多例对象
singleton:默认,单例
prototype:多例
-->
<bean id="accountService" class="com.zjw.service.impl.AccountServiceImpl"
init-method="init" destroy-method="destroy" />
Java类
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl() {
System.out.println("AccountServiceImpl……我创建了。。");
}
@Override
public void saveAccount() {
System.out.println("AccountServiceImpl……saveAccount方法执行了");
}
public void init(){
System.out.println("AccountServiceImpl……init方法执行了。。");
}
public void destroy(){
System.out.println("AccountServiceImpl……destroy方法执行了。。");
}
}
测试
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//Bean的生命周期
IAccountService accountService = (IAccountService) ac.getBean("accountService");
System.out.println(accountService);
accountService.saveAccount();
System.out.println(ac.isSingleton("accountService"));
ac.close();
System.out.println(accountService);
System.out.println("main 方法结束了。。。。");
}
}
结果
AccountServiceImpl……我创建了。。
AccountServiceImpl……init方法执行了。。
com.zjw.service.impl.AccountServiceImpl@add0edd
AccountServiceImpl……saveAccount方法执行了
true
AccountServiceImpl……destroy方法执行了。。
com.zjw.service.impl.AccountServiceImpl@add0edd
main 方法结束了。。。。
Spring创建Bean的三种方式及Bean的生命周期的更多相关文章
- Spring实例化Bean的三种方式及Bean的类型
1.使用类构造器实例化 [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...
- spring创建bean的三种方式
spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...
- Spring学习之实例化bean的三种方式
实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...
- Spring创建JobDetail的两种方式
一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路径. 2.编 ...
- Spring 循环依赖的三种方式(三级缓存解决Set循环依赖问题)
本篇文章解决以下问题: [1] . Spring循环依赖指的是什么? [2] . Spring能解决哪种情况的循环依赖?不能解决哪种情况? [3] . Spring能解决的循环依赖原理(三级缓存) 一 ...
- spring生成EntityManagerFactory的三种方式
spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...
- Spring 使用AspectJ的三种方式
Spring 使用AspectJ 的三种方式 一,使用JavaConfig 二,使用注解隐式配置 三,使用XML 配置 背景知识: 注意 使用AspectJ 的 时候 要导入相应的Jar 包 嗯 昨天 ...
- Spring静态注入的三种方式
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...
- 0036 Java学习笔记-多线程-创建线程的三种方式
创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...
- js学习-DOM之动态创建元素的三种方式、插入元素、onkeydown与onkeyup两个事件整理
动态创建元素的三种方式: 第一种: Document.write(); <body> <input type="button" id="btn" ...
随机推荐
- JUC并发—2.Thread源码分析及案例应用
大纲 1.什么是线程以及并发编程 2.微服务注册中心案例 3.以工作线程模式开启微服务的注册和心跳线程 4.微服务注册中心的服务注册功能 5.微服务注册中心的心跳续约功能 6.微服务的存活状态监控线程 ...
- 【Unity】投影矩阵和线性深度推导
[Unity]投影矩阵和线性深度推导 网络上有很多投影矩阵的推导,也有很多声称是基于 Unity 的,但和我的实测都不一致(现在看来是因为这些文章并不全面),此外有一些 Unity 本身的函数我也搞不 ...
- 读论文-顺序推荐系统_挑战、进展和前景(Sequential recommender systems_ challenges, progress and prospects)
前言 今天读的论文为一篇于2019年发表的论文,是关于顺序推荐系统(Sequential Recommender Systems,SRSs)的研究,作者对SRSs的挑战.进展和前景进行了系统综述. 要 ...
- helm部署redis集群
Redis 集群部署流程 前提:K8s+helm安装完成 1. 安装 NFS 服务器 1.1 安装 NFS 工具包 在 NFS 服务器上安装 nfs-utils 包: sudo yum install ...
- linux下安装cmake版本
点击查看代码 要在基于ARM的Linux系统上安装CMake 3.19.3版本,你可以按照以下步骤进行操作: 1. **下载CMake 3.19.3版本的ARM架构压缩包**: 你可以从CMake的官 ...
- 阻尼、模态应变能法与FRP的关系
阻尼的概念 系统损耗振动能或声能的能力称为阻尼 阻尼越大,输人系统的能量便能在较短时间内损耗完毕.系统从受激振动到重新静止所经历的时间就越短; 阻尼也可理解为系统受激后迅速恢复到受激前状态的一种能力 ...
- gorm stdErr = sql: Scan error on column index 0, name "total": converting NULL to float64 is unsupported
前言 使用 gorm 查询时,报错:stdErr = sql: Scan error on column index 0, name "total": converting NUL ...
- Xshell连接VMware虚拟机中的CentOS
步骤: 1. 检查Linux虚拟机的网络连接模式,确保它是NAT模式.(由于只在本机进行连接,所以没有选择桥接模式.当然,桥接模式的配置会有所不同,在此不做深入分析) 2. 在VMware works ...
- 使用Python完成设备巡检
在企业网络中,设备巡检是保持网络稳定性和安全性的核心任务.无论是路由器.交换机,还是防火墙和服务器等设备,都需要定期进行巡检,以确保网络设施的正常运行.然而,传统的设备巡检通常是通过手动登录设备.查看 ...
- js调用本地程序资源-兼容所有浏览器
在网页上通过JavaScript调用本地程序,兼容IE8/9/10/11.Opera.Chrome.Safari.Firefox等所有浏览器,在做Web开发时经常会遇到需要调用本地的一些exe或者dl ...