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

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);
        }
    }
}

你可能感兴趣的:(spring注解开发,spring,junit,java)