Spring常用註解

2021-09-24 16:33:51 字數 922 閱讀 6166

註解作用

原始註解(配置ioc和di)

@component

例項化bean

@controller

使用在web層的bean上

@service

使用service層的bean上

@repository

使用在dao層的bean上

@autowired

按照型別自動注入 (常用)

@qualifier("userdao")

按照spring容器中bean的id進行匹配 結合@autowired一起用

@resource(name="userdao")

相當於@autowired+@qualifier("userdao")

@value("$")

注入普通字串

@scope("singleton/prototype")

單例/多例

全註解配置

@configuration

加在類上,宣告為配置類

@componentscan(basepackages=)

加在類上,包掃瞄

@import()

加在類上,匯入其它配置類

@propertysource("classpath:jdbc.properties")

加在類上,載入properties配置檔案

@bean(name="datasource")

載入方法上,將該方法的返回值以指定的名稱儲存到spring容器中

spring整合junit

@runwith(springjunit4classrunner.class)

使用哪乙個類進行測試

載入配置檔案

@contextconfiguration(classes=)

載入配置類(全註解配置方式測試使用)

Spring常用註解

spring常用註解 1 引入context命名空間 在spring的配置檔案中 配置檔案如下 xml xmlns context spring context 2.5.xsd 開啟配置 spring 會自動掃瞄cn.pic包下面有註解的類,完成bean的裝配。xml xmlversion 1.0 ...

Spring常用註解

在spring中常用的註解 autowired註解 不推薦使用,建議使用 resource autowired可以對成員變數 方法和建構函式進行標註,來完成自動裝配的工作。autowired的標註位置不同,它們都會在spring在初始化這個bean時,自動裝配這個屬性。要使 autowired能夠工...

spring常用註解

1 引入context命名空間 在spring的配置檔案中 配置檔案如下 xml 收藏 xmlns context spring context 2.5.xsd 開啟配置 spring 會自動掃瞄cn.pic包下面有註解的類,完成bean的裝配。xml xmlns xmlns xsi xmlns c...