基于XML装配bean的解析-Bean的作用域
一、Bean的种类
1、普通bean:<bean id="" class="A"> ,spring直接创建A实例,并返回。
2、FactoryBean:是一个特殊的bean,具有工厂生成对象的能力,只能生成特定的对象。bean必须使用 FactoryBean接口,此接口提供方法 getObject() 用于获得特定bean。
<bean id="" class="FB"> 先创建FB实例,使用调用getObject()方法,并返回方法的返回值
FB fb = new FB();
return fb.getObject();
BeanFactory 和 FactoryBean 对比?
BeanFactory:工厂,用于生成任意bean。
FactoryBean:特殊bean,用于生成另一个特定的bean。例如:ProxyFactoryBean ,此工厂bean用于生产代理。
<bean id="" class="....ProxyFactoryBean"> 获得代理对象实例,AOP使用。
二、作用域
作用域:用于确定Spring创建Bean的实例个数。

取值:
singleton 单例,是默认值。
prototype 多例,每执行一次getBean将获得一个实例。例如:struts整合spring,配置action多例。
配置信息:
<bean id="" class="" scope="">
例如:
<bean id="userServiceId" class="com.zju.scope.UserServiceImpl" scope="prototype"></bean>
例1:singleton 单例
UserService.java
package com.zk.myspring;
public interface UserService {
public void addUser();
}
UserServiceImpl.java
package com.zk.myspring;
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("UserService");
}
}
ApplicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 默认scope为singleton -->
<bean id="userserviceId" class="com.zk.myspring.UserServiceImpl" scope="singleton"></bean>
</beans>
TestDemo.java
package com.zk.myspring; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test
public void demo1(){
String xmlpath="ApplicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlpath);
UserService us=ac.getBean("userserviceId", UserServiceImpl.class);
UserService us2=ac.getBean("userserviceId", UserServiceImpl.class);
//us.addUser();
//us2.addUser();
System.out.println(us);
System.out.println(us2);
System.out.println(us==us2);
}
}
运行效果图:

例2:prototype 多例
UserService.java
package com.zk.myspring;
public interface UserService {
public void addUser();
}
UserServiceImpl.java
package com.zk.myspring;
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("UserService");
}
}
TestDemo.java
package com.zk.myspring; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test
public void demo1(){
String xmlpath="ApplicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlpath);
UserService us=ac.getBean("userserviceId", UserServiceImpl.class);
UserService us2=ac.getBean("userserviceId", UserServiceImpl.class);
//us.addUser();
//us2.addUser();
System.out.println(us);
System.out.println(us2);
System.out.println(us==us2);
}
}
ApplicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="userserviceId" class="com.zk.myspring.UserServiceImpl" scope="prototype"></bean>
</beans>
运行效果图:

基于XML装配bean的解析-Bean的作用域的更多相关文章
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
- Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂
Spring中基于XML中的装配bean有三种方式: 1.默认构造 2.静态工厂 3.实例工厂 1.默认构造 在我们在Spring的xml文件中直接通过: <bean id=" ...
- 02Spring基于xml的IOC配置--实例化Bean的三种方式
maven依赖 <dependencies> <!--IOC相关依赖--> <dependency> <groupId>org.springframew ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- Spring源码学习笔记之基于ClassPathXmlApplicationContext进行bean标签解析
bean 标签在spring的配置文件中, 是非常重要的一个标签, 即便现在boot项目比较流行, 但是还是有必要理解bean标签的解析流程,有助于我们进行 基于注解配置, 也知道各个标签的作用,以及 ...
- 0003 - 基于xml的Spring Bean 的创建过程
一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...
- spring 基于XML和注解的两种事务配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- springMvc的一些简介 和基于xml的handlerMapping基本流程
其它步骤就不在介绍了 在大多数情况,都会使用基于annotation的方式进行HandlerMapping处理,在这里基于对这个流程的了解,就采用了基于xml配置了一个HandlerMapping & ...
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...
随机推荐
- maven通过pom文件下载相关依赖包的网址
下载有关依赖插件的网址去MVNREPOSITORY仓库寻找对应的版本然后添加到pom文件中就可以自动下载了 网址为:https://mvnrepository.com
- C++-基类的析构函数为什么要加virtual虚析构函数(转)
知识背景 要弄明白这个问题,首先要了解下C++中的动态绑定. 关于动态绑定的讲解,请参阅: C++中的动态类型与动态绑定.虚函数.多态实现 正题 直接的讲,C++中基类采用virtual虚析构函数是 ...
- Python标准库Random
基本方法 获取一个[0,1)的随机浮点数: import random print(random.random()) #输出 0.6701488343121276 获取指定区间的随机浮点数: impo ...
- python实现进度条下载
核心代码: for i in range(10): print('\r' + '>' * i, end='') 示例展示以搜狗输入法为例: import timeimport requestsi ...
- Windows ThinPC 7 部署后续设置与本地化
还原注销背景 %system32%\oobe\info\backgrounds 删除该目录下所有文件后变为wes7背景 24时制与非UNICODE字符乱码解决 Control Panel \ Regi ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy
//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...
- 15分钟带你了解前端工程师必知的javascript设计模式(附详细思维导图和源码)
15分钟带你了解前端工程师必知的javascript设计模式(附详细思维导图和源码) 前言 设计模式是一个程序员进阶高级的必备技巧,也是评判一个工程师工作经验和能力的试金石.设计模式是程序员多年工作经 ...
- VS 2017 mscorlib.dll 加载元数据时发生严重错误,需要终止调试
VS 2017 mscorlib.dll 加载元数据时发生严重错误,需要终止调试 C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0. ...
- COMMUNITY DETECTION_python-louvain
Python-louvain Package pip install python-louvain import community #first compute the best partition ...
- 2017-12-08 违法数据筛选.sql
SELECT R. ID, R.LKBH, R.CDBH, R.FXBH, R.ZJBH, R.SBBH, R.CPHM, R.CPYSBH, R.CPYS, R.CSYSBH, R.CSYS, R. ...