Spring學習之八 使用註解開發

2021-10-13 21:20:52 字數 968 閱讀 1279

在spring4之後,要使用註解開發,必須要保證aop的包匯入,要匯入context約束,增加註解的支援。

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

xmlns

=""xmlns:xsi

=""xmlns:context

=""xsi:schemalocation=""

>

<

context:annotation-config

/>

beans

>

//等價於

@component

public

class

user

@component 有幾個衍生註解,我們在web開發中,會按照mvc三層架構分層

- dao 【@repository】

- service【@service】

- controller 【@controller】

這四個註解功能都是一樣的,都是代表將某個類註冊到spring中,裝配bean

//等價於

@component

@scope

("prototype"

)//作用域

public

class

user

xml與註解:

xml與註解最佳實踐:

<

context:component-scan

base-package

="com.***"

/>

<

context:annotation-config

/>

spring學習(八 使用註解開發)

在spring4之後,要是用註解開發,必須保證aop的包匯入了 使用註解需要匯入context約束,增加註解的支援!bean component 元件 等價於 component public class user 屬性如何注入 component 元件 等價於 component public c...

Spring 使用註解開發

屬性的注入 component 元件,放在類上,說明這個類被spring管理了,就是bean component 元件 等價於 component public class user value 等價於 component public class user衍生註解 這四個註解功能都是一樣的,都是代...

Spring使用註解開發

在對應的實體類上宣告 component註解即可將類註冊為乙個元件,從而被spring掃瞄到。component public class user public user string name public string getname public void setname string na...