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

Spring如何更簡單的讀取和存儲對象

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

前言

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

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

一、存儲Bean對象

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

2999347187d74d918482ae57d3cca963.png

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

二、配置掃描路徑

那么我們這里可以新建一個項目來演示,取名為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 目錄下新建一個文件,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 存對象的注解才能生效) -->
    <content:component-scan base-package="com"></content:component-scan>
</beans>

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

三、添加注解存儲 Bean 對象

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

為什么需要五大類注解?

d8ff6dd2da8f478790c8d7e4e26ff811.png

在線演示五大注解添加存儲 Bean 對象;

一、Controller

首先在com包下面新建一個類,這里我取的名字是"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,否則會報錯的;

3、在啟動類App中將對象讀取出來;

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

運行結(jié)果:

35993469122346cdb84b631992e72d71.png

二、Service

同理,在com包下新建一個類,UserService;

UserService里代碼;

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

啟動類App讀取對象;

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

運行結(jié)果:

7933d25a63d14e69a650cb4dad0ee565.png

三、 Repository

com包下新建一個類,名為UserRepository;

UserRepository類中代碼段:

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

啟動類App中代碼段:

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

運行結(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");
    }
}

啟動類App中代碼段:

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

運行結(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");
    }
}

啟動類App中對應(yīng)代碼:

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

運行結(jié)果:

20042da5a70443c283fd1e01e0e571d0.png

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

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

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

d2f12f36b7d64d08b289b938c50b01ae.png

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

a04023eca791404b81c7e2746b843b47.png

2、我們再查看一下Service的源碼;

2b0a7e618d5b40e0bc3a01058208849d.png

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

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

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

1、首先找到全局搜索框

f46bc49993784e69b401d8efd322b67a.png

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

60a32c4c005e4f9ab0fc11aafbac0ad7.png

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

29e8cc094af64ad59083a16d3eff1389.png

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

12eaddaff4c94eab944a01a2312a1436.png

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

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

總結(jié)

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

相關(guān)文章

  • springboot排除某些自動配置的操作方法

    springboot排除某些自動配置的操作方法

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

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

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

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

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

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

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

    如何在java中使用Jython

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

    Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作

    這篇文章主要介紹了Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • mybatis一對多兩種mapper寫法實例

    mybatis一對多兩種mapper寫法實例

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

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

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

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

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

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

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

最新評論