Spring註解開發

2022-10-08 17:30:17 字數 1085 閱讀 5121

一、配置命名空間

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

xmlns:context=""

xsi:schemalocation=" /spring-beans.xsd

/spring-context.xsd">

二、註解(必須配置元件掃瞄)

@component:使用在類上用於例項化bean    等價於  

@autowired :使用在字段上用於根據型別依賴注入  (如果不加@qualifier)

@qualifier: 結合@autowired一起使用用於根據名稱進行依賴注入  (按照名稱注入)

等價於

@resource : 相當於@autowired+@qualifier,按照名稱進行注入  @resource(name = "userdao")

@controller : 使用在web層類上用於例項化bean  和 @component 一樣

@service : 使用在service層類上用於例項化bean 和 @component 一樣

@repository : 使用在dao層類上用於例項化bean 和 @component 一樣

@value : 注入普通屬性  可以用來獲取容器中的key值

@scope : 標註bean的作用範圍 

@postconstruct:使用在方法上標註該方法是bean的初始化方法  等價於 init-method

@predestroy : 使用在方法上標註該方法是bean的銷毀方法 等價於  destroy-method

新註解: 

@configuration : 用於指定當前類是乙個 spring 配置類,當建立容器時會從該類上載入註解 

@componentscan : 用於指定 spring 在初始化容器時要掃瞄的包。等價於 

@bean : 使用在方法上,標註將該方法的返回值儲存到 spring 容器中 

@propertysource  : 用於載入.properties 檔案中的配置

@import : 用於匯入其他配置類

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