Spring 高级 BeanFactory 后处理器 - BeanFactory 后处理器的作用

一、ConfigurationClassPostProcessor 

1、示范代码

Bean2

复制代码
package com.mangoubiubiu.show.a05.component;

import com.mangoubiubiu.show.a05.Bean1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class Bean2 {
    private static final Logger log = LoggerFactory.getLogger(Bean1.class);

    public Bean2() {
        log.debug("我被 Spring 管理啦");
    }
}
复制代码

Bean1

复制代码
package com.mangoubiubiu.show.a05;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Bean1 {

    private static final Logger log = LoggerFactory.getLogger(Bean1.class);

    public Bean1() {
        log.debug("我被 Spring 管理啦");
    }
}
复制代码

Config

复制代码
package com.mangoubiubiu.show.a05;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.mangoubiubiu.show.a05.component")
public class Config {


    @Bean
    public Bean1 bean1(){
        return new Bean1();
    }


    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }


    @Bean(initMethod = "init")
    public DruidDataSource druidDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/zeal_user");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }





}
复制代码
A05Application
复制代码
package com.mangoubiubiu.show.a05;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;

@Slf4j
public class A05Application {

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        //将config注册到容器里面
        context.registerBean("config",Config.class);


        //初始化容器

        context.refresh();

        for (String name: context.getBeanDefinitionNames()) {
            System.out.println(name);
        }

        context.close();
    }



}
复制代码

初次运行发现 只有我们手动用registerBean 注册的Bean 注入到了容器中,Config 里面 ComponentScan 和 @Bean 注解并没有生效

加入ConfigurationClassPostProcessor Bean 后处理器

发现 ComponentScan 和 @Bean 注解都生效了

总结:

  • ConfigurationClassPostProcessor 可以解析
    • @ComponentScan
    • @Bean
    • @Import
    • @ImportResource

二、MapperScannerConfigurer

1、示范代码

Mapper1

package com.mangoubiubiu.show.a05.mapper;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface Mapper1 {
}

A05Application

复制代码
package com.mangoubiubiu.show.a05;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;

@Slf4j
public class A05Application {

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        //将config注册到容器里面
        context.registerBean("config",Config.class);
        context.registerBean(ConfigurationClassPostProcessor.class);
        context.registerBean(MapperScannerConfigurer.class,beanDefinition -> {
            beanDefinition.getPropertyValues().add("basePackage","com.mangoubiubiu.show.a05.mapper");
        });

        //初始化容器

        context.refresh();

        for (String name: context.getBeanDefinitionNames()) {
            System.out.println(name);
        }

        context.close();
    }



}
复制代码

运行发现Mapper接口也被成功扫描进容器中

总结:

  • MapperScannerConfigurer 可以解析
    • Mapper 接口

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/16592952.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(43)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起