Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
miemieYaho committed Jun 15, 2020
1 parent 120678c commit 6eab36a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriverConfig;
import org.mybatis.scripting.velocity.VelocityLanguageDriver;
import org.mybatis.scripting.velocity.VelocityLanguageDriverConfig;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
Expand All @@ -31,6 +32,9 @@
import org.springframework.context.annotation.Configuration;

/**
* {@link EnableAutoConfiguration Auto-Configuration} for MyBatis's scripting language drivers.
* <p> copy from {@link org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration}</p>
*
* @author miemie
* @since 2019-10-22
*/
Expand All @@ -47,7 +51,6 @@ public class MybatisPlusLanguageDriverAutoConfiguration {
@ConditionalOnClass(FreeMarkerLanguageDriver.class)
@ConditionalOnMissingClass("org.mybatis.scripting.freemarker.FreeMarkerLanguageDriverConfig")
public static class LegacyFreeMarkerConfiguration {

@Bean
@ConditionalOnMissingBean
FreeMarkerLanguageDriver freeMarkerLanguageDriver() {
Expand All @@ -61,7 +64,6 @@ FreeMarkerLanguageDriver freeMarkerLanguageDriver() {
@Configuration
@ConditionalOnClass({FreeMarkerLanguageDriver.class, FreeMarkerLanguageDriverConfig.class})
public static class FreeMarkerConfiguration {

@Bean
@ConditionalOnMissingBean
FreeMarkerLanguageDriver freeMarkerLanguageDriver(FreeMarkerLanguageDriverConfig config) {
Expand All @@ -84,7 +86,6 @@ public FreeMarkerLanguageDriverConfig freeMarkerLanguageDriverConfig() {
@ConditionalOnMissingClass("org.mybatis.scripting.velocity.VelocityLanguageDriverConfig")
@SuppressWarnings("deprecation")
public static class LegacyVelocityConfiguration {

@Bean
@ConditionalOnMissingBean
org.mybatis.scripting.velocity.Driver velocityLanguageDriver() {
Expand All @@ -98,7 +99,6 @@ org.mybatis.scripting.velocity.Driver velocityLanguageDriver() {
@Configuration
@ConditionalOnClass({VelocityLanguageDriver.class, VelocityLanguageDriverConfig.class})
public static class VelocityConfiguration {

@Bean
@ConditionalOnMissingBean
VelocityLanguageDriver velocityLanguageDriver(VelocityLanguageDriverConfig config) {
Expand All @@ -116,7 +116,6 @@ public VelocityLanguageDriverConfig velocityLanguageDriverConfig() {
@Configuration
@ConditionalOnClass(ThymeleafLanguageDriver.class)
public static class ThymeleafConfiguration {

@Bean
@ConditionalOnMissingBean
ThymeleafLanguageDriver thymeleafLanguageDriver(ThymeleafLanguageDriverConfig config) {
Expand All @@ -129,5 +128,22 @@ ThymeleafLanguageDriver thymeleafLanguageDriver(ThymeleafLanguageDriverConfig co
public ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig() {
return ThymeleafLanguageDriverConfig.newInstance();
}

// This class provides to avoid the https://github.com/spring-projects/spring-boot/issues/21626 as workaround.
@SuppressWarnings("unused")
private static class MetadataThymeleafLanguageDriverConfig extends ThymeleafLanguageDriverConfig {

@ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf.dialect")
@Override
public DialectConfig getDialect() {
return super.getDialect();
}

@ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf.template-file")
@Override
public TemplateFileConfig getTemplateFile() {
return super.getTemplateFile();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public class MybatisPlusProperties {

public Resource[] resolveMapperLocations() {
return Stream.of(Optional.ofNullable(this.mapperLocations).orElse(new String[0]))
.flatMap(location -> Stream.of(getResources(location)))
.toArray(Resource[]::new);
.flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
}

private Resource[] getResources(String location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ public boolean isValid() {
return true;
}

@Override
protected List<String> list(URL url, String path) throws IOException {
String urlString = url.toString();
String baseUrlString = urlString.endsWith("/") ? urlString : urlString.concat("/");
Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
return Stream.of(resources)
.map(resource -> preserveSubpackageName(baseUrlString, resource, path))
.collect(Collectors.toList());
}

private String preserveSubpackageName(final String baseUrlString, final Resource resource, final String rootPath) {
private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
final String rootPath) {
try {
return rootPath + (rootPath.endsWith("/") ? "" : "/")
+ resource.getURL().toString().substring(baseUrlString.length());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
protected List<String> list(URL url, String path) throws IOException {
String urlString = url.toString();
String baseUrlString = urlString.endsWith("/") ? urlString : urlString.concat("/");
Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
return Stream.of(resources).map(resource -> preserveSubpackageName(baseUrlString, resource, path))
.collect(Collectors.toList());
}
}

0 comments on commit 6eab36a

Please sign in to comment.