目录
目录
近日研究Spring和SpringBoot的一些内容,给大家做一些分享,请大家多多提出您的宝贵意见。
学习知识要了解其涉及到的基本概念,才能理解这个知识,并且做到融汇贯通。
基本概念
Bean
官网链接:核心技术 (spring.io)
官网上介绍,bean是一个由Spring IoC容器实例化、组装和管理的对象。
作用域
首先,我们先了解什么是作用域,为什么要有作用域?
百度百科作用域概念:百度百科-验证
只要是代码,就至少有一个作用域
spring支持的bean作用域有哪些?
概念
① singletgn(唯一Bean实例)
使用该属性定义Bean时,IOC容器仅创建一个Bean实例,IOC容器每次返回的是同一个Bean实例。
② prototype(原型Bean)
使用该属性定义Bean时,lOC容器可以创建多个Bean实例,每次返回的都是一个新的实例。
③ request
该属性仅对HTTP请求产生作用,使用该属性定义Bean时,每次HTTP请求都会创建一个新的Bean,适用于WebApplicationContext环境。
④ session
该属性仅用于HTTP Session,同一个Session共享一个Bean实例。不同Session使用不同的实例。
⑤ global-session
该属性仅用于HTTP Session,同session作用域不同的是,所有的Session共享一个Bean实例。
1. Singleton 作用域
Singleton 是 Spring 默认的 Bean 作用域,它表示在整个应用程序中只存在一个 Bean 实例。以下是使用 Singleton 作用域创建 Bean 的示例:
@Configuration
public class AppConfig {
@Bean
@Scope("singleton")
public MyBean myBean() {
return new MyBean();
}
}
在上面的示例中,myBean()
方法使用 @Scope("singleton")
注解来指定 Bean 的作用域为 Singleton。
2. Prototype 作用域
Prototype 作用域表示每次从容器中获取 Bean 时都会创建一个新的实例。以下是使用 Prototype 作用域创建 Bean 的示例:
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public MyBean myBean() {
return new MyBean();
}
}
在上面的示例中,myBean()
方法使用 @Scope("prototype")
注解来指定 Bean 的作用域为 Prototype。
3. Request 作用域
Request 作用域表示每次 HTTP 请求都会创建一个新的 Bean 实例,该 Bean 实例仅在当前请求范围内可见。以下是使用 Request 作用域创建 Bean 的示例:
@Configuration
public class AppConfig {
@Bean
@Scope("request")
public MyBean myBean() {
return new MyBean();
}
}
在上面的示例中,myBean()
方法使用 @Scope("request")
注解来指定 Bean 的作用域为 Request。
4. Session 作用域
Session 作用域表示每个 HTTP Session 都会创建一个新的 Bean 实例,该 Bean 实例仅在当前 Session 范围内可见。以下是使用 Session 作用域创建 Bean 的示例:
@Configuration
public class AppConfig {
@Bean
@Scope("session")
public MyBean myBean() {
return new MyBean();
}
}
实例:singletgn和prototype
写了一个maven项目引入了spring-webmvc基础依赖,对singletgn和prototype二者做个比较
pom.xml文件
UserService文件
package com.tfjy.test;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* @BelongsPackage: com.tfjy.test
* @Author: aqiu
* @Description: 服务类
* @CreateTime: 2023-01-28 09:59
* @Version: 1.0
*/
@Component
//@Scope("prototype") //这里是开启注解,使用property类型的bean
public class UserService {
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan base-package="com.tfjy.test"/>
<!-- 将UserService设置为原型bean-->
<!-- <bean id="UserService" class="com.tfjy.test.UserService" scope="prototype"></bean>-->
</beans>
Test文件
import com.tfjy.test.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @BelongsProject: demo
* @Author: aqiu
* @Description: TODO
* @CreateTime: 2023-01-28 10:01
* @Version: 1.0
*/
public class Test {
//bean验证
@org.junit.Test
public void beanTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
UserService userServiceOne = context.getBean("UserService", UserService.class);
UserService userServiceTwo = context.getBean("UserService", UserService.class);
System.out.println(userServiceOne);
System.out.println(userServiceTwo);
//通过equals方法判断两个对象是否相等
if(userServiceOne.equals(userServiceTwo)){
System.out.println("两次getBean方法,获得了同一个单例对象");
}else{
System.out.println("两次getBean方法,获得的不是同一个单例对象");
}
}
}
不做任何操作,默认的获得了同一个单例对象
使用两种方式可以更换成property类型的bean
其一,使用注解的方式
在需要交由IOC容器管理的bean对象类上面添加**@Scope(“prototype”)**注解。
其二,使用xml配置文件的方式
bean.xml文件中的
<!--将UserService设置为原型bean-->
<bean id="UserService" class="com.tfjy.test.UserService" scope="prototype"></bean>