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 ...
随机推荐
- js控制数量包含截取
<div class="usermes_index_line"> 进行中的单 <div id="usermes_index_line_i2"& ...
- hdu 2476 题解
题目 题意 给出两个字符串 $ s1,s2 $,每次操作可以使一段连续的子串全变成一个字母,问最少多少次操作可以使 $ s1 $ 变为 $ s2 $. 例如 $ zzzzzfzzzzz $,长度为 $ ...
- 随记sqlserver学习笔记(一)
create database testuse test --部门表create table department( dept_id int not null identity primary key ...
- idea代码提示快捷键设置
代码提示快捷键设置: keymap--Main Menu--Code--Completion--Basic
- IDEA好用插件推荐
Maven Helper:排查maven依赖冲突神器,强力推荐! Alibaba Java Coding Guidelines:阿里巴巴编程规范 CamelCase:驼峰命名工具,SHIFT + AL ...
- A Story of One Country (Hard) CodeForces - 1181E2 (分治)
大意: 给定$n$个平面上互不相交的矩形. 若一个矩形区域只包含一个矩形或者它可以水平或垂直切成两块好的区域, 那么这个矩形区域是好的. 求判断整个平面区域是否是好的. 分治判断, 可以用链表实现删除 ...
- ef core数据迁移的一点小感悟
ef core在针对mysql数据迁移的时候,有些时候没法迁移...有两种情况没法迁移,一种是因为efcore的bug问题导致没法迁移,这个在github上有个问题集,另外一种是对数据表进行较大幅度的 ...
- .NET Core 使用swagger进行分组显示
其实,和swagger版本管理类似;只是平时接口太多;不好供前端人员进行筛选. 下面进入主题: 首先: //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddSwa ...
- springboot笔记02——快速入门quickstart
前言 学习一个新的框架,往往会用一个quickstart快速入门,这次就写一下springboot的quickstart程序. 开发环境 JDK 1.8 Springboot 2.1.6 Maven ...
- python之爬取小说
继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...