Code Ease Code Ease
  • 个人博客网站 (opens new window)
  • 好用的工具网站 (opens new window)
  • Java核心基础
  • 框架的艺术
  • 分布式与微服务
  • 开发经验大全
  • 设计模式
  • 版本新特性
数据库系列
大数据+AI
  • xxl-job
运维与Linux
  • 基于SpringBoot和BootStrap的论坛网址
  • 基于VuePress的个人博客网站
  • 基于SpringBoot开发的小功能
  • 做一个自己的IDEA插件
程序人生
关于我
  • 分类
  • 标签
  • 归档

神秘的鱼仔

你会累是因为你在走上坡路
  • 个人博客网站 (opens new window)
  • 好用的工具网站 (opens new window)
  • Java核心基础
  • 框架的艺术
  • 分布式与微服务
  • 开发经验大全
  • 设计模式
  • 版本新特性
数据库系列
大数据+AI
  • xxl-job
运维与Linux
  • 基于SpringBoot和BootStrap的论坛网址
  • 基于VuePress的个人博客网站
  • 基于SpringBoot开发的小功能
  • 做一个自己的IDEA插件
程序人生
关于我
  • 分类
  • 标签
  • 归档
服务器
  • Java核心基础

  • 框架的艺术

    • Spring

      • 重新带你走进Spring
      • 控制反转(IOC)和依赖注入(DI)的完美实现
      • 关于Spring中的Bean,一文搞定
      • Spring5竟然可以彻底抛弃xml配置
        • (一)概述
        • (二)如何使用注解开发
        • (三)更多的注解
        • (四)彻底抛弃xml配置文件
      • 通俗易懂的AOP切面详解
      • 一文搞定Spring整合Mybatis
      • 事务Transactional注解的参数与失效场景分析
      • 写了两年代码之后再来看看Spring中的Bean
      • 这次终于把Spring的监听器讲明白了
      • 你真的了解Maven吗?
      • 正式发布的Spring AI,能让Java喝上AI赛道的汤吗
    • Mybatis

    • SpringBoot

    • MQ

    • Zookeeper

    • netty

  • 分布式与微服务

  • 开发经验大全

  • 版本新特性

  • Java
  • 框架的艺术
  • Spring
CodeEase
2023-09-20
目录

Spring5竟然可以彻底抛弃xml配置

作者:鱼仔
博客首页: codeease.top (opens new window)
公众号:Java鱼仔

# (一)概述

前面看了这么多,不知道大家有没有这样一种感觉,如果每写一个对象都要去xml文件中定义这个bean,似乎依然还是很繁琐。Spring也考虑到了,因此在后续的Spring版本中,慢慢地开始用注解去代理xml文件配置。

# (二)如何使用注解开发

首先肯定要把Spring系列的依赖先导入,前面也介绍了,只需要导入spring-webmvc的依赖,他就会自动将其他的依赖导入:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.9.RELEASE</version>
</dependency>
1
2
3
4
5
6

接着在xml文件中开启注解:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描某个包,这个包下的注解才会生效-->
    <context:component-scan base-package="com.javayz.pojo"/>
    <context:annotation-config/>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13

然后就开始使用吧! 在pojo包下新建一个普通的实体类叫User

@Component
public class User {
    @Value("javayz")
    private String name;
    //省略get、set、toString方法
}
1
2
3
4
5
6

这里用到了两个注解,一个叫Component,这个注解相当于:

<bean id="user" class="com.javayz.pojo.User"/>
1

第二个注解叫Value,这个注解相当于:

<property name="name" value="javayz"/>
1

最后来测试一下:

public class Test {
    @org.junit.Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User  user = (User) context.getBean("user");
        System.out.println(user);
    }
}
1
2
3
4
5
6
7
8

# (三)更多的注解

@Component将一个类注入到Spring容器中,在实际的开发中,我们会用几个功能相同的注解来申明不同功能的类,比如:

当申明一个实体类时,我们会用@Repository

申明Service层的类时,用@Service

申明Controller层的类时,用@Controller

但是他们的功能都是一样的。

在配置文件中,我们通过scope来指定Bean的作用域,我们可以类上直接使用注解@Scope("singleton") 实现。

# (四)彻底抛弃xml配置文件

在Spring5中,我们甚至可以不用去写xml配置文件,直接使用@Configuration注解完成和配置文件一样的功能。

在上面项目的基础上,删除xml配置文件,新建一个config包,并在这个包下新建一个SpringConfig,这个类的核心是@Configuration注解.

@Configuration
@ComponentScan("com.javayz.pojo")
public class SpringConfig {

    @Bean(name = "user")
    public User getUser(){
        return new User();
    }
}
1
2
3
4
5
6
7
8
9

这段的代码和上面配置文件实现的功能完全一致。我最早学习Spring时还没有不写xml的方式,从这里也能看出Spring也在一步步减少繁琐的配置项。

测试一下,这里需要用到AnnotationConfigApplicationContext来获取配置类

@org.junit.Test
public void test2(){
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    User user = (User) context.getBean("user");
    System.out.println(user);
}
1
2
3
4
5
6
上次更新: 2025/02/18, 11:30:08
关于Spring中的Bean,一文搞定
通俗易懂的AOP切面详解

← 关于Spring中的Bean,一文搞定 通俗易懂的AOP切面详解→

最近更新
01
AI大模型部署指南
02-18
02
半个月了,DeepSeek为什么还是服务不可用
02-13
03
Python3.9及3.10安装文档
01-23
更多文章>
Theme by Vdoing | Copyright © 2023-2025 备案图标 浙公网安备33021202002405 | 浙ICP备2023040452号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式