Spring詳解使用注解開發(fā)流程
在Spring4之后 要使用注解開發(fā) 必須保證aop包導(dǎo)入了
使用注解需要導(dǎo)入context約束 增加 注解的支持
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟注解的支持--> <context:annotation-config/> </beans>
@Component:組件放在類上 說明這個類被Spring管理了 就是bean
import org.springframework.stereotype.Component; //等價于<bean id="user" class="com.kero.pojo.User"/> @Component public class User { public String name = "xxx"; }
@Value
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; //等價于<bean id="user" class="com.kero.pojo.User"/> @Component public class User { @Value("xxx") //等價于<property name="name" value="xxx"/> public String name; }
或者
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; //等價于<bean id="user" class="com.kero.pojo.User"/> @Component public class User { public String name; @Value("xxx") public void setName(String name) { this.name = name; } }
@Component有幾個衍生的注解 我們在Web開發(fā)中會按照MVC三層架構(gòu)分層
·dao[@Repository]
·service[@Service]
·controller[@Controller]
這四個注解功能一樣 都是代表將某個類注冊到Spring中 裝配Bean
注解的作用域@Scope
@Scope 放在類上,默認(rèn)是單例模式
@Scope(prototype)是原型模式,每次創(chuàng)建的都是一個新的對象
其作用等價于
補(bǔ)充:
@Scope("singleton") 或者@Scope 單例模式 下面代碼輸出結(jié)果為true
@Scope("prototype")下面代碼輸出結(jié)果為false
import com.kero.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = context.getBean("user", User.class); User user2 = context.getBean("user", User.class); System.out.println(user==user2); } }
xml vs 注解
·xml更加萬能 適用于任何場合 維護(hù)簡單方便
·注解 不是自己類使用不聊 維護(hù)相對復(fù)雜
最佳實(shí)踐:xml用來管理bean
注解只負(fù)責(zé)完成屬性的注入
我們在使用的過程中 需要注意 使用以下代碼
<!--指定要掃描的包 這個包下的注解就會生效->--> <context:component-scan base-package="com.kero"/> <!--開啟注解的支持--> <context:annotation-config/>
針對最佳實(shí)踐的例子
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--指定要掃描的包 這個包下的注解就會生效->--> <context:component-scan base-package="com.kero"/> <!--開啟注解的支持--> <context:annotation-config/> <bean id="user" class="com.kero.pojo.User" scope="prototype"/> </beans>
import org.springframework.beans.factory.annotation.Value; public class User { @Value("XXX") public String name; public void setName(String name) { this.name = name; } }
到此這篇關(guān)于Spring詳解使用注解開發(fā)流程的文章就介紹到這了,更多相關(guān)Spring注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring schedule配置多任務(wù)動態(tài)cron(增刪啟停)
這篇文章主要介紹了spring schedule配置多任務(wù)動態(tài)cron(增刪啟停),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringBoot整合SQLite數(shù)據(jù)庫全過程
sqlite是一個很輕量級的數(shù)據(jù)庫,可以滿足日常sql的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合SQLite數(shù)據(jù)庫的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03如何使用axis調(diào)用WebService及Java?WebService調(diào)用工具類
Axis是一個基于Java的Web服務(wù)框架,可以用來調(diào)用Web服務(wù)接口,下面這篇文章主要給大家介紹了關(guān)于如何使用axis調(diào)用WebService及Java?WebService調(diào)用工具類的相關(guān)資料,需要的朋友可以參考下2023-04-04SpringBoot整合Hbase的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot整合Hbase的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12淺析Java 數(shù)據(jù)結(jié)構(gòu)常用接口與類
本篇文章主要介紹了Java中的數(shù)據(jù)結(jié)構(gòu),Java工具包提供了強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)。需要的朋友可以參考下2017-04-04Java實(shí)現(xiàn)基于token認(rèn)證的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)基于token認(rèn)證的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08基于logback 實(shí)現(xiàn)springboot超級詳細(xì)的日志配置
java web 下有好幾種日志框架,比如:logback,log4j,log4j2(slj4f 并不是一種日志框架,它相當(dāng)于定義了規(guī)范,實(shí)現(xiàn)了這個規(guī)范的日志框架就能夠用 slj4f 調(diào)用)。這篇文章主要介紹了基于logback springboot超級詳細(xì)的日志配置,需要的朋友可以參考下2019-06-06