【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介:

【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件

写在前面
在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对!另外,每个项目编写大量的XML文件来配置Spring,也大大增加了项目维护的复杂度,往往很多个项目的Spring XML文件的配置大部分是相同的,只有很少量的配置不同,这也造成了配置文件上的冗余。

项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation

Spring IOC和DI
在Spring容器的底层,最重要的功能就是IOC和DI,也就是控制反转和依赖注入。

IOC:控制反转,将类的对象的创建交给Spring类管理创建。
DI:依赖注入,将类里面的属性在创建类的过程中给属性赋值。
DI和IOC的关系:DI不能单独存在,DI需要在IOC的基础上来完成。

在Spring内部,所有的组件都会放到IOC容器中,组件之间的关系通过IOC容器来自动装配,也就是我们所说的依赖注入。接下来,我们就使用注解的方式来完成容器组件的注册、管理及依赖、注入等功能。

在介绍使用注解完成容器组件的注册、管理及依赖、注入等功能之前,我们先来看看使用XML文件是如何注入Bean的。

通过XML文件注入JavaBean
首先,我们在工程的io.mykit.spring.bean包下创建Person类,作为测试的JavaBean,代码如下所示。

package io.mykit.spring.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;

/**

  • @author binghe
  • @version 1.0.0
  • @description 测试实体类
    */

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Person implements Serializable {

private static final long serialVersionUID = 7387479910468805194L;
private String name;
private Integer age;

}
接下来,我们在工程的resources目录下创建Spring的配置文件beans.xml,通过beans.xml文件将Person类注入到Spring的IOC容器中,配置如下所示。

<?xml version="1.0" encoding="UTF-8"?>

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id = "person" class="io.mykit.spring.bean.Person">
    <property name="name" value="binghe"></property>
    <property name="age" value="18"></property>
</bean>


到此,我们使用XML方式注入JavaBean就配置完成了。接下来,我们创建一个SpringBeanTest类来进行测试,这里,我使用的是Junit进行测试,测试方法如下所示。

@Test
public void testXmlConfig(){

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person = (Person) context.getBean("person");
System.out.println(person);

}
运行testXmlConfig()方法,输出的结果信息如下。

Person(name=binghe, age=18)
从输出结果中,我们可以看出,Person类通过beans.xml文件的配置,已经注入到Spring的IOC容器中了。

通过注解注入JavaBean
通过XML文件,我们可以将JavaBean注入到Spring的IOC容器中。那使用注解又该如何实现呢?别急,其实使用注解比使用XML文件要简单的多,我们在项目的io.mykit.spring.plugins.register.config包下创建PersonConfig类,并在PersonConfig类上添加@Configuration注解来标注PersonConfig类是一个Spring的配置类,通过@Bean注解将Person类注入到Spring的IOC容器中。

package io.mykit.spring.plugins.register.config;

import io.mykit.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**

  • @author binghe
  • @version 1.0.0
  • @description 以注解的形式来配置Person
    */

@Configuration
public class PersonConfig {

 @Bean
public Person person(){
    return new Person("binghe001", 18);
}

}
没错,通过PersonConfig类我们就能够将Person类注入到Spring的IOC容器中,是不是很Nice!!主要我们在类上加上@Configuration注解,并在方法上加上@Bean注解,就能够将方法中创建的JavaBean注入到Spring的IOC容器中。

接下来,我们在SpringBeanTest类中创建一个testAnnotationConfig()方法来测试通过注解注入的Person类,如下所示。

@Test
public void testAnnotationConfig(){

ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class);
Person person = context.getBean(Person.class);
System.out.println(person);

}
运行testAnnotationConfig()方法,输出的结果信息如下所示。

Person(name=binghe001, age=18)
可以看出,通过注解将Person类注入到了Spring的IOC容器中。

到这里,我们已经明确,通过XML文件和注解两种方式都可以将JavaBean注入到Spring的IOC容器中。那么,使用注解将JavaBean注入到IOC容器中时,使用的bean的名称是什么呢? 我们可以在testAnnotationConfig()方法中添加如下代码来获取Person类型下的注解名称。

//按照类型找到对应的bean名称数组
String[] names = context.getBeanNamesForType(Person.class);
Arrays.stream(names).forEach(System.out::println);
完整的testAnnotationConfig()方法的代码如下所示。

@Test
public void testAnnotationConfig(){

ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class);
Person person = context.getBean(Person.class);
System.out.println(person);

//按照类型找到对应的bean名称数组
String[] names = context.getBeanNamesForType(Person.class);
Arrays.stream(names).forEach(System.out::println);

}
运行testAnnotationConfig()方法输出的结果信息如下所示。

Person(name=binghe001, age=18)
person
那这里的person是啥?我们修改下PersonConfig类中的person()方法,将person()方法修改成person01()方法,如下所示。

package io.mykit.spring.plugins.register.config;

import io.mykit.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**

  • @author binghe
  • @version 1.0.0
  • @description 以注解的形式来配置Person
    */

@Configuration
public class PersonConfig {

@Bean
public Person person01(){
    return new Person("binghe001", 18);
}

}
此时,我们再次运行testAnnotationConfig()方法,输出的结果信息如下所示。

Person(name=binghe001, age=18)
person01
看到这里,大家应该有种豁然开朗的感觉了,没错!!使用注解注入Javabean时,bean在IOC中的名称就是使用@Bean注解标注的方法名称。我们可不可以为bean单独指定名称呢?那必须可以啊!只要在@Bean注解中明确指定名称就可以了。比如下面的PersonConfig类的代码,我们将person01()方法上的@Bean注解修改成@Bean("person")注解,如下所示。

package io.mykit.spring.plugins.register.config;

import io.mykit.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**

  • @author binghe
  • @version 1.0.0
  • @description 以注解的形式来配置Person
    */

@Configuration
public class PersonConfig {

@Bean("person")
public Person person01(){
    return new Person("binghe001", 18);
}

}
此时,我们再次运行testAnnotationConfig()方法,输出的结果信息如下所示。

Person(name=binghe001, age=18)
person
可以看到,此时,输出的JavaBean的名称为person。

结论:我们在使用注解方式向Spring的IOC容器中注入JavaBean时,如果没有在@Bean注解中明确指定bean的名称,就使用当前方法的名称来作为bean的名称;如果在@Bean注解中明确指定了bean的名称,则使用@Bean注解中指定的名称来作为bean的名称。

好了,咱们今天就聊到这儿吧!别忘了给个在看和转发,让更多的人看到,一起学习一起进步!!

项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation

原文地址https://www.cnblogs.com/binghe001/p/13052276.html

相关文章
|
7天前
|
Java Spring 容器
Spring使用异步注解@Async正确姿势
Spring使用异步注解@Async正确姿势,异步任务,spring boot
|
6天前
|
XML Java 数据格式
spring复习03,注解配置管理bean
Spring框架中使用注解配置管理bean的方法,包括常用注解的标识组件、扫描组件、基于注解的自动装配以及使用注解后的注意事项,并提供了一个基于注解自动装配的完整示例。
spring复习03,注解配置管理bean
|
7天前
|
XML 前端开发 Java
控制spring框架注解介绍
控制spring框架注解介绍
|
6天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
2月前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
3月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
3月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
101 0
|
6天前
|
XML Java 关系型数据库
springboot 集成 mybatis-plus 代码生成器
本文介绍了如何在Spring Boot项目中集成MyBatis-Plus代码生成器,包括导入相关依赖坐标、配置快速代码生成器以及自定义代码生成器模板的步骤和代码示例,旨在提高开发效率,快速生成Entity、Mapper、Mapper XML、Service、Controller等代码。
springboot 集成 mybatis-plus 代码生成器
|
14天前
|
Java 应用服务中间件 开发者
深入探索并实践Spring Boot框架
深入探索并实践Spring Boot框架
25 2
|
6天前
|
前端开发 Java Spring
springboot自定义拦截器的简单使用和一个小例子
本文介绍了如何在Spring Boot中创建和使用自定义拦截器,通过一个登录验证的示例,演示了拦截器在MVC流程中的preHandle、postHandle和afterCompletion三个环节的作用,并说明了如何在Spring Boot配置类中注册拦截器。
下一篇
无影云桌面