Spring中使用註解開發

2021-10-21 07:03:37 字數 1361 閱讀 7277

在spring4之後,要使用註解開發,必須要保證aop的包匯入了

使用註解需要匯入context約束增加註解的支援

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

xmlns

=""xmlns:context

=""xmlns:xsi

=""xsi:schemalocation

="/spring-beans.xsd

/spring-context.xsd"

>

<

context:annotation-config

/>

beans

>

1.bean

2.屬性如何注入

//等價於

//@component 元件

@component

public

class

user

3.衍生的註解:

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

dao【@repository】

service【@service】

controller【@controller】

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

4.作用域:

@component

@scope

("singleton"

)public

class

user

小結:

xml與註解:

xml更加萬能,適用於任何場合!維護簡單方便

註解:不是自己的類無法使用,維護相對複雜

xml與註解的最佳實現:

xml用來管理bean;

註解只負責屬性的注入;

我們在使用的過程中,只需要注意乙個問題:必須讓註解生效,就需要開啟註解的支援

<

context:component-scan

base-package

="com.dai.pojo"

/>

<

context:annotation-config

/>

Spring中使用註解開發

在spring4之後,要使用註解開發,必須保證匯入了aop的包!同時需要匯入context的支援和約束!xmlns xmlns xsi xmlns context xmlns aop xsi schemalocation context annotation config beans 還需要掃瞄指定...

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