Java之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"> <!--指定要掃描的包,這個(gè)包下的注解就會(huì)生效--> <context:component-scan base-package="com.example.springannotation" /> <context:annotation-config></context:annotation-config> <!-- <bean id="cat" class="com.example.springannotation.dao.Cat"/> <bean id="people" class="com.example.springannotation.dao.People"/>--> </beans>
注解的支持:
//@Component 等價(jià)于<bean id="pepople" class="com.example.springannotation.dao.People" /> @Component public class People { @Autowired(required = false) @Value("1235") //相當(dāng)<property name="id " value="1235"/> private int id; @Autowired(required = false) private String name ="ming"; @Value("qing") //相當(dāng)<property name="name " value="qing"/> public void setName(@Nullable String name) { this.name = name; } }
衍生的注解
@Component有幾個(gè)衍生注解,我們?cè)趙eb開發(fā)中,會(huì)按照mvc三層架構(gòu)分層!
- dao 【@Repository】
- service【@Service】
- controller【@Controler】
這四個(gè)注解功能都是一樣的,都是代表將某個(gè)類注冊(cè)到Spring中,裝配Bean。
@Scope("singleton") //singleton:標(biāo)識(shí)單例模式,prototype:標(biāo)識(shí)原型模式 、request:標(biāo)識(shí)請(qǐng)求模式、session:標(biāo)識(shí)會(huì)話模式
xml 與注解:
- xml更加萬(wàn)能,適用于任何場(chǎng)合!維護(hù)簡(jiǎn)單方便。注解不是自己類使用不了,維護(hù)相對(duì)復(fù)雜!
xml與注解最佳實(shí)踐:
- xml 用來(lái)管理bean;
- 注解只負(fù)責(zé)完成屬性的注入;
- 我們?cè)谑褂玫倪^(guò)程中,只需要注意一個(gè)問題:必須讓注解生效,就需要開啟注解的支持
<!--指定要掃描的包,這個(gè)包下的注解就會(huì)生效--> <context:component-scan base-package="com.example.springannotation" /> <context:annotation-config></context:annotation-config>
JAVA的方式配置Spring
- @Configuration 這個(gè)也會(huì)spring容器托管,注冊(cè)到容器中,因?yàn)樗緛?lái)就是一個(gè)Component,
- @Configuration代表這是一個(gè)配置類,就和我們之前看的beans.xml
// 配置類 代替 beans.xml import com.example.springannotation.dao.Cat; import com.example.springannotation.dao.People; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @ComponentScan("com.example.springannotation") @Import(WwConfig.class) //引入第二個(gè)配置 public class AppConfig { //注朋一個(gè)bean 相當(dāng)于當(dāng)于我們之前寫的一個(gè)bean標(biāo)簽 //這個(gè)方法的名字,就相當(dāng)于bean標(biāo)簽中的id屬性 //這個(gè)方法的返回價(jià),就和當(dāng)了bean標(biāo)簽中的class屬性 @Bean public People getPeople(){ return new People(); } @Bean public Cat getCat(){ return new Cat(); } } import org.springframework.context.annotation.Configuration; @Configuration public class WwConfig { }
//測(cè)試類 import com.example.springannotation.config.AppConfig; import com.example.springannotation.dao.People; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @SpringBootTest class SpringannotationApplicationTests { @Test void contextLoads() { 如果完全使用了配置類方式做, // 我們就只能通過(guò) AnnotationConfig 上下文來(lái)獲取容器,通過(guò)配置類的class對(duì)象加載! ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); People people = (People) context.getBean("getPeople"); System.out.println(people.toString()); } }
到此這篇關(guān)于Java之Spring注解開發(fā)案例詳解的文章就介紹到這了,更多相關(guān)Java之Spring注解開發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA使用commos-fileupload實(shí)現(xiàn)文件上傳與下載實(shí)例解析
這篇文章主要介紹了JAVA使用commos-fileupload實(shí)現(xiàn)文件上傳與下載的相關(guān)資料,需要的朋友可以參考下2016-02-02詳解使用Java代碼讀取并比較本地兩個(gè)txt文件區(qū)別
這篇文章主要為大家介紹了使用Java代碼讀取并比較本地兩個(gè)txt文件區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07基于java springboot + mybatis實(shí)現(xiàn)電影售票管理系統(tǒng)
這篇文章主要介紹了基于java springboot + mybatis實(shí)現(xiàn)的完整電影售票管理系統(tǒng)基于java springboot + mybatis,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08JAVA為什么要使用封裝及如何封裝經(jīng)典實(shí)例
這篇文章主要給大家介紹了關(guān)于JAVA為什么要使用封裝及如何封裝的相關(guān)資料,封裝就是將屬性私有化,提供公有的方法訪問私有屬性,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10java計(jì)算方差、標(biāo)準(zhǔn)差(均方差)實(shí)例代碼
在本篇文章里小編給大家分享了關(guān)于java計(jì)算方差、標(biāo)準(zhǔn)差(均方差)實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考下。2019-08-08Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能
這篇文章主要介紹了Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10