使用自定义注册,@import的方式进行实例化
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({xxxConfiguration.class,yyyConfiguration.class})
新建注解类 public @interface EnableResource{}
目录结构
src
main
anno
EnableResource
config
xxxConfiguration
@Configuration
@ConditionOnProperty(value='policy.type',havingValue='标识')
@MapperScan(basePackages = 'com.xg.mapper' ,sqlSessionFactoryRef = 'mysqlSqlSessionFactory')
public class xxxConfiguration{
@Value("$(param.jdbc.url)") //调用方的配置文件中
private String jdbcUrl
@Bean
public xxxService xxxServiceBean(){
return new xxxServiceImpl();
}
@Bean(name="mysqlDataSource")
public DataSource mysqlDataSource(String jdbcUrl){
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(jdbcUrl)
hikariConfig....
return new new HikariDataSource(hikariConfig);
}
@Primary
@Bean(name = "mysqlSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource datasource) throw Exception{
MybatisSqlSessionFactoryBean factoryBean = MybatisSqlSessionFactoryBean();
MybatisConfiguration config = new MybatisConfiguration();
config.addInterceptor(new PaginationInterceptor());
factoryBean.setConfiguration(config);
factoryBean.setDataSource(datasource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(classpath:mapper/*.xml));
return factoryBean.getObject();
}
}
entity
mapper
service
impl
resources
mapper
xxx.xml
调用方:在启动类上添加 引入Enable的注解
使用spring.factories的方式进行实例化