學習筆記 Spring註解開發

2021-09-25 19:49:56 字數 4269 閱讀 8765

ioc註解

看看之前開發的案例, 兩個bean(bean1和bean2), bean1依賴bean2

bean**:

public

class

bean1

@override

public string tostring()

}class

bean2

配置檔案中這樣配:

這時列印乙個bean1物件, 其中的bean2屬性是空的(很好理解, 因為沒有給bean1注入bean2):

@runwith

(springjunit4classrunner.

class

)@contextconfiguration()

public

class

}

之前開發時是在bean元素裡配置property元素, 然後再注入. 現在使用@autowired簡單些, 直接在屬性上面或者屬性的setter方法上面配置@autowired即可

package _03_anno._1_start;

import org.springframework.beans.factory.annotation.autowired;

public

class

bean1

}class

bean2

在屬性上配置時甚至可以不要setter方法

注意: 最好還是在配置檔案中配上, 表示di註解解析器

說明:

使用@qualifier(bean.id")的方式可以按照型別去尋找物件, 找到多個再按id去尋找(如果找不到則報錯)

@autowired

@qualifier

("bean1_01"

)private bean1 b1;

@test

public

void

testbean1()

@resource註解和@autowired功能其實是一樣的,且都需要配置.

不同點:

@autowired註解用於給物件屬性注入值, 而**@value註解用於給常量注入值**, 相當於bean的property屬性. 下面演示:

bean:

package _03_anno._2_value;

import org.springframework.beans.factory.annotation.value;

public

class

valuebean

}

配置檔案:

測試**:

@autowired

private valuebean valuebean;

@test

public

void

testvaluebean()

現在列印出來肯定是空的(port=8080), 因為沒有注入值, 之前我們是在配置檔案中新增元素, 在其中設定value屬性, 現在使用註解方式:

@value

("8080"

)private

int port;

@override

public string tostring()

這樣就能輸出"port=8080"

然而這樣做顯然沒有什麼用, 因為我還不如直接寫private int port = 8080;, 但為了降低耦合度, 可以將值寫在properties檔案中, 讓spring載入配置檔案即可

新建service.properties檔案:

service.port=8080
此時的xml檔案需要載入service.properties檔案:

現在的bean應該這樣寫(解析properties檔案):

@value

("$"

)private

int port;

@override

public string tostring()

測試**不變, 此時就能正常輸出port=8080

之前我們讓spring幫我們管理bean使用的是xml配置方式, 但這樣有點繁瑣, 使用註解可以簡單很多

bean

package _03_anno._3_component;

import org.springframework.stereotype.component;

@component

("componentbeanid"

)public

class

componentbean

@component註解即代表控制反轉,括號裡的value值相當於xml配置中的id屬性值. 因為貼在哪個類上就代表管理哪個類, 所以不需要類似xml中的class屬性

但這樣還不夠. 類似使用注入註解需要在xm檔案中配置"di註解解析器", 使用ioc註解需要在xm檔案中配置"ioc註解解析器", 用於解析ioc註解(即@component)

base-package屬性代表包名, 表示從哪個包下去掃瞄元件註解

其實除開@component註解另外還有3個元件, 但這4個元件功能是一樣的, 下面簡單說下區別:

優先使用下面3個.

之前配置bean的作用域(單例還是多例)和生命週期方法是在xml中配置的, 如下:

bean:

package _03_anno._4_lifecycle;

public

class

bean1

public

void

open()

public

void

dowork()

public

void

destroy()

}

配置檔案:

現在使用註解配置,scope屬性對應@scope, init-method屬性對應@postconstruct, destroy-method屬性對應predestroy, 如下演示:

bean:

package _03_anno._4_lifecycle;

import org.springframework.context.annotation.scope;

import org.springframework.stereotype.component;

@component

@scope

("prototype"

)public

class

bean1

public

void

open()

public

void

dowork()

public

void

destroy()

}

配置檔案中需新增

Spring註解開發

spring註解開發 dao層用的註解 repository service層的註解 service controller表現層的註解 controller 以上的三個註解都是用 componment新增三個衍生的註解 屬性依賴注入 value的屬性注入 value wwtmy love 注入的是屬...

spring註解開發

第一步,設定xml約束檔案 第一步,設定xml約束檔案 xmlns xsi xmlns context xsi schemalocation spring beans.xsd spring context.xsd 第二步,定義bean 除了 component外,spring提供了3個功能基本和 c...

Spring註解開發

在spring4之後,要使用註解開發,但是必須保證aop的包存在 使用註解必須保證匯入context約束增加註解的支援 xmlns xmlns xsi xmlns context xsi schemalocation spring beans.xsd spring context.xsd conte...