亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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ì)象的核?是使?注解,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下

前言

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

??在 Spring 中想要更簡(jiǎn)單的存儲(chǔ)和讀取對(duì)象的核?是使?注解,也就是我們接下來(lái)要學(xué)習(xí) Spring 中的相關(guān)注解,來(lái)存儲(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)目來(lái)演示,取名為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、類(lèi)注解:包含以下五種:@Controller(控制器)、@Service(服務(wù))、@Repository(倉(cāng)庫(kù))、@Component(組件)、@Configuration(配置)。

為什么需要五大類(lèi)注解?

d8ff6dd2da8f478790c8d7e4e26ff811.png

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

一、Controller

首先在com包下面新建一個(gè)類(lèi),這里我取的名字是"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");
    }
}

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

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

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

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

35993469122346cdb84b631992e72d71.png

二、Service

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

UserService里代碼;

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

啟動(dòng)類(lèi)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è)類(lèi),名為UserRepository;

UserRepository類(lèi)中代碼段:

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

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

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

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

0b365d05ab554575be1ad23f3a2f235f.png

四、Configuration

在com包下新建類(lèi),名為UserConfiguration;

UserConfiguration類(lèi)中代碼段:

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

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

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

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

acaa2cc86ebb42238aa874d99e3b63d9.png

五、Component

在com包下新建類(lèi),名為UserCompenent;

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

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

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

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

20042da5a70443c283fd1e01e0e571d0.png

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

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

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

d2f12f36b7d64d08b289b938c50b01ae.png

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

a04023eca791404b81c7e2746b843b47.png

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

2b0a7e618d5b40e0bc3a01058208849d.png

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

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

四、Spring使用五大類(lèi)注解生成beanName的問(wèn)題

1、首先找到全局搜索框

f46bc49993784e69b401d8efd322b67a.png

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

60a32c4c005e4f9ab0fc11aafbac0ad7.png

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

29e8cc094af64ad59083a16d3eff1389.png

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

12eaddaff4c94eab944a01a2312a1436.png

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

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

總結(jié)

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

相關(guān)文章

  • springboot排除某些自動(dòng)配置的操作方法

    springboot排除某些自動(dòng)配置的操作方法

    Spring Boot 提供的自動(dòng)配置非常強(qiáng)大,某些情況下,自動(dòng)配置的功能可能不符合我們的需求,需要我們自定義配置,這個(gè)時(shí)候就需要排除/禁用Spring Boot 某些類(lèi)的自動(dòng)化配置了,本文給大家介紹springboot排除某些自動(dòng)配置的方法,感興趣的朋友一起看看吧
    2023-08-08
  • Java為什么占用四個(gè)字節(jié)你知道嗎

    Java為什么占用四個(gè)字節(jié)你知道嗎

    這篇文章主要介紹了Java為什么占四個(gè)字節(jié),文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Mybatis的dao層,service層的封裝方式

    Mybatis的dao層,service層的封裝方式

    這篇文章主要介紹了Mybatis的dao層,service層的封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • servlet監(jiān)聽(tīng)器的學(xué)習(xí)使用(三)

    servlet監(jiān)聽(tīng)器的學(xué)習(xí)使用(三)

    這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽(tīng)器學(xué)習(xí)使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 如何在java中使用Jython

    如何在java中使用Jython

    這篇文章主要介紹了如何在java中使用Jython,由于項(xiàng)目中需要用到Java調(diào)用Python的腳本,來(lái)實(shí)現(xiàn)一些功能,就對(duì)jython做了一些了解,通過(guò)jython可以實(shí)現(xiàn)java對(duì)python腳本的調(diào)用,需要的朋友可以參考一下
    2022-03-03
  • Java反射 JavaBean對(duì)象自動(dòng)生成插入,更新,刪除,查詢sql語(yǔ)句操作

    Java反射 JavaBean對(duì)象自動(dòng)生成插入,更新,刪除,查詢sql語(yǔ)句操作

    這篇文章主要介紹了Java反射 JavaBean對(duì)象自動(dòng)生成插入,更新,刪除,查詢sql語(yǔ)句操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • mybatis一對(duì)多兩種mapper寫(xiě)法實(shí)例

    mybatis一對(duì)多兩種mapper寫(xiě)法實(shí)例

    這篇文章主要介紹了mybatis一對(duì)多兩種mapper寫(xiě)法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Java 23種設(shè)計(jì)模型詳解

    Java 23種設(shè)計(jì)模型詳解

    本文主要介紹Java 23種設(shè)計(jì)模型,這里整理了詳細(xì)的資料,及實(shí)現(xiàn)各種設(shè)計(jì)模型的示例代碼,有需要的小伙伴可以參考下
    2016-09-09
  • Servlet會(huì)話技術(shù)基礎(chǔ)解析

    Servlet會(huì)話技術(shù)基礎(chǔ)解析

    這篇文章主要介紹了Servlet會(huì)話技術(shù)基礎(chǔ)解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Springboot jdbctemplate整合實(shí)現(xiàn)步驟解析

    Springboot jdbctemplate整合實(shí)現(xiàn)步驟解析

    這篇文章主要介紹了Springboot jdbctemplate整合實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評(píng)論