Spring入门程序_06【Bean的作用域——以最常用的singleton和prototype为例】
2023-09-27 14:25:39 时间
小结
新的需要记忆理解的就是
<bean>
中新加的属性吧,比如scope="singleton"
、scope="prototype"
。。。具体看下边的xml文件
目录结构
圈起来的就是
代码(1)—singleton(单例)
先说singleton的例子,与prototype仅仅差别是在中的属性关键字。。。。
空的类
package com.itheima.scope;
public class Scope {
}
beans4.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">
<!-- 这里是singleton作用域 -->
<bean id="scope" class="com.itheima.scope.Scope" scope="singleton"/>
<!-- 这里就是prototype作用域
<bean id="scope" class="com.itheima.scope.Scope" scope="prototype" />-->
</beans>
测试类
package com.itheima.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeTest {
public static void main(String[] args) {
// 定义配置文件路径
String xmlPath = "com/itheima/scope/beans4.xml";
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 输出获得实例
System.out.println(applicationContext.getBean("scope"));
// System.out.println(applicationContext.getBean("scope"));
}
}
输出(1)—singleton(单例)
可由看出,两个输出均一样,这说明singleton作用域仅仅 创建了一个Scop类的实例。
【注】这里,上面xml文件中不写scope="singleton"也可以,因为默认就是它。。
代码(2)—prototype(原型)
与代码(1)区别仅仅如下所示
空的类和上边一样,不再写了。。。。。。。。。。。
beans4.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="scope" class="com.itheima.scope.Scope" scope="singleton"/> -->
<bean id="scope" class="com.itheima.scope.Scope" scope="prototype" />
</beans>
测试类
package com.itheima.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeTest {
public static void main(String[] args) {
// 定义配置文件路径
String xmlPath = "com/itheima/scope/beans4.xml";
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 输出获得实例
System.out.println(applicationContext.getBean("scope"));
System.out.println(applicationContext.getBean("scope"));
System.out.println(applicationContext.getBean("scope"));
// ....................
}
}
输出(2)—prototype(原型)
【注】为了测试,专门又加了一个输出,可以明显看出,prototype作用域可以创建多个不同实例。。。。
相关文章
- Spring+Mybatis整合
- Spring IoC、DI入门小程序
- Spring Boot 第一个示例启动运行
- Spring-程序中获取注册bean的方式
- spring boot + mybatis plus生成了一个controller,返回数据正常,但报错Error resolving template [xx/xx/xxxx], template might not exist or might not be accessible by any of the configured Template Resolvers
- 精品spring boot汽车销售管理系统
- 第二章 第二个spring-boot程序
- 微服务框架:如果不用 Spring Boot,还可以选择谁?
- Spring Boot 实现读写分离,还有谁不会??
- Spring
- 控制spring容器自动加载顺序
- spring boot如何集成resteasy-netty使用netty作为http服务器
- Spring Boot入门(12)实现页面访问量统计功能
- spring+hibernate常见异常集合
- Spring入门程序_08***【Bean的常用装配(1)——基于Xml装配(使用 “设值注入” )】
- Spring入门程序_03【Spring中的Bean——(1)构造器实例化】
- Spring入门程序_02【依赖注入(DI)的实现,通过setter方法来实现】
- Spring入门程序_01【applicationContext.xml配置文件的编写 <bean id=“ “ class=“ “> ClassPathXmlApplicationContext】
- spring之BeanFactoryAware接口
- Spring Boot2 系列教程(三十二)Spring Boot 整合 Shiro
- spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)
- SpringBoot Admin监控Spring程序
- Spring Cloud Consul使用——服务注册与发现(注册中心)