Skip to content

Commit

Permalink
Change to support spring-native
Browse files Browse the repository at this point in the history
* Set infrastructure role for MapperScannerConfigurer bean
* Set the mapperInterface to MapperFactoryBean for accessing the interface type at bean definition post processing

See gh-635
  • Loading branch information
kazuki43zoo committed Jan 4, 2022
1 parent ee81d55 commit 965619c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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());

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -233,6 +234,13 @@ private void processBeanDefinitions(Set<BeanDefinitionHolder> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/org/mybatis/spring/config/NamespaceTest.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 965619c

Please sign in to comment.