@Configuration和@Bean给容器中注册组件

文章介绍了Spring框架中的IOC(控制反转)和DI(依赖注入)概念,以及如何通过注解方式在Spring配置类中使用@configuration和@Bean注解将JavaBean注入到IOC容器中进行管理。示例代码展示了如何创建Person类并配置MainConfig类进行注入,以及如何在测试类中获取和验证IOC容器中的Person组件。
摘要由CSDN通过智能技术生成

1.Spring IOC和DI

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

DI和IOC它俩之间的关系是DI不能单独存在,DI需要在IOC的基础上来完成。

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

1.1.通过注解注入JavaBean

在工程的com.tianxia.springannotation.entiry包下创建一个Person类,作为测试的JavaBean

package com.tianxia.springannotation.entity;

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

import java.io.Serializable;

/**
 * Person类
 * @author liqb
 * @date 2023-04-21 16:00
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person implements Serializable {

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;
}

在项目的com.tianxia.springannotation.config包下创建一个MainConfig类,并在该类上添加**@Configuration注解来标注该类是一个Spring的配置类**,也就是告诉Spring它是一个配置类,最后通过**@Bean注解将Person类注入到Spring的IOC**容器中

package com.tianxia.springannotation.config;

import com.tianxia.springannotation.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 * @author liqb
 * @date 2023-04-21 16:05
 **/
// 告诉Spring这是一个配置类
@Configuration
public class MainConfig {

    /**
     * @Bean注解是给IOC容器中注册一个bean,
     * 类型自然就是返回值的类型,id默认是用方法名作为id
     * @author liqb
     * @date 2023-04-21 16:06
     * @return
     */
    @Bean
    public Person person() {
        return new Person("liayun", 20);
    }
}

通过MainConfig类我们就能够将Person类注入到Spring的IOC容器中,在类上加上**@Configuration注解,并在方法上加上@Bean注解,就能够将方法中创建的JavaBean注入到Spring的IOC**容器中。

观察ioc中Person组件

package com.tianxia.springannotation;

import com.tianxia.springannotation.config.MainConfig;
import com.tianxia.springannotation.entity.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试类
 * @author liqb
 * @date 2023-04-21 16:11
 **/
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringAnnotationTest {

    /**
     * 测试通过注解注入的Person类
     * @author liqb
     * @date 2023-04-21 16:15
     */
    @Test
    public void testInjecttion() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = applicationContext.getBean(Person.class);
        System.out.println(person);
    }

    /**
     * 测试Person这个类型的组件在IOC容器中的名字
     * @author liqb
     * @date 2023-04-21 16:17
     */
    @Test
    public void testJavaBeanInIocName() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        // 查询Person这个类型的组件在IOC容器中的名字
        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : namesForType) {
            // person
            System.out.println(name);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值