Skip to content

Commit 43e01fb

Browse files
authored
Merge pull request #642 from kazuki43zoo/gh-635.support-spring-native
Change to support spring-native
2 parents ee81d55 + 965619c commit 43e01fb

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.mybatis.spring.mapper.MapperFactoryBean;
2626
import org.mybatis.spring.mapper.MapperScannerConfigurer;
2727
import org.springframework.beans.BeanUtils;
28+
import org.springframework.beans.factory.config.BeanDefinition;
2829
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2930
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3031
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -138,6 +139,9 @@ void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes a
138139

139140
builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages));
140141

142+
// for spring-native
143+
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
144+
141145
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
142146

143147
}

src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.mybatis.spring.mapper.MapperFactoryBean;
2222
import org.mybatis.spring.mapper.MapperScannerConfigurer;
2323
import org.springframework.beans.BeanUtils;
24+
import org.springframework.beans.factory.config.BeanDefinition;
2425
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2526
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2627
import org.springframework.beans.factory.support.BeanNameGenerator;
@@ -104,6 +105,9 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
104105
builder.addPropertyValue("defaultScope", element.getAttribute(ATTRIBUTE_DEFAULT_SCOPE));
105106
builder.addPropertyValue("basePackage", element.getAttribute(ATTRIBUTE_BASE_PACKAGE));
106107

108+
// for spring-native
109+
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
110+
107111
return builder.getBeanDefinition();
108112
}
109113

src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Optional;
2121
import java.util.Set;
2222

23+
import org.apache.ibatis.io.Resources;
2324
import org.apache.ibatis.session.SqlSessionFactory;
2425
import org.mybatis.logging.Logger;
2526
import org.mybatis.logging.LoggerFactory;
@@ -233,6 +234,13 @@ private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
233234
// the mapper interface is the original class of the bean
234235
// but, the actual class of the bean is MapperFactoryBean
235236
definition.getConstructorArgumentValues().addGenericArgumentValue(beanClassName); // issue #59
237+
try {
238+
// for spring-native
239+
definition.getPropertyValues().add("mapperInterface", Resources.classForName(beanClassName));
240+
} catch (ClassNotFoundException ignore) {
241+
// ignore
242+
}
243+
236244
definition.setBeanClass(this.mapperFactoryBeanClass);
237245

238246
definition.getPropertyValues().add("addToConfig", this.addToConfig);

src/test/java/org/mybatis/spring/annotation/MapperScanTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.mybatis.spring.mapper.AnnotatedMapper;
4040
import org.mybatis.spring.mapper.AppConfigWithDefaultPackageScan;
4141
import org.mybatis.spring.mapper.MapperInterface;
42+
import org.mybatis.spring.mapper.MapperScannerConfigurer;
4243
import org.mybatis.spring.mapper.MapperSubinterface;
4344
import org.mybatis.spring.mapper.child.MapperChildInterface;
4445
import org.mybatis.spring.type.DummyMapperFactoryBean;
@@ -112,6 +113,10 @@ void testDefaultMapperScan() {
112113
applicationContext.getBean("mapperSubinterface");
113114
applicationContext.getBean("mapperChildInterface");
114115
applicationContext.getBean("annotatedMapper");
116+
117+
assertThat(applicationContext
118+
.getBeanDefinition(applicationContext.getBeanNamesForType(MapperScannerConfigurer.class)[0]).getRole())
119+
.isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE);
115120
}
116121

117122
@Test

src/test/java/org/mybatis/spring/config/NamespaceTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.mybatis.spring.SqlSessionTemplate;
3434
import org.mybatis.spring.mapper.AnnotatedMapper;
3535
import org.mybatis.spring.mapper.MapperInterface;
36+
import org.mybatis.spring.mapper.MapperScannerConfigurer;
3637
import org.mybatis.spring.mapper.MapperSubinterface;
3738
import org.mybatis.spring.mapper.ScopedProxyMapper;
3839
import org.mybatis.spring.mapper.child.MapperChildInterface;
@@ -94,6 +95,11 @@ void testInterfaceScan() {
9495
applicationContext.getBean("mapperSubinterface");
9596
applicationContext.getBean("mapperChildInterface");
9697
applicationContext.getBean("annotatedMapper");
98+
99+
assertThat(applicationContext.getBeanFactory()
100+
.getBeanDefinition(applicationContext.getBeanNamesForType(MapperScannerConfigurer.class)[0]).getRole())
101+
.isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE);
102+
97103
}
98104

99105
@Test

src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,7 +110,16 @@ void testInterfaceScan() {
110110

111111
assertThat(Stream.of(applicationContext.getBeanDefinitionNames()).filter(x -> x.startsWith("scopedTarget")))
112112
.hasSize(1);
113-
113+
assertThat(applicationContext.getBeanDefinition("mapperInterface").getPropertyValues().get("mapperInterface"))
114+
.isEqualTo(MapperInterface.class);
115+
assertThat(applicationContext.getBeanDefinition("mapperSubinterface").getPropertyValues().get("mapperInterface"))
116+
.isEqualTo(MapperSubinterface.class);
117+
assertThat(applicationContext.getBeanDefinition("mapperChildInterface").getPropertyValues().get("mapperInterface"))
118+
.isEqualTo(MapperChildInterface.class);
119+
assertThat(applicationContext.getBeanDefinition("annotatedMapper").getPropertyValues().get("mapperInterface"))
120+
.isEqualTo(AnnotatedMapper.class);
121+
assertThat(applicationContext.getBeanDefinition("scopedTarget.scopedProxyMapper").getPropertyValues()
122+
.get("mapperInterface")).isEqualTo(ScopedProxyMapper.class);
114123
}
115124

116125
@Test

0 commit comments

Comments
 (0)