27
27
import org .apache .ibatis .reflection .TypeParameterResolver ;
28
28
import org .mybatis .spring .mapper .MapperFactoryBean ;
29
29
import org .mybatis .spring .mapper .MapperScannerConfigurer ;
30
+ import org .slf4j .Logger ;
31
+ import org .slf4j .LoggerFactory ;
30
32
import org .springframework .aot .hint .MemberCategory ;
31
33
import org .springframework .aot .hint .RuntimeHints ;
32
34
import org .springframework .beans .PropertyValue ;
36
38
import org .springframework .beans .factory .config .BeanDefinition ;
37
39
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
38
40
import org .springframework .beans .factory .support .RegisteredBean ;
41
+ import org .springframework .core .env .Environment ;
42
+ import org .springframework .core .io .Resource ;
43
+ import org .springframework .core .io .ResourceLoader ;
44
+ import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
39
45
import org .springframework .util .ReflectionUtils ;
46
+ import org .springframework .util .ResourceUtils ;
47
+ import org .springframework .util .StringUtils ;
40
48
41
49
/**
42
50
* @since 3.0.4
43
51
*/
44
52
class MyBatisBeanFactoryInitializationAotProcessor
45
53
implements BeanFactoryInitializationAotProcessor , BeanRegistrationExcludeFilter {
46
54
55
+ private static final Logger logger = LoggerFactory .getLogger (MyBatisBeanFactoryInitializationAotProcessor .class );
56
+
57
+ private static final ResourceLoader RESOURCE_RESOLVER = new PathMatchingResourcePatternResolver ();
58
+
59
+ private static final String CONFIG_LOCATION = MybatisProperties .MYBATIS_PREFIX + ".config-location" ;
60
+
47
61
private static final Set <Class <?>> EXCLUDE_CLASSES = new HashSet <>();
48
62
49
63
static {
@@ -58,6 +72,10 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
58
72
}
59
73
return (context , code ) -> {
60
74
RuntimeHints hints = context .getRuntimeHints ();
75
+
76
+ Environment environment = beanFactory .getBean (Environment .class );
77
+ configLocation (environment , hints );
78
+
61
79
for (String beanName : beanNames ) {
62
80
BeanDefinition beanDefinition = beanFactory .getBeanDefinition (beanName .substring (1 ));
63
81
PropertyValue mapperInterface = beanDefinition .getPropertyValues ().getPropertyValue ("mapperInterface" );
@@ -78,6 +96,19 @@ public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
78
96
return EXCLUDE_CLASSES .contains (registeredBean .getBeanClass ());
79
97
}
80
98
99
+ private void configLocation (Environment environment , RuntimeHints hints ) {
100
+ String configLocation = environment .getProperty (CONFIG_LOCATION );
101
+ if (StringUtils .hasText (configLocation )) {
102
+ Resource resource = RESOURCE_RESOLVER .getResource (configLocation );
103
+ if (resource .exists ()) {
104
+ Stream .of (configLocation .replace (ResourceUtils .CLASSPATH_URL_PREFIX , "" ))
105
+ .forEach (hints .resources ()::registerPattern );
106
+ } else {
107
+ logger .error ("{}: {} does not exist" , CONFIG_LOCATION , configLocation );
108
+ }
109
+ }
110
+ }
111
+
81
112
private void registerMapperRelationships (Class <?> mapperInterfaceType , RuntimeHints hints ) {
82
113
Method [] methods = ReflectionUtils .getAllDeclaredMethods (mapperInterfaceType );
83
114
for (Method method : methods ) {
0 commit comments