Springboot @Configuration

2.1组件添加

1. @Configuration

这个注解,标记在类上(代表告诉springboot这个类是一个配置类)。相当于我们原来写的spring的配置文件。

1.1. @Bean

这个注解写在被@Configuration类里,用在方法上,就是把方法名当作bean的id放入Spring的IOC容器中,相当于我们原来写的spring配置文件里的<bean></bean>标签,返回值就是我们bean 的对象

实例:

两个普通的bean对象 Pet类

package com.atguigu.boot.bean;

public class Pet {


    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Pet(String name) {
        this.name = name;
    }

    public Pet() {
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name=" + name +  +
                };
    }
}

user类

package com.atguigu.boot.bean;

public class User {

    private String name;

    private Integer age;

    private Pet pet;

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public User() {
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name=" + name +  +
                ", age=" + age +
                ", pet=" + pet +
                };
    }
}

配置类 MyConfig

package com.atguigu.boot.config;

import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

    @Bean
    public User user01(){
        User user = new User();
    
        return user;
    }

    @Bean
    public Pet myPet(){
        return new Pet();
    }
}

这里就相当于原来写一个Spring的配置类,并创建两个Bean属性

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user01" class="com.atguigu.boot.bean.User"></bean>

    <bean id="myPet" class="com.atguigu.boot.bean.Pet"></bean>
</beans>

由此可见,Spring的简便

从容器中取出name是user01,也就是刚刚在配置类Myconfig里创建好的

其中@Configation方法中有一个可设置的参数,默认为true

@Configuration(proxyBeanMethods = true)的情况下:
假如此时为User类中配置他的Pet属性

此时调用方法查看结论是否正确

@Configuration(proxyBeanMethods = false)的情况下:

验证一下:

proxyBeanMethods: 是不是代理bean的方法
Full(proxyBeanMethods = true) Full模式保证了组件是单例的,当被其他组件引用时,就一定能确定就是该组件被引入了,但是每次实例化一个组件时,都得去IOC容器里查看是否该组件已经存在。所以程序在启动时就会变慢。
Lite(proxyBeanMethods = false) Lite模式不能保证组件是单例的,即如果从配置类中调用同类中方法获得的值不是IOC容器中的值。优点就是启动快

这两种模式下 通过getBean时都是从ioc容器中拿到同一个组件
而要是单单调用方法的话,就会生成一个新的组件
    配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

资源来自--尚硅谷雷丰阳老师

经验分享 程序员 微信小程序 职场和发展