Spring boot 學習筆記(三)

2021-08-07 19:09:49 字數 1165 閱讀 8464

一.bean的scope

scope描述的是spring容器如何新建bean的市裡的。spring的scope有以下幾種,通過

@scope註解來實現。

(1)singleton:乙個spring容器中只有乙個bean的例項,為此spring的預設配置,全容器

共享乙個例項。

(2)prototype:每次呼叫新建乙個bean的例項。

(3)request:web專案中,給每乙個http request新建乙個bean例項。

(4)session:web專案中,給每乙個http session新建乙個bean例項。

(5)globalsession:這個只在portal應用中有用,給每乙個global http session新建乙個

例項。二,示例一

package scope;

import org.springframework.stereotype.service;

@service

public class demosingletonservice

**解釋

預設為singleton,相當於@scope(「singleton」)。

例項二package scope;

import org.springframework.context.annotation.scope;

import org.springframework.stereotype.service;

@service

@scope("prototype")

public class demoprototypeservice

**解釋

宣告scope為prototype。

示例三 配置類

package scope;

import org.springframework.context.annotation.componentscan;

import org.springframework.context.annotation.configuration;

@configuration

@componentscan("scope")

public class scopeconfig

例項四 測試

結果true

false

false

SpringBoot學習筆記(三)

restcontroller 註解,對具有返回型別的controller類使用。相當於 responsebody controller。使用 restcontroller的目的在於使用傳統方法是返回資料格式都為json,需要使用 responsebody註解,但是 restcontroller修飾的...

springboot 學習筆記(三)

在實現開發中配置檔案會有多個 可以將主配置調換到不同的配置檔案中 以下配置改變工作區配置檔案 啟用某個配置檔案 spring.profiles.active demo 注意在專案檔案下編寫乙個控制器 controller public class democontroller private str...

Spring Boot學習筆記(三)

在我們開發專案過程中,經常需要定時任務來幫助我們來做一些內容,spring boot 預設已經幫我們實行了,只需要新增相應的註解就可以實現 pom 包裡面只需要引入 spring boot starter 包即可 org.springframework.boot spring boot starte...