欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于spring web-mvc衍生注解

 更新時(shí)間:2022年08月19日 10:33:45   作者:深淺Java  
這篇文章主要介紹了關(guān)于spring web-mvc衍生注解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

衍生注解

在web開發(fā)中,會(huì)根據(jù)webmvc的三層加購分層,這時(shí)候每層都有相同一個(gè)不同的注解名字,但功能都相同。

  • Service層:@Service用于標(biāo)注業(yè)務(wù)層組件
  • controller層:@Controller用于標(biāo)注控制層組件
  • dao層:@Repository用于標(biāo)注數(shù)據(jù)訪問組件
  • 組件歸類層,名字可以隨便定義,例如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è)組件歸類層,文件樣式如下

dao和pojo

User類

import com.yc.dao.Userdao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//該注釋表示注冊到容器中,直接由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;
//該注釋表示注冊到容器中,直接由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)建測試類

ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
User u=context.getBean("user",User.class);
System.out.println(u);

輸出

User{name=‘Tom’, userdao=null}

最終輸出的只有User類,但是Userdao層次卻直接顯示空,問題是沒有把Userdao的bean引入到User的bean中,所以在測試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)建測試類,輸出

User{name=‘Tom’, userdao=Userdao{name=‘游泳’}}

@Component使用

@Component還可以加參數(shù),里面值相當(dāng)于注入bean中的id值,上面的案例默認(rèn)為User類的小寫user,如果設(shè)置為@Component(value = “user2”)或者@Component(“user2”),則將測試類改寫為

  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)文章

  • java貪吃蛇游戲?qū)崿F(xiàn)代碼

    java貪吃蛇游戲?qū)崿F(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了java貪吃蛇游戲?qū)崿F(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • zuul過濾器中轉(zhuǎn)發(fā)請求頭的解決方案

    zuul過濾器中轉(zhuǎn)發(fā)請求頭的解決方案

    這篇文章主要介紹了zuul過濾器中轉(zhuǎn)發(fā)請求頭的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java構(gòu)建對象常用3種方法解析

    Java構(gòu)建對象常用3種方法解析

    這篇文章主要介紹了Java構(gòu)建對象常用3種方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Springboot容器級后置處理器BeanDefinitionRegistryPostProcessor

    Springboot容器級后置處理器BeanDefinitionRegistryPostProcessor

    這篇文章主要介紹了Springboot容器級后置處理器BeanDefinitionRegistryPostProcessor,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • 關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄

    關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄

    這篇文章主要介紹了關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ)的方法

    SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ)的方法

    這篇文章主要介紹了SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)

    Java的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)屬性,下面我們就一起來看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:
    2016-06-06
  • Mybatis攔截器實(shí)現(xiàn)一種百萬級輕量分表方案

    Mybatis攔截器實(shí)現(xiàn)一種百萬級輕量分表方案

    這篇文章主要介紹了Mybatis攔截器實(shí)現(xiàn)一種百萬級輕量分表方案,需要的朋友可以參考下
    2024-02-02
  • Java多線Condition條件變量正確使用方法詳解

    Java多線Condition條件變量正確使用方法詳解

    這篇文章主要為大家,介紹了Java多線Condition條件變量正確使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • JAVA JDK8 List分組的實(shí)現(xiàn)和用法

    JAVA JDK8 List分組的實(shí)現(xiàn)和用法

    今天小編就為大家分享一篇關(guān)于JAVA JDK8 List分組的實(shí)現(xiàn)和用法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12

最新評論