Spring註解開發

2021-09-28 21:45:41 字數 1551 閱讀 6555

在spring4之後,要使用註解開發,但是必須保證aop的包存在

使用註解必須保證匯入context約束增加註解的支援

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

xmlns

=""xmlns:xsi

=""xmlns:context

=""xsi:schemalocation

="/spring-beans.xsd

/spring-context.xsd"

>

<

context:annotation-config

/>

beans

>

@component

放在類上,說明這個類被spring管理了,就是bean,相當於下面的bean:

@component

public

class

user

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

xmlns

=""xmlns:xsi

=""xmlns:context

=""xsi:schemalocation

="/spring-beans.xsd

/spring-context.xsd"

>

<

context:annotation-config

/>

<

context:component-scan

base-package

="com.kuang.pojo"

/>

beans

>

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

@value(「狂神」):相當於下面的,可以放在屬性或者setter方法上

name

="name"

value

="狂神"

/>

@component

public

class

user

@autowired:自動裝配通過name和type

@qualifier:通過name和type匹配不上再使用

@resource:自動裝配通過name匹配

@scope(value = 「singleton」)

@component

@scope

(value =

"singleton"

)public

class

user

}

xml與註解的最佳實踐:

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註解開發

org.springframeworkgroupid spring webmvcartifactid 5.2.0.releaseversion dependency org.springframeworkgroupid spring jdbcartifactid 5.2.0.releaseversi...