03 Spring对Bean的管理
Spring创建bean的三种方式
1.第一种方式:使用默认构造函数创建
bean.xml
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 默认情况下使用的就是无参数的构造方法. -->
<bean id="accountService" class="com.itzn.service.AccountServiceImpl"></bean>
</beans>
AccountServiceImpl.java
package com.itzn.service;
import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl(){
System.out.println("默认构造");
}
public void save() {
System.out.println("保存方法");
}
}
测试:AccountTest.java
package com.itzn.ui; import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
import com.itzn.service.AccountServiceImpl;
import com.itzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService iAccountService = (IAccountService) ac.getBean("accountService");
System.out.println(iAccountService); iAccountService.save();
}
}
输出结果:
2.第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
bean.xml
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 第一种方式:默认情况下使用的就是无参数的构造方法.
<bean id="accountService" class="com.itzn.service.AccountServiceImpl"></bean>
-->
<!-- 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器) -->
<bean id="beanFactory" class="com.itzn.factory.BeanFactory"></bean>
<bean id="accountService" factory-bean="beanFactory" factory-method="getASIBean"></bean>
</beans>
BeanFactory.java
package com.itzn.factory; import com.itzn.service.AccountServiceImpl;
public class BeanFactory {
public AccountServiceImpl getASIBean(){
System.out.println("BeanFactory实例工厂的getASIBean方法...");
return new AccountServiceImpl();
}
}
AccountTest .java
package com.itzn.ui; import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
import com.itzn.service.AccountServiceImpl;
import com.itzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountTest {
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService iAccountService = (IAccountService) ac.getBean("accountService");
System.out.println(iAccountService); iAccountService.save();
}
}
测试结果:
3.第三种方式:第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
bean.xml
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 第一种方式:默认情况下使用的就是无参数的构造方法.
<bean id="accountService" class="com.itzn.service.AccountServiceImpl"></bean>
-->
<!-- 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
<bean id="beanFactory" class="com.itzn.factory.BeanFactory"></bean>
<bean id="accountService" factory-bean="beanFactory" factory-method="getASIBean"></bean>
-->
<!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) -->
<bean id="accountService" class="com.itzn.factory.StaticFactory" factory-method="getASIBean"></bean>
</beans>
StaticFactory.java
package com.itzn.factory;
import com.itzn.service.AccountServiceImpl;
public class StaticFactory { public static AccountServiceImpl getASIBean(){
System.out.println("StaticFactory实例工厂的getASIBean方法...");
return new AccountServiceImpl();
}
}
AccountTest.java
package com.itzn.ui;
import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
import com.itzn.service.AccountServiceImpl;
import com.itzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService iAccountService = (IAccountService) ac.getBean("accountService");
System.out.println(iAccountService); iAccountService.save();
}
}
测试结果:
bean对象的作用范围
scope属性 :
* singleton:单例的.(默认的值.)
* prototype:多例的.
* request:web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();
* session:web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();
* globalSession:一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;
实际开发中主要使用singleton,prototype
Customer.java
package cn.itzn.srping.demo3;
public class Customer {
public Customer(){
System.out.println("Customer被实例化了");
}
}
bean.xml
//单例配置
<bean id="customer" class="cn.itzn.srping.demo3.Customer"></bean>
//多例配置
<bean id="customer" class="cn.itzn.srping.demo3.Customer" scope="prototype"></bean>
SpringTest.java
public class SpringTest {
@Test
public void Dome1()
{
ApplicationContext myContext=new ClassPathXmlApplicationContext("bean.xml");
Customer customer1=(Customer) myContext.getBean("customer");
System.out.println(customer1); Customer customer2=(Customer) myContext.getBean("customer");
System.out.println(customer2);
}
}
bean对象的生命周期
单例对象:scope="singleton"
一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:
对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
多例对象:scope="prototype"
每次访问对象时,都会重新创建对象实例。
生命周期:
对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。
对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。
03 Spring对Bean的管理的更多相关文章
- spring对bean的管理细节
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring中bean的管理
Spring 中常见的容器 我们知道spring容器就是spring中bean的驻留场所.spring容器并不是只有一个.spring自带了多个容器实现,可以归为两种不同的类型:bean工厂和应用上下 ...
- Spring之Bean的管理方式(Content,Beans)
Spring的bean管理(注释) 注解 代码里特殊的标记,使用注解也可以直接完成相关功能 注解写法:@注解名称(属性名=属性值) 使用在类,方法,属性上面 Spring注解开发准备 导入jar包 ( ...
- IOC——Spring的bean的管理(xml配置文件)
Bean实例化(三种方式) 1.使用类的无参构造进行创建(大多数情况下) <bean id="user" class="com.bjxb.ioc.User" ...
- Spring中Bean的管理问题
首先,配置文件中定义的bean并不是都在启动时实例化. <bean id="accountService" class="com.foo.DefaultAccoun ...
- 03 Spring框架 bean的属性以及bean前处理和bean后处理
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...
- IOC——Spring的bean的管理(注解方式)
注解(简单解释) 1.代码里面特殊标记,使用注解可以完成一定的功能 2.注解写法 @注解名称(属性名称=属性值) 3.注解使用在类上面,方法上面和属性上面 注意:注解方式不能完全替代配置文件方式 Sp ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
目前这里能调用是因为,在service的实现类里面,new了一个dao的对象 正常情况下 这里不应该是new一个对象,应该等于null或为空 设置为空侯再运行就会报错 出错的原因是这里为null 需要 ...
- Spring(二)Bean入门
一.BeanFactory介绍 1.1.Bean: 在Spring技术中是基于组件的 最基本了是最常用的单元 其实实例保存在Spring的容器当中 Bean通常被定义在配置文件当中,Bean实例化由S ...
随机推荐
- centos7修改yum源为阿里镜像
参考博客: https://blog.csdn.net/kxwinxp/article/details/78578492 https://blog.csdn.net/inslow/article/de ...
- 《ucore lab2》实验报告
资源 ucore在线实验指导书 我的ucore实验代码 练习1:实现 first-fit 连续物理内存分配算法 题目 在实现first fit 内存分配算法的回收函数时,要考虑地址连续的空闲块之间的合 ...
- [转帖]PCIe 6.0 v0.3版本草案已完稿:2021年转正、x16带宽飙至128GB/s
PCIe 6.0 v0.3版本草案已完稿:2021年转正.x16带宽飙至128GB/s https://www.cnbeta.com/articles/tech/899389.htm 硬件发展突飞猛进 ...
- Nginx 系列教程
Nginx(一):Nginx介绍 Nginx(二):编译安装Nginx及参数说明 Nginx(三):nginx.conf配置文件说明 [1] 配置参数说明 Nginx(三):nginx.conf配置文 ...
- Spring Boot 入门(八):集成RabbitMQ消息队列
本片文章续<Spring Boot 入门(七):集成 swagger2>,关于RabbitMQ的介绍请参考<java基础(六):RabbitMQ 入门> 1.增加依赖 < ...
- python 之 数据库(多表查询之连接查询、子查询、pymysql模块的使用)
10.10 多表连接查询 10.101 内连接 把两张表有对应关系的记录连接成一张虚拟表 select * from emp,dep: #连接两张表的笛卡尔积 select * from emp,de ...
- WUSTOJ 1298: 操作格子(Java)
题目链接:
- docker 实践九:docker swarm
介绍了 docker 三剑客中的 docker-machine 和 docker-compose 之后,就剩下一个 docker swarm 了.那本篇的主角就是它了. 注:环境为 CentOS7,d ...
- IIS不能下载config配置文件的解决方法
之前作程序升级的时候,需要从服务端下载后缀为config的配置文件,结果程序抛出404异常.后来百度才知道,是IIS禁止下载config文件的原因.在这里记录一下解决方法. 在我的电脑,右键管理,打开 ...
- 去除echarts饼状图的引导线
series: { name: "流量占比分布", type: "pie", radius: ["40%", "60%" ...