diff --git a/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperAutoConfiguration.java b/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperAutoConfiguration.java index 2ef4db8..174608f 100644 --- a/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperAutoConfiguration.java +++ b/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperAutoConfiguration.java @@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Lazy; import java.util.List; @@ -44,8 +45,9 @@ */ @Configuration @ConditionalOnBean(SqlSessionFactory.class) -@EnableConfigurationProperties(PageHelperProperties.class) +@EnableConfigurationProperties(PageHelperPropertiesProcessKebabCase.class) @AutoConfigureAfter(MybatisAutoConfiguration.class) +@Import(PageHelperProperties.class) @Lazy(false) public class PageHelperAutoConfiguration implements InitializingBean { @@ -53,7 +55,7 @@ public class PageHelperAutoConfiguration implements InitializingBean { private final PageHelperProperties properties; - public PageHelperAutoConfiguration(List sqlSessionFactoryList, PageHelperProperties properties) { + public PageHelperAutoConfiguration(List sqlSessionFactoryList, PageHelperProperties properties, PageHelperPropertiesProcessKebabCase propertiesKC) { this.sqlSessionFactoryList = sqlSessionFactoryList; this.properties = properties; } diff --git a/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperProperties.java b/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperProperties.java index 10155f6..f5ddf7a 100644 --- a/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperProperties.java +++ b/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperProperties.java @@ -24,7 +24,7 @@ package com.github.pagehelper.autoconfigure; -import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; import java.util.Properties; @@ -33,11 +33,9 @@ * * @author liuzh */ -@ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX) +@Component public class PageHelperProperties extends Properties { - public static final String PAGEHELPER_PREFIX = "pagehelper"; - public Boolean getOffsetAsPageNum() { return Boolean.valueOf(getProperty("offsetAsPageNum")); } diff --git a/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperPropertiesProcessKebabCase.java b/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperPropertiesProcessKebabCase.java new file mode 100644 index 0000000..c28fa86 --- /dev/null +++ b/pagehelper-spring-boot-autoconfigure/src/main/java/com/github/pagehelper/autoconfigure/PageHelperPropertiesProcessKebabCase.java @@ -0,0 +1,173 @@ +package com.github.pagehelper.autoconfigure; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.Optional; + +/** + * 原来的{@link PageHelperProperties}继承了{@link java.util.Properties}
+ * 在使用中发现SpringBoot会直接将application.yml(application.properties)的配置名原样set到集合中, + * 这样会导致配置文件里的kebab-case风格配置映射到配置类中还是kebab-case而不是camelCase, + * 之后设置属性的时候就会因为找不到camelCase的配置导致失效。 + *
+ * 所有kebab-case风格的配置项都会产生这个问题,即配置不生效。 + *
+ * 这个类不继承{@link java.util.Properties},用于提前接收配置文件中的配置项, + * 由于没有继承{@link java.util.Properties},SpringBoot可以做正常的风格转换, + * 转换完成后再将camelCase风格的配置放到原来的{@link PageHelperProperties}中 + * + * @author showen + */ +@ConfigurationProperties(prefix = PageHelperPropertiesProcessKebabCase.PAGEHELPER_PREFIX) +public class PageHelperPropertiesProcessKebabCase { + + public static final String PAGEHELPER_PREFIX = "pagehelper"; + + private final PageHelperProperties properties; + private Boolean offsetAsPageNum; + private Boolean rowBoundsWithCount; + private Boolean pageSizeZero; + private Boolean reasonable; + private Boolean supportMethodsArguments; + private String dialect; + private String helperDialect; + private Boolean autoRuntimeDialect; + private Boolean autoDialect; + private Boolean closeConn; + private String params; + private Boolean defaultCount; + private String dialectAlias; + private String autoDialectClass; + + @Autowired + public PageHelperPropertiesProcessKebabCase(PageHelperProperties properties) { + this.properties = properties; + } + + public Boolean getOffsetAsPageNum() { + return offsetAsPageNum; + } + + public void setOffsetAsPageNum(Boolean offsetAsPageNum) { + this.offsetAsPageNum = offsetAsPageNum; + Optional.ofNullable(offsetAsPageNum).ifPresent(properties::setOffsetAsPageNum); + } + + public Boolean getRowBoundsWithCount() { + return rowBoundsWithCount; + } + + public void setRowBoundsWithCount(Boolean rowBoundsWithCount) { + this.rowBoundsWithCount = rowBoundsWithCount; + Optional.ofNullable(rowBoundsWithCount).ifPresent(properties::setRowBoundsWithCount); + } + + public Boolean getPageSizeZero() { + return pageSizeZero; + } + + public void setPageSizeZero(Boolean pageSizeZero) { + this.pageSizeZero = pageSizeZero; + Optional.ofNullable(pageSizeZero).ifPresent(properties::setPageSizeZero); + } + + public Boolean getReasonable() { + return reasonable; + } + + public void setReasonable(Boolean reasonable) { + this.reasonable = reasonable; + Optional.ofNullable(reasonable).ifPresent(properties::setReasonable); + } + + public Boolean getSupportMethodsArguments() { + return supportMethodsArguments; + } + + public void setSupportMethodsArguments(Boolean supportMethodsArguments) { + this.supportMethodsArguments = supportMethodsArguments; + Optional.ofNullable(supportMethodsArguments).ifPresent(properties::setSupportMethodsArguments); + } + + public String getDialect() { + return dialect; + } + + public void setDialect(String dialect) { + this.dialect = dialect; + Optional.ofNullable(dialect).ifPresent(properties::setDialect); + } + + public String getHelperDialect() { + return helperDialect; + } + + public void setHelperDialect(String helperDialect) { + this.helperDialect = helperDialect; + Optional.ofNullable(helperDialect).ifPresent(properties::setHelperDialect); + } + + public Boolean getAutoRuntimeDialect() { + return autoRuntimeDialect; + } + + public void setAutoRuntimeDialect(Boolean autoRuntimeDialect) { + this.autoRuntimeDialect = autoRuntimeDialect; + Optional.ofNullable(autoRuntimeDialect).ifPresent(properties::setAutoRuntimeDialect); + } + + public Boolean getAutoDialect() { + return autoDialect; + } + + public void setAutoDialect(Boolean autoDialect) { + this.autoDialect = autoDialect; + Optional.ofNullable(autoDialect).ifPresent(properties::setAutoDialect); + } + + public Boolean getCloseConn() { + return closeConn; + } + + public void setCloseConn(Boolean closeConn) { + this.closeConn = closeConn; + Optional.ofNullable(closeConn).ifPresent(properties::setCloseConn); + } + + public String getParams() { + return params; + } + + public void setParams(String params) { + this.params = params; + Optional.ofNullable(params).ifPresent(properties::setParams); + } + + public Boolean getDefaultCount() { + return defaultCount; + } + + public void setDefaultCount(Boolean defaultCount) { + this.defaultCount = defaultCount; + Optional.ofNullable(defaultCount).ifPresent(properties::setDefaultCount); + } + + public String getDialectAlias() { + return dialectAlias; + } + + public void setDialectAlias(String dialectAlias) { + this.dialectAlias = dialectAlias; + Optional.ofNullable(dialectAlias).ifPresent(properties::setDialectAlias); + } + + public String getAutoDialectClass() { + return autoDialectClass; + } + + public void setAutoDialectClass(String autoDialectClass) { + this.autoDialectClass = autoDialectClass; + Optional.ofNullable(autoDialectClass).ifPresent(properties::setAutoDialectClass); + } +}