Skip to content

Commit

Permalink
🐛 Native Support mybatis.config-location
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxiaowei-com-cn committed Oct 17, 2024
1 parent 5ef758c commit c17619a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
# run: xcode-select --install

- name: Test with Spring Boot Native Latest
run: ./mvnw -V compile -Pnative native:compile -am -pl mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-annotation,mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-xml
run: ./mvnw -V clean compile -Pnative native:compile -am -pl mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-annotation,mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-xml

- name: Run Native Latest
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.ibatis.reflection.TypeParameterResolver;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.PropertyValue;
Expand All @@ -36,14 +38,26 @@
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

/**
* @since 3.0.4
*/
class MyBatisBeanFactoryInitializationAotProcessor
implements BeanFactoryInitializationAotProcessor, BeanRegistrationExcludeFilter {

private static final Logger logger = LoggerFactory.getLogger(MyBatisBeanFactoryInitializationAotProcessor.class);

private static final ResourceLoader RESOURCE_RESOLVER = new PathMatchingResourcePatternResolver();

private static final String CONFIG_LOCATION = MybatisProperties.MYBATIS_PREFIX + ".config-location";

private static final Set<Class<?>> EXCLUDE_CLASSES = new HashSet<>();

static {
Expand All @@ -58,6 +72,10 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
}
return (context, code) -> {
RuntimeHints hints = context.getRuntimeHints();

Environment environment = beanFactory.getBean(Environment.class);
configLocation(environment, hints);

for (String beanName : beanNames) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName.substring(1));
PropertyValue mapperInterface = beanDefinition.getPropertyValues().getPropertyValue("mapperInterface");
Expand All @@ -78,6 +96,19 @@ public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
return EXCLUDE_CLASSES.contains(registeredBean.getBeanClass());
}

private void configLocation(Environment environment, RuntimeHints hints) {
String configLocation = environment.getProperty(CONFIG_LOCATION);
if (StringUtils.hasText(configLocation)) {
Resource resource = RESOURCE_RESOLVER.getResource(configLocation);
if (resource.exists()) {
Stream.of(configLocation.replace(ResourceUtils.CLASSPATH_URL_PREFIX, ""))
.forEach(hints.resources()::registerPattern);
} else {
logger.error("{}: {} does not exist", CONFIG_LOCATION, configLocation);
}
}
}

private void registerMapperRelationships(Class<?> mapperInterfaceType, RuntimeHints hints) {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(mapperInterfaceType);
for (Method method : methods) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"resources": {
"includes": [
{ "pattern": "mybatis-config.xml" },
{ "pattern": "sample/mybatis/graalvm/xml/mapper/CityMapper.xml" },
{ "pattern": "sample/mybatis/graalvm/xml/mapper/HotelMapper.xml" }
]
Expand Down

0 comments on commit c17619a

Please sign in to comment.