美文网首页spring
Spring IoC 容器装配 Bean

Spring IoC 容器装配 Bean

作者: anlostsheep | 来源:发表于2020-05-17 02:05 被阅读0次

以注解的方式装配 Bean 到 Spring IoC 容器中

使用一个基于注解的方式的 IoC 容器:AnnotationConfigApplicationContext

实例

首先创建一个 Java 对象(POJO)

User.java

@Data
public class User {

    private Integer id;
    private String name;
    private String age;
}

然后再定义一个 Java 配置文件 AppConfig.java

package com.lostsheep.java.design.mode.springbean;

import com.lostsheep.java.design.mode.builder.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <b><code>Appconfig</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2020/5/17 1:38.
 *
 * @author lostsheep
 * @since java-design-mode 0.1.0
 */
@Configuration
public class Appconfig {

    @Bean(name = "user")
    public User initUser() {
        return User.builder()
                .id(1)
                .age("20")
                .name("lostsheep")
                .build();

    }
}
  • @Configuration 注解代表该类是一个 Java 配置文件,Spring 的容器会根据它来生成 IoC 容器去装配 Bean
  • @Bean 注解代表将 initUser() 方法返回的 POJO 对象装配到 IoC 容器中,而属性 name="user" 定义了这个 Bean 的名称,如果没有配置 name 属性,则会将方法名称 initUser 作为 Bean 名称保存到 Spring IoC 容器中

使用 AnnotationConfigApplicationContext 来构建自己的 IoC 容器

代码片段

package com.lostsheep.java.design.mode.springbean;

import com.lostsheep.java.design.mode.builder.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <b><code>Appconfig</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2020/5/17 1:38.
 *
 * @author lostsheep
 * @since java-design-mode 0.1.0
 */
@Configuration
@Slf4j
public class AppConfig {

    @Bean(name = "user")
    public User initUser() {
        return User.builder()
                .id(1)
                .age("20")
                .name("lostsheep")
                .build();

    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);

        User userBean = applicationContext.getBean(User.class);

        log.info(userBean.toString());
    }
}

  • 代码中将 Java 配置文件 AppConfig 反射对象传递给 AnnotationConfigApplicationContext 的构造方法中,这样它就可以读取配置了
  • 然后将配置类中的 Bean 装配到 IoC 容器中,并使用 getBean() 方法获取对应 Bean,启动该 main() 方法可以看到控制台打印的 Bean 信息
D:\Tools\jdk1.8.0_241\bin\java.exe
01:56:18.930 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7b1d7fff
01:56:18.943 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
01:56:19.037 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
01:56:19.038 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
01:56:19.039 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
01:56:19.041 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
01:56:19.049 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appConfig'
01:56:19.054 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user'
01:56:19.079 [main] INFO com.lostsheep.java.design.mode.springbean.AppConfig - User{id=1, name='lostsheep', age='20'}
  • 已经可以明显地看到配置到配置文件中的名称为 user 的 Bean 已经被装配到 IoC 容器中,并且可以通过 getBean() 方法获取对应的 Bean,并在日志中输出了该 Bean 的信息
  • 当然这只是很简单的方法,而注解 @Bean 也不是唯一创建 Bean 的方法,还有其他的方法可以让 IoC 容器装配 Bean ,而且 Bean 之间还有依赖的关系需要进一步处理。

相关文章

网友评论

    本文标题:Spring IoC 容器装配 Bean

    本文链接:https://www.haomeiwen.com/subject/yltjohtx.html