SpringBoot @Bean @Configuration

@Configuration底层是含有@Component,所以@Configuration具有和@Component的作用
@Configuration可以理解为spring的application.xml中的< beans>标签(配置容器,用于构建bean定义,初始化Spring容器)
@Configuration标注在类上,相当于把该类作为spring的xml

配置文件中的<beans>,作用为:配置spring容器(应用上下文)
package com.dsx.demo;

import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化。。。");
    }
}

相当于以下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">

</beans>

@Bean可理解为spring的xml中的< bean>标签
@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的,作用为:注册bean对象

package com.dsx.demo;

public class User {

    private String username;
    private String url;
    private String password;

    public void sayHello() {
        System.out.println("User sayHello...");
    }

    public String toString() {
        return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
    }

    public void start() {
        System.out.println("User初始化。。。");
    }

    public void cleanUp() {
        System.out.println("User销毁。。。");
    }
}

配置类:

package com.dsx.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化。。。");
    }

    // @Bean注解注册bean,同时可以指定初始化和销毁方法
    //把该方法返回的对象注册到spring容器中,交给spring管理
    @Bean
    @Scope("prototype")
    public User testUser() {
        return new User();
    }
}

1.@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名号,则默认为该方法名;
2.@Bean注解默认作用域为单例singleton,可以通过@Scope(“prototype”)设为原型作用域;
3.既然@Bean的作用是注册bean对象,那么完全可以使用@Comonet @Controller @Service @Repository等注 解来注册bean(在需要的类上加注解),当然需要配置@ComponentScan注解进行自动扫描

使用@Bean注解是可以配置initMethod()和destoryMethod方法,分别在实例和销毁的时候执行.
或者使用通过@PostConstruct和@PreDestory注解实现初始化和销毁bean之前的操作

用xml注册bean对象和使用注解@Bean注册对象之间有什么明显的区别呢?
比较明显的区别在于加载对象存在一定的先后顺序时会出现一个循环加载Bean的问题,容易出现在于使用动态数据源切换,继承AbstractRoutingDataSource实现的方法。为什么会出现这样的一种情况,这个就归结于springboot加载@Bean不是按照写代码的顺序加载的,解决的办法也是有的使用@Order注解的或者@Bean的方法上标识@DependsOn注解,来指定当前Bean实例化时需要触发哪些Bean的提前实例化。所以我还是建议在容易出现循环加载的地方使用xml配置文件初始化bean,然后在Application.class中添加上@ImportResource导入相应的xml文件。

SpringBoot不是Spring的加强版,所有@Configuration和@Bean同样可以在普通的spring项目中用

猜你喜欢

转载自blog.csdn.net/weixin_42286175/article/details/85990095
今日推荐