最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring如何更簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象

 更新時(shí)間:2023年06月02日 10:50:51   作者:小浪學(xué)編程  
這篇文章主要給大家介紹了關(guān)于Spring如何更簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象的相關(guān)資料,在Spring 中想要更簡(jiǎn)單的存儲(chǔ)和讀取對(duì)象的核?是使?注解,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下

前言

上篇博客我們介紹了如何創(chuàng)建一個(gè)spring項(xiàng)目,并且如何的存、取對(duì)象,介紹了相關(guān)方法,那么本篇博客將接著上篇博客的內(nèi)容介紹如何更加簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象。

??在 Spring 中想要更簡(jiǎn)單的存儲(chǔ)和讀取對(duì)象的核?是使?注解,也就是我們接下來要學(xué)習(xí) Spring 中的相關(guān)注解,來存儲(chǔ)和讀取 Bean 對(duì)象。

一、存儲(chǔ)Bean對(duì)象

1、之前我們存儲(chǔ)Bean時(shí),需要在spring-config中添加一行bean注冊(cè)內(nèi)容才行;如下圖:

2999347187d74d918482ae57d3cca963.png

而現(xiàn)在我們需要一個(gè)簡(jiǎn)單的注解即可完成;

二、配置掃描路徑

那么我們這里可以新建一個(gè)項(xiàng)目來演示,取名為spring-2;

4502c035d21d4a97a6a87c064df30011.png

1、首先依然是配置pom.xml文件,添加spring框架支持;

f39391671b8645c6a96dafd40a0c9584.png

 添加以下代碼;

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.24</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.24</version>
        </dependency>
    </dependencies>

2、在resources 目錄下新建一個(gè)文件,spring-config.xml;

b3c1c2ca579d40cd8209ba5af82a46ae.png

添加以下配置代碼;

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置 Spring 掃描的根路徑(此根路徑下的所有 Spring 存對(duì)象的注解才能生效) -->
    <content:component-scan base-package="com"></content:component-scan>
</beans>

注意注釋里面的內(nèi)容,base-package后面的路徑要正確;

三、添加注解存儲(chǔ) Bean 對(duì)象

1、類注解:包含以下五種:@Controller(控制器)、@Service(服務(wù))、@Repository(倉庫)、@Component(組件)、@Configuration(配置)。

為什么需要五大類注解?

d8ff6dd2da8f478790c8d7e4e26ff811.png

在線演示五大注解添加存儲(chǔ) Bean 對(duì)象;

一、Controller

首先在com包下面新建一個(gè)類,這里我取的名字是"UserController"。

67c3f691449e4107a35ff4908f1c279b.png

2、UserController里面的代碼;

package com;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
 
@Controller
public class UserController {
    public void sayHi(){
        System.out.println("hello UserController");
    }
}

 注意千萬不要遺漏注解@Controller,否則會(huì)報(bào)錯(cuò)的;

3、在啟動(dòng)類App中將對(duì)象讀取出來;

ApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
        UserController userController = context.getBean(UserController.class);
        userController.sayHi();

運(yùn)行結(jié)果:

35993469122346cdb84b631992e72d71.png

二、Service

同理,在com包下新建一個(gè)類,UserService;

UserService里代碼;

package com;
 
import org.springframework.stereotype.Service;
 
@Service
public class UserService {
    public void sayHi(){
        System.out.println("hello UserService");
    }
}

啟動(dòng)類App讀取對(duì)象;

ApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
        UserService userService = context.getBean(UserService.class);
        userService.sayHi();

運(yùn)行結(jié)果:

7933d25a63d14e69a650cb4dad0ee565.png

三、 Repository

com包下新建一個(gè)類,名為UserRepository;

UserRepository類中代碼段:

package com;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
    public void sayHi(){
        System.out.println("hello UserRepository");
    }
}

啟動(dòng)類App中代碼段:

ApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
        UserRepository userRepository = context.getBean(UserRepository.class);
        userRepository.sayHi();

運(yùn)行結(jié)果:

0b365d05ab554575be1ad23f3a2f235f.png

四、Configuration

在com包下新建類,名為UserConfiguration;

UserConfiguration類中代碼段:

package com;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfiguration {
    public void sayHi(){
        System.out.println("hello UserConfiguration");
    }
}

啟動(dòng)類App中代碼段:

ApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
        UserConfiguration userConfiguration = context.getBean(UserConfiguration.class);
        userConfiguration.sayHi();

運(yùn)行結(jié)果:

acaa2cc86ebb42238aa874d99e3b63d9.png

五、Component

在com包下新建類,名為UserCompenent;

package com;
import org.springframework.stereotype.Component;
@Component
public class UserComponent {
    public void sayHi(){
        System.out.println("hello UserComponent");
    }
}

啟動(dòng)類App中對(duì)應(yīng)代碼:

ApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
        UserComponent userComponent = context.getBean(UserComponent.class);
        userComponent.sayHi();

運(yùn)行結(jié)果:

20042da5a70443c283fd1e01e0e571d0.png

五大類注解之間有什么關(guān)系?

想搞清楚這個(gè)問題我們可以去查看一下每個(gè)注解對(duì)應(yīng)的源碼,直接按住Ctrl+鼠標(biāo)左鍵即可前往對(duì)應(yīng)注解的源碼;

1、比如我們這里先查看一下Controller的源碼;

d2f12f36b7d64d08b289b938c50b01ae.png

進(jìn)來之后我們發(fā)現(xiàn)Controller的實(shí)現(xiàn)也用到了Component;

a04023eca791404b81c7e2746b843b47.png

2、我們?cè)俨榭匆幌耂ervice的源碼;

2b0a7e618d5b40e0bc3a01058208849d.png

 同樣,進(jìn)來之后我們發(fā)現(xiàn)Service的實(shí)現(xiàn)也用到了Component;

所以可得到以下結(jié)論: @Controller、@Service、@Repository、@Configuration 都是基于 @Component 實(shí)現(xiàn)的,所以@Component 可以認(rèn)為是其他 4 個(gè)注解的父類。

四、Spring使用五大類注解生成beanName的問題

1、首先找到全局搜索框

f46bc49993784e69b401d8efd322b67a.png

2、點(diǎn)擊之后輸入beanname,找到紅色箭頭指向的類,雙擊打開;

60a32c4c005e4f9ab0fc11aafbac0ad7.png

3、打開后往下拉,找到紅色框框中的方法,ctrl+鼠標(biāo)左鍵查看源碼;

29e8cc094af64ad59083a16d3eff1389.png

4、可以看到beanname的命名規(guī)則;

12eaddaff4c94eab944a01a2312a1436.png

??簡(jiǎn)單來說就是,類名中第一個(gè)字母為大寫,第二個(gè)字母也為大寫,那么beanname的名稱就是返回"原類名",否則將首字母轉(zhuǎn)換為小寫作為beanname返回;

??OK,今天的內(nèi)容就到這里啦,下篇博客繼續(xù)更新使用方法注解@Bean將對(duì)象更簡(jiǎn)單的存儲(chǔ)到容器中!!

總結(jié)

到此這篇關(guān)于Spring如何更簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象的文章就介紹到這了,更多相關(guān)Spring讀取和存儲(chǔ)對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Idea 配置國(guó)內(nèi) Maven 源的圖文教程

    Idea 配置國(guó)內(nèi) Maven 源的圖文教程

    這篇文章主要介紹了Idea 配置國(guó)內(nèi) Maven 源的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • SpringBoot項(xiàng)目中HTTP請(qǐng)求體只能讀一次的解決方案

    SpringBoot項(xiàng)目中HTTP請(qǐng)求體只能讀一次的解決方案

    在基于Spring開發(fā)Java項(xiàng)目時(shí),可能需要重復(fù)讀取HTTP請(qǐng)求體中的數(shù)據(jù),例如使用攔截器打印入?yún)⑿畔⒌?但當(dāng)我們重復(fù)調(diào)用getInputStream()或者getReader()時(shí),通常會(huì)遇到SpringBoot HTTP請(qǐng)求只讀一次的問題,本文給出了幾種解決方案,需要的朋友可以參考下
    2024-08-08
  • Java利用StampedLock實(shí)現(xiàn)讀寫鎖的方法詳解

    Java利用StampedLock實(shí)現(xiàn)讀寫鎖的方法詳解

    在jdk8以后,java提供了一個(gè)性能更優(yōu)越的讀寫鎖并發(fā)類StampedLock,該類的設(shè)計(jì)初衷是作為一個(gè)內(nèi)部工具類,用于輔助開發(fā)其它線程安全組件。本文就來和大家一起學(xué)習(xí)下StampedLock的功能和使用
    2022-10-10
  • 基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式

    基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式

    這篇文章主要介紹了基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • 實(shí)例講解Java并發(fā)編程之閉鎖

    實(shí)例講解Java并發(fā)編程之閉鎖

    這篇文章主要介紹了實(shí)例講解Java并發(fā)編程之閉鎖,閉鎖相當(dāng)于一扇門,在閉鎖到達(dá)結(jié)束狀態(tài)之前,這扇門一直是關(guān)閉著的,沒有任何線程可以通過,當(dāng)?shù)竭_(dá)結(jié)束狀態(tài)時(shí),這扇門才會(huì)打開并容許所有線程通過,需要的朋友可以參考下
    2015-04-04
  • 解決spring boot網(wǎng)關(guān)gateway導(dǎo)致的坑,無法下載文件問題

    解決spring boot網(wǎng)關(guān)gateway導(dǎo)致的坑,無法下載文件問題

    這篇文章主要介紹了解決spring boot網(wǎng)關(guān)gateway導(dǎo)致的坑,無法下載文件的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • mybatis 延遲加載的深入理解

    mybatis 延遲加載的深入理解

    這篇文章主要介紹了mybatis 延遲加載的深入理解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • java 中多線程生產(chǎn)者消費(fèi)者問題詳細(xì)介紹

    java 中多線程生產(chǎn)者消費(fèi)者問題詳細(xì)介紹

    這篇文章主要介紹了java 中多線程生產(chǎn)者消費(fèi)者問題詳細(xì)介紹的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • IDEA搭建Maven模塊化項(xiàng)目的實(shí)現(xiàn)

    IDEA搭建Maven模塊化項(xiàng)目的實(shí)現(xiàn)

    本文主要介紹了IDEA搭建Maven模塊化項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java實(shí)現(xiàn)將txt文件轉(zhuǎn)成xls文件的方法

    Java實(shí)現(xiàn)將txt文件轉(zhuǎn)成xls文件的方法

    今天小編就為大家分享一篇Java實(shí)現(xiàn)將txt文件轉(zhuǎn)成xls文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評(píng)論

襄汾县| 武隆县| 玉山县| 九台市| 化隆| 成都市| 当涂县| 昭平县| 长岭县| 特克斯县| 志丹县| 梅河口市| 龙山县| 阿鲁科尔沁旗| 巩留县| 徐州市| 凭祥市| 左贡县| 尖扎县| 达孜县| 芦山县| 沙洋县| 息烽县| 石门县| 清流县| 灵丘县| 阿合奇县| 碌曲县| 剑河县| 桐庐县| 吐鲁番市| 太康县| 闻喜县| 波密县| 阜宁县| 石嘴山市| 郯城县| 双鸭山市| 张家界市| 尉犁县| 松阳县|