springboot自定义配置文件
2021-01-05 01:28
标签:lin throws sping 初始化 highlight 经验 ict plain match 前言:如果你一点spring的基础没有,建议你不要学习springboot,至少先有一个spring的项目经验或者自己搭建过spring的项目再学习springboot,这样你会发现在spring中搞不懂的,在springboot中得到一些答案。springboot的原则是“约定大于配置”,所以在使用springboot的时候如果出现问题,没有一点基础,解决问题就很困难。 目标:将spring的容器中的配置:数据库的配置,定时器的配置转换到springboot中,实现spring与springbooot的配置对接。 数据库的配置转换: spring中数据库连接的配置如下 pringboot中的配置 spring 中的配置 springboot配置sqlSessionFactoryBean,对应上面的sping的SqlSessionFactoryBean类 。 spring中的配置 springboot中的配置 结论:springboot是通过@Configuration来标注自定义配置,配置中使用@Bean来添加类作用与spring配置中的.xml文件作用一样,两者都是容器的作用。 springboot自定义配置文件 标签:lin throws sping 初始化 highlight 经验 ict plain match 原文地址:https://www.cnblogs.com/moxiaotao/p/12983297.html
"dataSource"
class
=
"org.apache.commons.dbcp.BasicDataSource"
destroy-method=
"close"
>
"driverClassName"
value=
"${jdbc.mybatis.driver}"
/>
"url"
value=
"${jdbc.mybatis.url}"
/>
"username"
value=
"${jdbc.mybatis.username}"
/>
"password"
value=
"${jdbc.mybatis.password}"
/>
"initialSize"
value=
"10"
/>
"maxActive"
value=
"1000"
/>
"maxIdle"
value=
"30"
/>
"minIdle"
value=
"10"
/>
"maxWait"
value=
"2000"
/>
@Bean
(name =
"dataSource"
)
public
BasicDataSource myGetDataSource() {
BasicDataSource dataSource =
new
BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setPassword(passWord);
dataSource.setUsername(userName);
dataSource.setMaxIdle(
2
);
dataSource.setMaxActive(
20
);
dataSource.setMaxWait(
1000
);
dataSource.setInitialSize(
2
);
//
dataSource.setValidationQuery(
"SELECT 1"
);
dataSource.setRemoveAbandoned(
true
);
dataSource.setTestWhileIdle(
true
);
dataSource.setTimeBetweenEvictionRunsMillis(
30000
);
dataSource.setNumTestsPerEvictionRun(
30
);
dataSource.setMinEvictableIdleTimeMillis(
1800000
);
return
dataSource;
}
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
"dataSource"
ref=
"dataSource"
/>
"mapperLocations"
value=
"classpath:springMVCmybatis/com/my/mapper/*.xml"
/>
@Bean
(name =
"sqlSessionFactoryBean"
)
public
SqlSessionFactoryBean myGetSqlSessionFactory(DataSource dataSource)
throws
Exception {
SqlSessionFactoryBean sqlSessionFactoryBean =
new
SqlSessionFactoryBean();
// mapperLocations
ResourcePatternResolver resolver =
new
PathMatchingResourcePatternResolver();
try
{
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
"classpath:mapper/*Mapper.xml"
));
}
catch
(IOException e) {
log.info(
"sqlSessionFactoryBean的setMapperLocations有问题"
);
e.printStackTrace();
}
// dataSource
sqlSessionFactoryBean.setDataSource(dataSource);
// SqlSessionFactory sessionFactory = sqlSessionFactoryBean.getObject();
return
sqlSessionFactoryBean;
}
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
"basePackage"
value=
"springMVCmybatis"
/>
"sqlSessionFactoryBeanName"
value=
"sqlSessionFactory"
/>
package
com.my.myconfigure;
import
org.mybatis.spring.mapper.MapperScannerConfigurer;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter;
import
org.springframework.context.annotation.Configuration;
//
//
//
//
//
@Configuration
// 这个注释是需要在加载MybatisDbConfigure.class之后再加载MapperScanConfig这个类
@AutoConfigureAfter
(MybatisDbConfigure.
class
)
public
class
MapperScanConfig {
public
MapperScannerConfigurer myGetMapperScannerConfigurer() {
MapperScannerConfigurer myMapperScannerConfigurer =
new
MapperScannerConfigurer();
myMapperScannerConfigurer.setBasePackage(
"com.my.dao"
);
myMapperScannerConfigurer.setSqlSessionFactoryBeanName(
"sqlSessionFactoryBean"
);
return
myMapperScannerConfigurer;
}
}