设为首页 加入收藏

TOP

[Spring6.0源码解析]简述@Configuration注解(二)
2023-07-25 21:32:35 】 浏览:55
Tags:Spring6.0 解析 简述 @Configuration 注解
uot;, (person1 == person2)); } }

proxyBeanMethods的使用

在之前提到,proxyBeanMethods配置表示用 @Bean 注解的方法在IOC容器中是否为单例对象,默认为true。

默认情况下,打印出结果如下:

person1 是否等于 person2 ===>> true

修改一下proxyBeanMethods的值为false:

@Configuration(proxyBeanMethods = false)
public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

打印结果如下:

person1 是否等于 person2 ===>> false

从输出结果可以看出,当@Configuration中的proxyBeanMethods属性为false时,每次调用@Configuration注解标注类中被@Bean标注的方法时,都会返回不同的Bean实例对象。

创建IOC容器

传入配置类

调用AnnotationConfigApplicationContext类的构造方法传入配置类的Class对象创建IOC容器时,可以省略配置类上的@Configuration注解,如下:

public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

输出结果:

person1 是否等于 person2 ===>> false

可以看到,若省略配置类上的@Configuration注解,则每次调用配置类中被@Bean注解标注的方法时,都会返回不同的Bean实例对象,与@Configuration中设置proxyBeanMethods的属性为false的效果相同。

传入包

调用AnnotationConfigApplicationContext类的构造方法传入包名创建IOC容器时,不能省略配置类上的@Configuration注解:

public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

执行函数:

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("io.binghe.spring.annotation.chapter01.configuration");
        ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
        Person person1 = config.person();
        Person person2 = config.person();
        LOGGER.info("person1 是否等于 person2 ===>> {}", (person1 == person2));
    }

此时运行main方法,会发生报错:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.binghe.spring.annotation.chapter01.configuration.config.ConfigurationAnnotationConfig' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1148)
	at io.binghe.spring.annotation.chapter01.configuration.ConfigurationAnnotationTest.main(ConfigurationAnnotationTest.java:36)

添加上@Configuration注解则程序执行正常。

扩展知识

AnnotationConfigApplicationContext

Spring在 BeanFactory 的基础上提供一些具体容器的实现。AnnotationConfigApplicationContext就是一个用来管理注解 Bean 的容器。如下结构图:

img

从图中可以看到,AnnotationConfigApplicationContext继承GenericApplicationContext(通用应用上下文),而GenericApplicationContext又实现了BeanDefinitionRegistry接口,所以可以通过AnnotationConfigApplicationContext实例类注册BeanDefintion,然后调用refresh()方法来初始化上下文。AnnotationConfigApplicationContext还继承了AbstractApplicationContext,而AbstractApplicationContext提供了ApplicationContext的抽象实现

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇《Spring揭秘》-第二章- 学习记录 下一篇Disruptor-简单使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目