Spring系列——@Configuration+@Bean注册组件

本文对比了Spring中XML配置与@Configuration+@Bean注解的方式添加组件。XML配置繁琐,而从Spring3.0开始,@Configuration和@Bean提供了更简洁的注册Bean的方法,常用于SpringBoot应用。示例展示了如何使用@Bean创建并注入一个User对象,并通过测试验证了配置的正确性。
摘要由CSDN通过智能技术生成

Spring系列——@Configuration+@Bean注册组件

前言

在Spring3.0之前我们都是使用XML的方式来给Spring容器中添加组件的,使用过XMl给Spring容器添加组件的兄弟,大家都知道XML形式添加组件是多么繁琐,所以在Sping3.0之后,支持@Configuration+@Bean添加组件,而且在SpringBoot中基本上都是使用@Congfiguration+@Bean的模式来注册组件,所以我们需要了解一下@Configuration+@Bean的使用。

XML形式添加组件

-Bean实体类

package org.example.config01;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
}
  • 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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 class="org.example.config01.User" id="user">
        <property name="name" value="xml形式注入的Bean"></property>
    </bean>
</beans>

注册一个name=“xml形式注入的Bean”,beanName="user"的User对象

  • 测试
@Test
    public void xml(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/config01/beans.xml");
        String[] beanNamesForType = applicationContext.getBeanNamesForType(User.class);
        for (String name : beanNamesForType) {
            System.out.println(name);
            System.out.println(applicationContext.getBean(name));
        }
    }
  • 测试结果
    在这里插入图片描述

@Configuration+@Bean方式注入Bean

  • Bean实体类
package org.example.config01;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
}
  • 配置类
package org.example.config01;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean(value = "user01")
    public User user(){
        return new User("编码方式注入的Bean");
    }
}

注册一个name=“编码方式注入的Bean”,beanName="user01"的User对象(默认beanName为方法名也就是user,@Bean注解可以通过value设置beanName

  • 测试
@Test
@Test
public void annotation(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
    String[] beanNamesForType = applicationContext.getBeanNamesForType(User.class);
    for (String name : beanNamesForType) {
        System.out.println(name);
        System.out.println(applicationContext.getBean(name));
    }
}
  • 测试结果
    在这里插入图片描述
    我们可以看到beanName="user01"和我们预期的结果一样。
    相信到这里大家对@Configuration+@Bean注入组件也有一定的了解了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值