Spring註解開發

2021-10-04 02:34:25 字數 1345 閱讀 2068

引入context約束

<?xml version="1.0" encoding="utf-8"?>

xmlns

=""xmlns:xsi

=""xmlns:context

=""xsi:schemalocation

="/spring-beans.xsd

/spring-context.xsd"

>

beans

>

指定掃瞄包:

<

context:component-scan

base-package

="com.eddievim.pojo"

/>

編寫類及其註解:

@component

("user"

)// 相當於配置檔案中

public

class

user

衍生註解

為了更好的進行分層,spring可以使用其它三個註解,功能一樣,目前使用哪乙個功能都一樣。

@value

@scope

-xml與註解整合開發 :推薦最佳實踐

xml管理bean

註解完成屬性注入

使用過程中, 可以不用掃瞄,掃瞄是為了類上的註解

<

context:annotation-config

/>

作用:

進行註解驅動註冊,從而使註解生效

用於啟用那些已經在spring容器裡註冊過的bean上面的註解,也就是顯示的向spring註冊

如果不掃瞄包,就需要手動配置bean

如果不加註解驅動,則注入的值為null!

編寫乙個實體類,user

@component

//將這個類標註為spring的乙個元件,放到容器中!

public

class

user

新建乙個config配置包,編寫乙個myconfig配置類

@configuration

//代表這是乙個配置類

public

class

myconfig

}

測試

@test

public

void

test2()

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...