SpringBoot---配置文件
来源:互联网 发布:大数据公司怎么盈利 编辑:程序博客网 时间:2024/06/05 14:22
1.配置文件applicationContext.xml,让boot启动时,加载配置xml。@ImportResource,让boot主动加载我们的bean。例如:
@ImportResource(locations = "classpath*:/applicationContext.xml")
其实用boot,可以不用在项目工程里加载spring的上下文的,这里是为了方便说明,我手动建立了spring的上下文。如果不配置启动会报错,这个跟Spring Boot的启动机制有关,boot在启动启动类以后,才会根据类文件间的依赖去加载spring的bean类,boot本身并不会自动去读取xml文件,所以boot是不知道我们在xml里注入的bean的,所以肯定找不到datasource和mybatis配置,所以,我们要在StartApp类里增加一个注解@@ImportResource,让boot主动加载我们的bean。
如果在applicationContext.xml中配置了mybatis,这时启动还是会报错,因为boot本着自动配置的原则,会帮你去加载mybatias的自动配置类,但自动配置类又是用的包内自有的mapper,导致找不到包,这时我们排除一下自动配置:
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
application.context.xml如下
<!-- 数据源配置, 使用Tomcat JDBC连接池 --><beanid="dataSource"class="org.apache.tomcat.jdbc.pool.DataSource"destroy-method="close"><!-- Connection Info --><propertyname="driverClassName"value="${jdbc.driver}" /><propertyname="url"value="${jdbc.url}" /><propertyname="username"value="${jdbc.username}" /><propertyname="password"value="${jdbc.password}" /><!-- Connection Pooling Info --><propertyname="maxActive"value="10" /><propertyname="maxIdle"value="50" /><propertyname="minIdle"value="0" /><propertyname="defaultAutoCommit"value="false" /></bean><!-- MyBatis配置 --><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource" /><!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --><propertyname="typeAliasesPackage"value="com.hjf.boot.api.domain" /><!-- 显式指定Mapper文件位置 --><propertyname="mapperLocations"value="classpath:/mybatis/*Mapper.xml" /></bean><!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.hjf.boot.demo.boot_mybatis.domain" /><propertyname="annotationClass"value="com.hjf.boot.demo.boot_mybatis.dao.Mapper"/></bean>
2.通过class来进行配置文件,也就是写一个java的配置代码
方法:
第1步:建立配置类文件config。这里需要三个类文件:
DataSourceConfig —>datasource源配置
MyBatisConfig —> 配置sqlSessionFactory
MyBatisMapperScannerConfig —> 配置mapperScannerConfigurer
这里需要说明一下:最后一个配置因为依赖前两个,需要最后一个加载,否则会报错。
具体代码可以参考。http://blog.csdn.net/mickjoust/article/details/51646658
- springboot 配置文件
- SpringBoot 配置文件
- SpringBoot---配置文件
- springboot-配置文件
- springboot-配置文件
- SpringBoot配置文件
- springboot配置文件
- 【SpringBoot】SpringBoot之入门配置文件
- springBoot配置文件application.properties
- Springboot 之 多配置文件
- springboot中配置文件说明
- SpringBoot——配置文件
- 使用SpringBoot加载配置文件
- SpringBoot---配置文件2
- Springboot最全配置文件
- SpringBoot读取配置文件乱码
- Springboot配置文件的使用
- SpringBoot 配置文件 application.properties
- Android ViewTreeObserver介绍
- 003 - ajax跨域的问题和处理
- Struts属性和模型驱动
- Spring第一个应用程序
- Linux下修改Mysql的用户(root)的密码
- SpringBoot---配置文件
- 浅学Swift 3.0.1中的Classes and structures
- 从事前端开发必须要了解的CSS原理
- mysql 引擎的优缺点
- EditText的hint位置和光标位置靠右
- 微信支付iOS
- CUDA安装后路径不全导致TensorFlow MNIST例子运行中PyThon发生异常
- js keycode对照表
- 【LeetCode-Java】54. Spiral Matrix+59. Spiral Matrix II