讓乙個類跟著springboot專案的啟動而啟動

2021-10-03 13:14:54 字數 1458 閱讀 3412

1、將要執行的方法所在的類交個spring容器掃瞄(

@component),

並且在要執行的方法上新增@postconstruct註解或者靜態**塊執行

@component

public

class

test2

/** * @postcontruct』在依賴注入完成後自動呼叫

*/@postconstruct

public

static

void

haha()

}2.類上加註解@component,實現initializingbean介面

@component

public

class

busiclient

implements

initializingbean

} system.out.

println

("網路通訊已準備好,可以進行業務操作了........");

scanner scanner =

newscanner

(system.in)

;while

(true

)elseif(

"q".

equals

(msg.

tolowercase()

))} scanner.

close()

; system.

exit(1

);}else}}

}3、實現servletcontextaware介面並重寫其setservletcontext方法

@component

public

class

teststarted

implements

servletcontextaware

}4、實現servletcontextlistener介面

/** * 在初始化web應用程式中的任何過濾器或servlet之前,將通知所有servletcontextlistener上下文初始化。

*/@override

public

void

contextinitialized

(servletcontextevent sce)

/** * 在同一應用程式上下文中,可以使用有序介面或@order注釋對其進行排序。

*/@override

public

void

runthrows exception

6、實現commandlinerunner介面

/** *

*/@override

public

void

run(string...

)throws exception

使用idea建立第乙個springboot專案

前言 如今springboot越來越火,越來越多的公司選擇使用springboot作為專案的開發框架,其設計目的就是用來簡化spring專案的搭建和開發過程,省略了傳統spring springmvc專案繁瑣的配置,可以讓開發人員快速上手。下面詳細說明下如何使用idea建立我們的第乙個springb...

定義乙個類讓其只能例項化乙個物件

乙個類只例項化出乙個物件稱為單例模式 include class singleclass else private singleclass static int count 靜態成員變數count,不允許在類中初始化。定義為const則可以在這初始化,但是不可更改,不適合在這使用 int singl...

python 之 類(乙個類包含乙個類)

1.知識點 1 乙個類可以作為另乙個類的元素。2 當乙個類的屬性為列表新增另乙個類名時,會預設新增另乙個類的 str self 的返回值 箱子 class box 屬性 def init self,b color,b volumn self.color b color self.volumn b v...