From 965619ca47a83f22abcdd8ba692e9d6d2eb525fa Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Tue, 4 Jan 2022 23:27:24 +0900 Subject: [PATCH] Change to support spring-native * Set infrastructure role for MapperScannerConfigurer bean * Set the mapperInterface to MapperFactoryBean for accessing the interface type at bean definition post processing See gh-635 --- .../spring/annotation/MapperScannerRegistrar.java | 6 +++++- .../config/MapperScannerBeanDefinitionParser.java | 6 +++++- .../spring/mapper/ClassPathMapperScanner.java | 10 +++++++++- .../mybatis/spring/annotation/MapperScanTest.java | 7 ++++++- .../org/mybatis/spring/config/NamespaceTest.java | 8 +++++++- .../spring/mapper/MapperScannerConfigurerTest.java | 13 +++++++++++-- 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java b/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java index 6976e8f919..62157601a5 100644 --- a/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java +++ b/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 the original author or authors. + * Copyright 2010-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import org.mybatis.spring.mapper.MapperFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -138,6 +139,9 @@ void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes a builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages)); + // for spring-native + builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); + registry.registerBeanDefinition(beanName, builder.getBeanDefinition()); } diff --git a/src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java b/src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java index 10b41e4894..fc025fec01 100644 --- a/src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java +++ b/src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 the original author or authors. + * Copyright 2010-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import org.mybatis.spring.mapper.MapperFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanNameGenerator; @@ -104,6 +105,9 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa builder.addPropertyValue("defaultScope", element.getAttribute(ATTRIBUTE_DEFAULT_SCOPE)); builder.addPropertyValue("basePackage", element.getAttribute(ATTRIBUTE_BASE_PACKAGE)); + // for spring-native + builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); + return builder.getBeanDefinition(); } diff --git a/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java b/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java index aea7433c9a..a977302cd5 100644 --- a/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java +++ b/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 the original author or authors. + * Copyright 2010-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.util.Optional; import java.util.Set; +import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.logging.Logger; import org.mybatis.logging.LoggerFactory; @@ -233,6 +234,13 @@ private void processBeanDefinitions(Set beanDefinitions) { // the mapper interface is the original class of the bean // but, the actual class of the bean is MapperFactoryBean definition.getConstructorArgumentValues().addGenericArgumentValue(beanClassName); // issue #59 + try { + // for spring-native + definition.getPropertyValues().add("mapperInterface", Resources.classForName(beanClassName)); + } catch (ClassNotFoundException ignore) { + // ignore + } + definition.setBeanClass(this.mapperFactoryBeanClass); definition.getPropertyValues().add("addToConfig", this.addToConfig); diff --git a/src/test/java/org/mybatis/spring/annotation/MapperScanTest.java b/src/test/java/org/mybatis/spring/annotation/MapperScanTest.java index 3d4e2ea09a..e9a28c907a 100644 --- a/src/test/java/org/mybatis/spring/annotation/MapperScanTest.java +++ b/src/test/java/org/mybatis/spring/annotation/MapperScanTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 the original author or authors. + * Copyright 2010-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ import org.mybatis.spring.mapper.AnnotatedMapper; import org.mybatis.spring.mapper.AppConfigWithDefaultPackageScan; import org.mybatis.spring.mapper.MapperInterface; +import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.mybatis.spring.mapper.MapperSubinterface; import org.mybatis.spring.mapper.child.MapperChildInterface; import org.mybatis.spring.type.DummyMapperFactoryBean; @@ -112,6 +113,10 @@ void testDefaultMapperScan() { applicationContext.getBean("mapperSubinterface"); applicationContext.getBean("mapperChildInterface"); applicationContext.getBean("annotatedMapper"); + + assertThat(applicationContext + .getBeanDefinition(applicationContext.getBeanNamesForType(MapperScannerConfigurer.class)[0]).getRole()) + .isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE); } @Test diff --git a/src/test/java/org/mybatis/spring/config/NamespaceTest.java b/src/test/java/org/mybatis/spring/config/NamespaceTest.java index 11bd770501..bc88238fe6 100644 --- a/src/test/java/org/mybatis/spring/config/NamespaceTest.java +++ b/src/test/java/org/mybatis/spring/config/NamespaceTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 the original author or authors. + * Copyright 2010-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.mapper.AnnotatedMapper; import org.mybatis.spring.mapper.MapperInterface; +import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.mybatis.spring.mapper.MapperSubinterface; import org.mybatis.spring.mapper.ScopedProxyMapper; import org.mybatis.spring.mapper.child.MapperChildInterface; @@ -94,6 +95,11 @@ void testInterfaceScan() { applicationContext.getBean("mapperSubinterface"); applicationContext.getBean("mapperChildInterface"); applicationContext.getBean("annotatedMapper"); + + assertThat(applicationContext.getBeanFactory() + .getBeanDefinition(applicationContext.getBeanNamesForType(MapperScannerConfigurer.class)[0]).getRole()) + .isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE); + } @Test diff --git a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java index 27976c1361..c144270aca 100644 --- a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java +++ b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 the original author or authors. + * Copyright 2010-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -110,7 +110,16 @@ void testInterfaceScan() { assertThat(Stream.of(applicationContext.getBeanDefinitionNames()).filter(x -> x.startsWith("scopedTarget"))) .hasSize(1); - + assertThat(applicationContext.getBeanDefinition("mapperInterface").getPropertyValues().get("mapperInterface")) + .isEqualTo(MapperInterface.class); + assertThat(applicationContext.getBeanDefinition("mapperSubinterface").getPropertyValues().get("mapperInterface")) + .isEqualTo(MapperSubinterface.class); + assertThat(applicationContext.getBeanDefinition("mapperChildInterface").getPropertyValues().get("mapperInterface")) + .isEqualTo(MapperChildInterface.class); + assertThat(applicationContext.getBeanDefinition("annotatedMapper").getPropertyValues().get("mapperInterface")) + .isEqualTo(AnnotatedMapper.class); + assertThat(applicationContext.getBeanDefinition("scopedTarget.scopedProxyMapper").getPropertyValues() + .get("mapperInterface")).isEqualTo(ScopedProxyMapper.class); } @Test