search
HomeJavajavaTutorialExample analysis of read and write operations in Java Spring

    Storing bean objects

    When we stored bean objects before, we needed to add a line of beans to the configuration file to inject them

    Example analysis of read and write operations in Java Spring

    This injection method is not good. If there is an error in the configuration file, the program will not display the error message. This is very unfriendly to beginners.

    1. Create a project

    Create startup class and main method

    Example analysis of read and write operations in Java Spring

    Introduce dependencies

    Example analysis of read and write operations in Java Spring

    <dependencies>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

    Create configuration file

    We agreed that the bean object to be created will be placed under the com.bean package:

    Example analysis of read and write operations in Java Spring

    <dependencies>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

    2. Add annotations to store beans in the container

    The core of using Spring is to use annotations. There are 5 annotations we have learned.

    @Controller [Controller] @Service [Service] @Repository [Warehouse] @Configuration [Configuration/Layout] @Component [Component] 2.1 controller

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    We were able to get the object by passing in the id parameter before, but that was adding a line of beans to manually add the id, but now that we use annotations, can we only use class types? Get it?

    When we use annotations to inject bean objects, the id actually defaults to the camel case form of the class name:

    Example analysis of read and write operations in Java Spring

    The reason will not be explained first. , will be discussed later.

    Easy mistakes:
    We previously added the root directory for bean object search in the configuration file, which means that the search will start from this directory downwards, but not upwards :

    Lower-level directory

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    ##Superior directory

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring##2.2 Service

    Example analysis of read and write operations in Java Spring

    #2.3 RepositoryExample analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    ##2.4 Configuration

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring##2.5 Component

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring2.6 Why are there 5 major categories of annotations

    Example analysis of read and write operations in Java Spring

    in the company Everyone is responsible for different areas, so the class annotations used are also different, but the essence of the five major categories of annotations is the same.

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java SpringWe can clearly see through the two class annotations that the other four annotations are methods that introduce the Component annotation. In other words, Component is the father of the other four class annotations. So what does Component do? It’s just that it’s not easy to throw it into the other four classes when annotating it~

    2.7 Why does the id default to a small camel numberExample analysis of read and write operations in Java Spring

    We use global search:

    Example analysis of read and write operations in Java Spring

    Find AnnotationBeanNameGenerator:

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    ##Test:

    Example analysis of read and write operations in Java Spring

    2.8 Using bean annotations
    Bean annotation is a method annotation, when used on a method , the returned object will also be injected by Spring, but it needs to be used with five major categories of annotations (any one will do)

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    id is the method name.

    2.8.1 Rename bean

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring##2.8.2 Rename multiple

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    Note

    : When renaming using the name attribute, the original method name will lose its effect, which means that only the modified one can be used The name is used as the ID. Get the bean object

    Getting the bean object is also called object injection. There are three methods:

      Property injection
    • Constructor injection
    • Setter injection
    • 1. Property injection

    1.1 Autowired

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring##Although there seems to be no problem now, when we Objects of the same type have been injected multiple times:

    Example analysis of read and write operations in Java Spring

    At this time, you need to accurately locate the selected object. Example analysis of read and write operations in Java SpringType name

    :

    ##1.2Resource

    Example analysis of read and write operations in Java Spring

    1.3 The difference between the two

    Example analysis of read and write operations in Java SpringResource has a name attribute, which can specify the name of the injected bean

    But Autowired also has a matching annotation, @Qualifier, which can also specify the selected bean object, so the two Well, I think it’s a tie

    Example analysis of read and write operations in Java Spring

    psExample analysis of read and write operations in Java Spring: The Autowired annotation initially queries all types for the types of objects that need to be injected. However, Autowired can only inject one object at a time, so an error will be reported when an object of the same type is injected multiple times. The Qualifier annotation essentially filters the results of its query.

    2. Constructor injection

    or use Autowired annotation:

    Example analysis of read and write operations in Java Spring

    ##ps

    : When there is only one constructor, Autowired can be omittedExample analysis of read and write operations in Java Spring

    but cannot be omitted when there are more than two constructors

    Example analysis of read and write operations in Java Spring

    Setter injection

    or through Autowired

    Example analysis of read and write operations in Java Spring

    Example analysis of read and write operations in Java Spring

    ## Resource also supports Setter injection.

    Classic interview questions

    What are the differences between property injection, constructor injection, and Setter injection?

    Example analysis of read and write operations in Java Spring

    The above is the detailed content of Example analysis of read and write operations in Java Spring. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    Java Spring怎么实现定时任务Java Spring怎么实现定时任务May 24, 2023 pm 01:28 PM

    java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

    Java axios与spring前后端分离传参规范是什么Java axios与spring前后端分离传参规范是什么May 03, 2023 pm 09:55 PM

    一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

    Spring Boot与Spring Cloud的区别与联系Spring Boot与Spring Cloud的区别与联系Jun 22, 2023 pm 06:25 PM

    SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

    Spring 最常用的 7 大类注解,史上最强整理!Spring 最常用的 7 大类注解,史上最强整理!Jul 26, 2023 pm 04:38 PM

    随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

    从零开始学Spring Cloud从零开始学Spring CloudJun 22, 2023 am 08:11 AM

    作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

    Java Spring框架创建项目与Bean的存储与读取实例分析Java Spring框架创建项目与Bean的存储与读取实例分析May 12, 2023 am 08:40 AM

    1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

    Java Spring Bean生命周期管理的示例分析Java Spring Bean生命周期管理的示例分析Apr 18, 2023 am 09:13 AM

    SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

    spring设计模式有哪些spring设计模式有哪些Dec 29, 2023 pm 03:42 PM

    spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment