整合大量配置在 application.properties 或 application.yml 的参数。
以 MybatisProperties 为例:
首先,在 application.properties 或 application.yml 进行配置:
#mybatis的配置 mybatis: config-location: classpath:/mybatis/mybatis-config.xml mapper-locations: - classpath*:com/jeeplus/modules/**/*Mapper.xml - classpath:/META-INF/admin-mybatis-mappings/*.xml - classpath:/META-INF/modeler-mybatis-mappings/*.xml type-aliases-package: com.jeeplus.modules configuration-properties: prefix: boolValue: TRUE blobType: BLOB接着,创建 MybatisProperties 类设置属性,提供注解 @ConfigurationProperties,设置注解属性 prefix 为配置的前缀。
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX) public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; private String configLocation; private String[] mapperLocations; private String typeAliasesPackage; private String typeHandlersPackage; ... }做了这些仍然不够,如何让属性类真正被 Spring 发现并赋值呢?
由此可见,Mybatis 选择的就是第三种方式