出现这个异常的原因是:我们在配置文件中指定了:config-location,然后我们又配置了关于configuration的信息,导致的报错。 大体意思就是,二者只能选择一种,要么指定config-location我们自己的xml配置文件,要么我们就使用Mybatis的start提供的配置。 如果两者都存在,那么他就会包冲突。 比如如下配置:
mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:mapper/*Mapper.xml configuration: cache-enabled: true这样他就会启动报错:
Property 'configuration' and 'configLocation' can not specified with together为了验证这一个异常,那么我们就深入源码看一波:
找到mybatis-spring-boot-starter的包,然后打开找到META-INF/spring.factories文件
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration这里能够查看mybatis使用了springboot的那些扩展点,同时也包括自动装配的扩展点: 然后我们进入org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
@org.springframework.context.annotation.Configuration @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class }) @ConditionalOnBean(DataSource.class) @EnableConfigurationProperties(MybatisProperties.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class) public class MybatisAutoConfiguration {}这里我们看到@EnableConfigurationProperties(MybatisProperties.class)注解指定的类(他就是读取配置文件的类)
/** * 用于自定义默认设置的配置对象。如果指定了configLocation,则不使用此属性。 */ @NestedConfigurationProperty // 嵌套装配注解 private Configuration configuration;这个属性上面的注解上讲到:如果configLocation,则不使用此属性。
那么我们看他他抛出异常的位置: 异常从这里抛出,那么传过来的expression一定是为false的。
然后再到调用它处看: 这个逻辑看下来就是不能够两者都有,但是可以两者都没有,所以两种都有的情况下就会报这个错。