【Spring框架全系列】Spring更简单的读取和存储对象

这篇具有很好参考价值的文章主要介绍了【Spring框架全系列】Spring更简单的读取和存储对象。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

📬📬哈喽,大家好,我是小浪。上篇博客我们介绍了如何创建一个spring项目,并且如何的存、取对象,介绍了相关方法,那么本篇博客将接着上篇博客的内容介绍如何更加简单的读取和存储对象。

🌃在 Spring 中想要更简单的存储和读取对象的核⼼是使⽤注解,也就是我们接下来要学习 Spring 中的相关注解,来存储和读取 Bean 对象。

📲目录

一、存储Bean对象

二、配置扫描路径

三、添加注解存储 Bean 对象

为什么需要五大类注解?

在线演示五大注解添加存储 Bean 对象;

一、Controller

二、Service

三、 Repository

四、Configuration

五、Component

五大类注解之间有什么关系?

四、Spring使用五大类注解生成beanName的问题


一、存储Bean对象

1、之前我们存储Bean时,需要在spring-config中添加一行bean注册内容才行;如下图:
【Spring框架全系列】Spring更简单的读取和存储对象

而现在我们需要一个简单的注解即可完成;

二、配置扫描路径

那么我们这里可以新建一个项目来演示,取名为spring-2;

【Spring框架全系列】Spring更简单的读取和存储对象

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

【Spring框架全系列】Spring更简单的读取和存储对象

 添加以下代码;

    <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;

【Spring框架全系列】Spring更简单的读取和存储对象

添加以下配置代码;

<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>

注意注释里面的内容,base-package后面的路径要正确;

三、添加注解存储 Bean 对象

1、类注解:包含以下五种:@Controller(控制器)、@Service(服务)、@Repository(仓库)、@Component(组件)、@Configuration(配置)。

为什么需要五大类注解?

【Spring框架全系列】Spring更简单的读取和存储对象

在线演示五大注解添加存储 Bean 对象;

一、Controller

首先在com包下面新建一个类,这里我取的名字是"UserController"。
【Spring框架全系列】Spring更简单的读取和存储对象

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();

运行结果:

【Spring框架全系列】Spring更简单的读取和存储对象

二、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();

运行结果:

【Spring框架全系列】Spring更简单的读取和存储对象

三、 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();

运行结果:

【Spring框架全系列】Spring更简单的读取和存储对象

四、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();

运行结果:

【Spring框架全系列】Spring更简单的读取和存储对象

五、Component

在com包下新建类,名为UserCompenent;

package com;

import org.springframework.stereotype.Component;

@Component
public class UserComponent {
    public void sayHi(){
        System.out.println("hello UserComponent");
    }
}

启动类App中对应代码:

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

运行结果:

【Spring框架全系列】Spring更简单的读取和存储对象

五大类注解之间有什么关系?

想搞清楚这个问题我们可以去查看一下每个注解对应的源码,直接按住Ctrl+鼠标左键即可前往对应注解的源码;

1、比如我们这里先查看一下Controller的源码;

【Spring框架全系列】Spring更简单的读取和存储对象

进来之后我们发现Controller的实现也用到了Component;

【Spring框架全系列】Spring更简单的读取和存储对象

2、我们再查看一下Service的源码;

【Spring框架全系列】Spring更简单的读取和存储对象

 同样,进来之后我们发现Service的实现也用到了Component;

所以可得到以下结论: @Controller、@Service、@Repository、@Configuration 都是基于 @Component 实现的,所以@Component 可以认为是其他 4 个注解的父类。

四、Spring使用五大类注解生成beanName的问题

1、首先找到全局搜索框

【Spring框架全系列】Spring更简单的读取和存储对象

2、点击之后输入beanname,找到红色箭头指向的类,双击打开;

【Spring框架全系列】Spring更简单的读取和存储对象

3、打开后往下拉,找到红色框框中的方法,ctrl+鼠标左键查看源码;

【Spring框架全系列】Spring更简单的读取和存储对象

4、可以看到beanname的命名规则;

【Spring框架全系列】Spring更简单的读取和存储对象

🎑简单来说就是,类名中第一个字母为大写,第二个字母也为大写,那么beanname的名称就是返回"原类名",否则将首字母转换为小写作为beanname返回;

💡OK,今天的内容就到这里啦,下篇博客继续更新使用方法注解@Bean将对象更简单的存储到容器中,感谢阅读,欢迎订阅本专栏!!

需要代写各种大作业,web前端,Java课程设计等,都可以添加下方VX,详细交流喔!🗃🗃文章来源地址https://www.toymoban.com/news/detail-448884.html

到了这里,关于【Spring框架全系列】Spring更简单的读取和存储对象的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring框架全系列】如何创建一个SpringBoot项目

    🌇哈喽,大家好,我是小浪。前几篇博客我们已经介绍了什么是Spring,以及如何创建一个Spring项目,OK,那么单单掌握Spring是完全不够的,Spring的家族体系十分强大,我们还需要深入学习,以便于我们后续达到能独立做项目的水平。今天我们来学习SpringBoot。🏜 📲目录 一、

    2024年02月03日
    浏览(39)
  • 【Spring框架全系列】SpringBoot配置文件相关操作

    🌇哈喽,大家好,我是小浪。上篇博客我们已经学习了如何创建一个Spring项目,那么创建Spirng项目还可以直接通过在Spring官网的方式来创建,做法也非常的简单,感兴趣的小伙伴可以在C站搜个教程尝试一下;那么,今天我们就来学习SpringBoot如何配置文件;💡💡 📲目录 一

    2024年02月05日
    浏览(38)
  • Spring系列3 -- 更简单的读取和存储对象

             上一篇章总结了,Spring的创建与使用,通过创建Maven项目配置Spring的环境依赖,创建Spring框架的项目,然后通过在Resource目录下创建Spring-config.xml配置文件,添加bean/bean标签将我们需要的bean对象注入到容器中,然后通过ApplicationContext获取Spring上下文,使用getBean()方法获取be

    2024年02月13日
    浏览(28)
  • Spring 更简单的读取和存储对象

    在 Spring 中要想更简单的存储和读取对象 , 核心是 使用注解 , 所以我们需要通过 Spring 中相关注解 , 来存储和读取 Bean 对象. 之前我们存储 Bean 时 , 需要在 spring-config.xml 中添加一行注释才行: 而现在我们只需一个注解就可以替代之前要写一行配置 , 不过在存储对象之前 , 我们先

    2024年02月02日
    浏览(31)
  • 5-Spring更简单的读取和存储对象

    目录 1.存储Bean对象 1.1.前置工作:在配置文件中设置bean扫描的根路径(重要) 1.2.添加注解存储Bean对象到Spring中 1.2.1.类注解(添加到某个类上,将当前的类存储到Spring中):@Controller,@Service,@Repository,@Component,@Configuration 关于类注解的bean的命名规则: PS:为什么要这么多类

    2023年04月25日
    浏览(35)
  • Spring 更简单的读取和存储对象、使用注解存取对象

    我们知道物件都会随着时代的发展,越变越简单的。Spring 框架也不意外;我们在之前存储Bean对象是在配置文件中添加 bean/bean 来存入Bean对象到Spring当中的,使用那个类对象就要存入那个,一个两个类对象还好,如果需要的对象多了,就会非常麻烦。现在,随着 Spring 发展不用

    2024年02月11日
    浏览(37)
  • Spring更简单的存储和读取Bean对象

    目录 1.第一个Spring项目 2.存储Bean对象 2.1 准备工作 2.2 五大类注解 2.3 方法注解@Bean 2.4 Bean对象的默认命名规则 3. 读取Bean对象 3.1 属性注入 3.2 setter注入 3.3 构造方法注入 3.4 注入异常问题 3.5 注入方式优缺点 3.6 @Autowired和@Resource的区别 在学习更简单的方式来读取和存储Bean对象

    2024年02月03日
    浏览(38)
  • 【JavaEE进阶】Spring 更简单的读取和存储对象

    配置扫描路径是使用注解之前的前置工作,是非常重要的,是必须的操作项.只有被配置的包下的所有类,添加了注解才能被正确的识别并保存到 Spring 中. 首先创建一个Spring项目.创建好后,第一步就是配置扫描路径:在 resources 目录中创建一个 spring-config.xml 文件.然后在 spring-config.

    2024年02月12日
    浏览(31)
  • 【Spring】更简单的读取和存储对象,五大类注解

    经过前面的学习,我们已经可以实现基本的 Spring 读取和存储对象的操作了,但在操作的过程中我们发现读取和存储对象并没有想象中的那么 “简单”,所以接下来我们要学习更加简单的操作 Bean 对象的方法 在 Spring 中想要 更简单的存储和读取对象的核心是使用注解 ,也就

    2024年02月15日
    浏览(25)
  • JavaEE 突击 5 - Spring 更简单的读取和存储对象(2)

    大家新年快乐 , 祝大家新的一年如图 这个专栏给大家介绍一下 Java 家族的核心产品 - SSM 框架 JavaEE 进阶专栏 Java 语言能走到现在 , 仍然屹立不衰的原因 , 有一部分就是因为 SSM 框架的存在 接下来 , 博主会带大家了解一下 Spring、Spring Boot、Spring MVC、MyBatis 相关知识点 并且带领

    2024年02月19日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包