作用:为bean工厂补充一些bean的定义。
ConfigurationClassPostPorcessor
Bean工厂后置处理器(ConfigurationClassPostProcessor)可以去解析:
@ComponentScan
@Bean
@Import
@ImportResource
package com.xkj.org.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.xkj.org.a05.component")
public class Config {@Beanpublic Bean1 bean1() {return new Bean1();}@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean;}@Beanpublic DruidDataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setUrl("jdbc:mysql://localhost:3306/test");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}}
package com.xkj.org.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管理啦");}
}
package com.xkj.org.a05.component;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;@Component
public class Bean2 {private static final Logger log = LoggerFactory.getLogger(Bean2.class);public Bean2() {log.debug("我被Spring管理啦");}
}
package com.xkj.org.a05;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;public class A05Application {private static final Logger log = LoggerFactory.getLogger(A05Application.class);public static void main(String[] args) {GenericApplicationContext context = new GenericApplicationContext();context.registerBean("config", Config.class);//bean工厂后置处理器context.registerBean(ConfigurationClassPostProcessor.class);context.refresh();for (String name: context.getBeanDefinitionNames()) {System.out.println(name);}context.close();}
}
MapperScannerConfigure
解析Mybatis的@MapperScanner @Mapper
//bean工厂后置处理器,第二个参数指定扫描的包(很少使用) context.registerBean(MapperScannerConfigurer.class, bd -> {bd.getPropertyValues().add("basePackage", "com.xkj.org.a05.mapper"); });
package com.xkj.org.a05.mapper;import org.apache.ibatis.annotations.Mapper;@Mapper
public interface Mapper1 {
}
package com.xkj.org.a05.mapper;import org.apache.ibatis.annotations.Mapper;@Mapper
public interface Mapper2 {
}