SpringBoot @Configuration与@Bean注解使用介绍

目录
  • demo示例
  • 特点和特性

之前我们都是通过xml的方式定义bean,里面会写很多bean元素,然后spring启动的时候,就会读取bean xml配置文件,然后解析这些配置,然后会将这些bean注册到spring容器中,供使用者使用。

Spring3.0开始,@Configuration用于定义配置类,定义的配置类可以替换xml文件,一般和@Bean注解联合使用。

@Configuration注解可以加在类上,让这个类的功能等同于一个bean xml配置文件。

@Bean注解类似于bean xml配置文件中的bean元素,用来在spring容器中注册一个bean。

demo示例

1.创建一个工程

2.创建bean文件夹并创建两个示例用户和部门

如下:

用户

package com.example.ethan.bean;
public class User {
    private String name;
    private Integer age;
    private Dept dept;
	public User() {
	}
    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public Dept getDept() {
        return dept;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

部门

package com.example.ethan.bean;
public class Dept {
    private String name;
    public Dept(String name) {
        this.name = name;
    }
}

3.创建配置类

使用@Configuration注解,并使用@Bean注解创建bean

package com.example.ethan.config;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration  //告诉SpringBoot这是一个配置类 == 配置文件
public class ConfigDemo {
    @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        return zhangsan;
    }
    @Bean("my rd")
    public Dept rd(){
        return new Dept("研发部");
    }
}

4.在主程序查看

编写主程序,查看容器中的Bean

package com.example.ethan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class EthanApplication {
    public static void main(String[] args) {
        // 返回ioc容器
        ConfigurableApplicationContext run = SpringApplication.run(EthanApplication.class, args);
        // 查看容器组件
        String[] beanDefinitionNames = run.getBeanDefinitionNames();
        System.out.println("========================");
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
    }
}

运行后可以看到configDemo、user01、my rd都已经被容器管理。

特点和特性

1.配置类本身也是组件,也会有被IOC容器管理的Bean,且是一个单实例的代理对象。

在主程序验证代码如下:

ConfigDemo bean = run.getBean(ConfigDemo.class);
 System.out.println(bean);

// 结果:com.example.ethan.config.ConfigDemo$$EnhancerBySpringCGLIB$$24eef7b3@a619c2

可以看到得到的bean是一个CGLIB代理对象。

2.默认被IOC容器管理@Bean注解产生的Bean是单实例的。

在主程序验证代码如下:

Dept d1 = run.getBean("my rd", Dept.class);
Dept d2 = run.getBean("my rd", Dept.class);
System.out.println("组件:"+(d1 == d2));
// 组件:true

结果为true,证明@Bean注解产生的Bean是单实例的。

3.@configration的proxyBeanMethods属性。

这个属性默认为true。他的意思就是,当从容器获取Bean时,是否用上面第1点中的代理对象调用方法获取bean。

增加验证如下:

	ConfigDemo bean = run.getBean(ConfigDemo.class);
    System.out.println(bean);
	// 如果@Configuration(proxyBeanMethods = true)代理对象调用方法
    User user = bean.user01();
    User user1 = bean.user01();
    System.out.println(user == user1);

当configration的proxyBeanMethods=true时,结果为true,否则结果为false。
也就是,当proxyBeanMethods=true,使用代理对象获取Bean,代理会拦截所有被@Bean修饰的方法,默认情况(bean为单例)下确保这些方法只被调用一次,放进容器,然后从容器查找到,就会直接使用,从而确保这些bean是同一个bean,即单例的。

否则,不使用代理对象获取Bean,每次获取都新建,所以两个Bean不相等。

这样做的主要目的是为了解决组件依赖问题,比如下面的部门被用户
依赖,可以保证用户依赖的部门是单实例。

验证代码如下:

首先让用户依赖部门

package com.example.ethan.config;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods=true)  //告诉SpringBoot这是一个配置类 == 配置文件
public class ConfigDemo {
    @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        //user组件依赖了Dept组件
        zhangsan.setDept(rd());
        return zhangsan;
    }
    @Bean("my rd")
    public Dept rd(){
        return new Dept("研发部");
    }
}

然后测试用户依赖的组件是否是容器中的Bean

	User user01 = run.getBean("user01", User.class);
    Dept dept = run.getBean("tom", Dept.class);
    System.out.println("用户的宠物:"+(user01.getDept() == dept));
    // // 用户的部门:true

测试可以看到,当proxyBeanMethods=true时,结果为true,否则为false。

当proxyBeanMethods=true时也称为 Full模式,否则称为Lite模式。

Full(proxyBeanMethods = true)【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
组件依赖必须使用Full模式默认。其他默认是否Lite模式。
最佳实战

  • 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
  • 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

到此这篇关于SpringBoot @Configuration与@Bean注解使用介绍的文章就介绍到这了,更多相关SpringBoot @Configuration与@Bean内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot配置@Configuration注解和@bean注解

    目录 1.@Configuration注解 2.@bean注解 3.单实例 4.配置类也是容器的组件 5.直接调用配置类里面的person1()方法 6.proxyBeanMethods——代理bean的方法 1.@Configuration注解 用法:作用在类上面 作用:告诉SpringBoot这是一个配置类,相当于Spring中的xml配置文件. @Configuration //告诉SpringBoot这是一个配置类 == 配置文件 public class Config { } 2.@b

  • SpringBoot通过注解注入Bean的几种方式解析

    目录 1.背景 xml扫描包的方式 2.通过注解注入的一般形式 2.1.Bean类 2.2.Configuration类 2.3.Test类 3.通过构造方法注入Bean 3.1.Bean类 3.2.AnotherBean类 3.3.Configuration类 4.通过set方法注入Bean 4.1.MyBean类 4.2.Configuration类和Test类 5.通过属性去注入Bean 6.通过List注入Bean 6.1.MyBeanList类 6.2.MyConfiguration类

  • SpringBoot Bean花式注解方法示例下篇

    目录 1.容器初始化完成后注入bean 2.导入源的编程式处理 3.bean裁定 拓展 4.最终裁定 1.容器初始化完成后注入bean import lombok.Data; import org.springframework.stereotype.Component; @Component("miao") @Data public class Cat { } 被注入的JavaBean import org.springframework.context.annotation.Con

  • Springboot @Configuration @bean注解作用解析

    这篇文章主要介绍了springboot @Configuration @bean注解作用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @Configuration注解可以达到在Spring中使用xml配置文件的作用 @Bean就等同于xml配置文件中的<bean> 在spring项目中我们集成第三方的框架如shiro会在spring.xml配置文件中进行配置,例如: <!-- 配置shiro框架提供过滤器工厂 --> <

  • SpringBoot @Configuration与@Bean注解使用介绍

    目录 demo示例 特点和特性 之前我们都是通过xml的方式定义bean,里面会写很多bean元素,然后spring启动的时候,就会读取bean xml配置文件,然后解析这些配置,然后会将这些bean注册到spring容器中,供使用者使用. Spring3.0开始,@Configuration用于定义配置类,定义的配置类可以替换xml文件,一般和@Bean注解联合使用. @Configuration注解可以加在类上,让这个类的功能等同于一个bean xml配置文件. @Bean注解类似于bean

  • SpringBoot对不同Bean注解的区别和使用场景说明

    目录 对不同Bean注解的区别和使用场景 什么是Bean? 注解@Bean @Component …等都有什么区别? SpringBoot注入对象冲突如何解决? SpringBoot的各种注解 @Configuration 总结 对不同Bean注解的区别和使用场景 什么是Bean? 谈Bean的潜台词是在说Spring中的Bean,我们都知道Spring中的BeanFactory,而Bean这个概念也是由此而来.在Spring中,只要一个类能被实例化,并被Spring容器管理,这个类就称为一个B

  • SpringBoot中的Condition包下常用条件依赖注解案例介绍

    目录 一.@ConditionalOnClass() Spring中存在指定class对象时,注入指定配置 1.首先引入pom依赖 2.实体类测试对象 3.定义@ConditionalOnClass()配置类 4.启动类测试 二.注入指定配置 1.首先引入pom依赖 2.实体类测试对象 3.定义@ConditionalOnMissingClass()配置类 4.启动类测试 三.加载指定配置 1.首先引入pom依赖 2.实体类测试对象 2.1 引入条件判断实体类 3.定义@ConditionalO

  • Spring注解@Configuration与@Bean注册组件的使用详解

    目录 原始Spring开发 Person.java pom.xml bean.xml PersonTest.java 注解Spring开发 原始Spring开发 Person.java 准备Person.java类: package com.jektong.spring; public class Person { private String name; private int age; public Person() { super(); } public Person(String nam

  • @Bean注解和@Configuration、@Component注解组合使用的区别

    目录 一.@Bean的"full"模式和"lite"模式 二.两种模式的差异 1."full"模式下@Bean方法互相调用 2."lite"模式下@Bean方法互相调用 三.总结 一.@Bean的"full"模式和"lite"模式 在一般常见情况下,@Bean注解在@Configuration类中声明,称之为"full"模式:当@Bean注解和@Component注解

  • SpringBoot启动类@SpringBootApplication注解背后的秘密

    在用SpringBoot的项目的时候,会发现不管干什么都离不开启动类,他是程序唯一的入口,那么他究竟为我们做了什么?本篇文章主要解析@SpringBootApplication. 一.启动类 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } 二.@SpringBoo

  • SpringBoot基于redis自定义注解实现后端接口防重复提交校验

    目录 一.添加依赖 二.代码实现 三.测试 一.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.4.RELEASE</version> </dependency> <dependency> <

  • SpringBoot详细列举常用注解的说明

    目录 1 概述 2 常用注解 1 概述 IOC 是Spring 最为重要的功能之一,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. 简言之,注解本身没有什么业务功能的,和 xml 一样,是一种元数据,元数据即解释数据的数据,这就是所谓配置. 2 常用注解 @Component 表示一个带注释的类是一个“组件”,成为Spring管理的Bean.当使用基于注解的配置和类路径扫描时,这些类被视为自动检测的候选对象.同时@Co

随机推荐