關(guān)于spring web-mvc衍生注解
衍生注解
在web開(kāi)發(fā)中,會(huì)根據(jù)webmvc的三層加購(gòu)分層,這時(shí)候每層都有相同一個(gè)不同的注解名字,但功能都相同。
- Service層:@Service用于標(biāo)注業(yè)務(wù)層組件
- controller層:@Controller用于標(biāo)注控制層組件
- dao層:@Repository用于標(biāo)注數(shù)據(jù)訪問(wèn)組件
- 組件歸類層,名字可以隨便定義,例如User層,@Component可以進(jìn)行標(biāo)注
注解:使用了注解之后,在容器中不需要注入bean,但要指定查找的的項(xiàng)目。
容器配置
<?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"> <!--指定注入時(shí)需要查找的項(xiàng)目--> <context:component-scan base-package="com.yc.ch"/> </beans>
創(chuàng)建一個(gè)dao層和一個(gè)組件歸類層,文件樣式如下
User類
import com.yc.dao.Userdao; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; //該注釋表示注冊(cè)到容器中,直接由spring容器托管, @Component public class User { //表示設(shè)置name的參數(shù)值為為Tom //等價(jià)于 <property name="name" value="Tom"/> @Value("Tom") private String name; private Userdao userdao; public String getName() { return name; } public void setName(String name) { this.name = name; } public Userdao getUserdao() { return userdao; } public void setUserdao(Userdao userdao) { this.userdao = userdao; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", userdao=" + userdao + '}'; } }
Userdao類
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Repository; //該注釋表示注冊(cè)到容器中,直接由spring容器托管, @Repository public class Userdao { //表示設(shè)置name的參數(shù)值為為游泳 //等價(jià)于 <property name="name" value="游泳"/> @Value("游泳") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Userdao{" + "name='" + name + '\'' + '}'; } }
spring容器配置
<?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"> <!--表示查找到com.yc下的所有層--> <context:component-scan base-package="com.yc"/> </beans>
創(chuàng)建測(cè)試類
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); User u=context.getBean("user",User.class); System.out.println(u);
輸出
User{name=‘Tom’, userdao=null}
最終輸出的只有User類,但是Userdao層次卻直接顯示空,問(wèn)題是沒(méi)有把Userdao的bean引入到User的bean中,所以在測(cè)試User類時(shí)顯示空。解決:使用使用注解 @Autowired方式匹配到Userdao中
將User類修改
import com.yc.dao.Userdao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class User { @Value("Tom") private String name; @Autowired private Userdao userdao; public String getName() { return name; } public void setName(String name) { this.name = name; } public Userdao getUserdao() { return userdao; } public void setUserdao(Userdao userdao) { this.userdao = userdao; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", userdao=" + userdao + '}'; } }
修改spring容器配置
<?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.yc"/> <context:annotation-config/> </beans>
創(chuàng)建測(cè)試類,輸出
User{name=‘Tom’, userdao=Userdao{name=‘游泳’}}
@Component使用
@Component還可以加參數(shù),里面值相當(dāng)于注入bean中的id值,上面的案例默認(rèn)為User類的小寫user,如果設(shè)置為@Component(value = “user2”)或者@Component(“user2”),則將測(cè)試類改寫為
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); User u=context.getBean("user2",User.class); System.out.println(u);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
zuul過(guò)濾器中轉(zhuǎn)發(fā)請(qǐng)求頭的解決方案
這篇文章主要介紹了zuul過(guò)濾器中轉(zhuǎn)發(fā)請(qǐng)求頭的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Springboot容器級(jí)后置處理器BeanDefinitionRegistryPostProcessor
這篇文章主要介紹了Springboot容器級(jí)后置處理器BeanDefinitionRegistryPostProcessor,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄
這篇文章主要介紹了關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ)的方法
這篇文章主要介紹了SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
Hibernate想要實(shí)現(xiàn)雙向的關(guān)聯(lián)就必須在映射文件的兩端同時(shí)配置<one-to-one>,另外還要在主映射的一端采用foreign外鍵關(guān)聯(lián)屬性,下面我們就一起來(lái)看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:2016-06-06Mybatis攔截器實(shí)現(xiàn)一種百萬(wàn)級(jí)輕量分表方案
這篇文章主要介紹了Mybatis攔截器實(shí)現(xiàn)一種百萬(wàn)級(jí)輕量分表方案,需要的朋友可以參考下2024-02-02JAVA JDK8 List分組的實(shí)現(xiàn)和用法
今天小編就為大家分享一篇關(guān)于JAVA JDK8 List分組的實(shí)現(xiàn)和用法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12