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 接口

浙公网安备 33010602011771号