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ù)雜
最佳實踐:xml用來管理bean
注解只負(fù)責(zé)完成屬性的注入
我們在使用的過程中 需要注意 使用以下代碼
<!--指定要掃描的包 這個包下的注解就會生效->-->
<context:component-scan base-package="com.kero"/>
<!--開啟注解的支持-->
<context:annotation-config/>針對最佳實踐的例子
<?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?Boot實現(xiàn)web.xml功能示例詳解
這篇文章主要介紹了Spring?Boot實現(xiàn)web.xml功能,通過本文介紹我們了解到,在Spring Boot應(yīng)用中,我們可以通過注解和編程兩種方式實現(xiàn)web.xml的功能,包括如何創(chuàng)建及注冊Servlet、Filter以及Listener等,需要的朋友可以參考下2023-09-09
SpringCloud實現(xiàn)SSO 單點登錄的示例代碼
作為分布式項目,單點登錄是必不可少的,這篇文章主要介紹了SpringCloud實現(xiàn)SSO 單點登錄的示例代碼,非常具有實用價值,需要的朋友可以參考下2019-01-01
Spring?Boot循環(huán)依賴原理、解決方案與最佳實踐(全解析)
循環(huán)依賴指兩個或多個Bean相互直接或間接引用,形成閉環(huán)依賴關(guān)系,這篇文章主要介紹了Spring?Boot循環(huán)依賴原理、解決方案與最佳實踐(全解析),需要的朋友可以參考下2025-04-04
Java實戰(zhàn)之課程信息管理系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java實現(xiàn)課程信息管理系統(tǒng),文中采用到的技術(shù)有:Springboot、SpringMVC、MyBatis、FreeMarker等,感興趣的可以了解一下2022-04-04
詳解spring cloud使用Hystrix實現(xiàn)單個方法的fallback
本篇文章主要介紹了詳解spring cloud-使用Hystrix實現(xiàn)單個方法的fallback,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計劃任務(wù)
本篇文章主要介紹了詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計劃任務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03

