spring bean的作用域

bean的作用域

1、bean常见的四种作用域

  • singleton:单例,整个IOC容器中只会存在一个实例对象。
  • prototype:每个注入的地方都会生成一个新的实例对象。
  • request:每个request请求都会生成一个新的实例对象。
  • session:每个session会话中生成一个实例对象。

2、实例代码

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mine</groupId>
	<artifactId>spring-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>

	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>

		<!-- log -->
		<!-- log4j通过slf4j来代理 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
		</dependency>
		<!-- apache commons logging通过slf4j来代理 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
		</dependency>
		<!-- java.util.logging 通过slf4j来代理 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jul-to-slf4j</artifactId>
		</dependency>
		<!-- log -->
	</dependencies>
</project>

PrototypeScopeService

package com.mine.scope.service;

import java.util.UUID;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Service
@Scope("prototype")
@Data
@Slf4j
public class PrototypeScopeService {
	private String serviceId;

	public PrototypeScopeService() {
		serviceId = "prototype-" + UUID.randomUUID().toString();
		log.info("PrototypeScopeService service id :" + serviceId);
	}
}

RequestScopeService

注意:此地方只能用@RequestScope,不能用@Scope(“request”)

package com.mine.scope.service;

import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@Data
@RequestScope
public class RequestScopeService {
	private String serviceId;

	public RequestScopeService() {
		serviceId = "request-" + UUID.randomUUID().toString();
		log.info("RequestScopeService service id :" + serviceId);
	}

}

SessionScopeService

注意:此地方只能用@SessionScope,不能用@Scope(“session”)

package com.mine.scope.service;

import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.SessionScope;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Service
@SessionScope
@Slf4j
@Data
public class SessionScopeService {
	private String serviceId;

	public SessionScopeService() {
		serviceId = "session-" + UUID.randomUUID().toString();
		log.info("SessionScopeService service id :" + serviceId);
	}

}

ScopeController

package com.mine.scope.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mine.scope.service.PrototypeScopeService;
import com.mine.scope.service.RequestScopeService;
import com.mine.scope.service.SessionScopeService;

@RestController
@RequestMapping("scope")
public class ScopeController {
	@Autowired
	private PrototypeScopeService prototypeScopeService1;
	
	@Autowired
	private PrototypeScopeService prototypeScopeService2;

	@Autowired
	private RequestScopeService requestScopeService;

	@Autowired
	private SessionScopeService sessionScopeService;

	@RequestMapping("getPrototype")
	public String getPrototype(String type) {
		if ("1".equals(type)) {
			return prototypeScopeService1.getServiceId();
		} else {
			return prototypeScopeService2.getServiceId();
		}
	}

	@RequestMapping("getRequest")
	public String getRequest(String type) {
		return requestScopeService.getServiceId();
	}

	@RequestMapping("getSession")
	public String getSession(String type) {
		return sessionScopeService.getServiceId();
	}
}

SpringApp

package com.mine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringApp {

	public static void main(String[] args) {
		SpringApplication.run(SpringApp.class, args);
	}

}

3、启动应用后,测试地址如下

http://localhost:8080/scope/getPrototype?type=1

http://localhost:8080/scope/getPrototype?type=2

会生成两个不同的实例,不会变。

http://localhost:8080/scope/getRequest

每次请求,都会生成一个实例

http://localhost:8080/scope/getSession

每个会话一个实例,清除浏览器缓存后,会重新生成一个实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值