Skip to content

Allow repeatable for @MapperScan #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/org/mybatis/spring/annotation/MapperScan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2016 the original author or authors.
* Copyright 2010-2017 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 @@ -18,6 +18,7 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand Down Expand Up @@ -70,6 +71,7 @@
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
@Repeatable(MapperScans.class)
public @interface MapperScan {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @author Michael Lanyon
* @author Eduardo Macarron
* @author Putthiphong Boonphong
*
*
* @see MapperFactoryBean
* @see ClassPathMapperScanner
* @since 1.2.0
Expand All @@ -65,8 +65,15 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes mapperScanAttrs = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
if (mapperScanAttrs != null) {
registerBeanDefinitions(mapperScanAttrs, registry);
}
}

void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegistry registry) {

AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

// this check is needed in Spring 3.1
Expand Down Expand Up @@ -117,4 +124,24 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
scanner.doScan(StringUtils.toStringArray(basePackages));
}

/**
* A {@link MapperScannerRegistrar} for {@link MapperScans}.
* @since 2.0.0
*/
static class RepeatingRegistrar extends MapperScannerRegistrar {
/**
* {@inheritDoc}
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
AnnotationAttributes mapperScansAttrs = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScans.class.getName()));
if (mapperScansAttrs != null) {
Arrays.stream(mapperScansAttrs.getAnnotationArray("value"))
.forEach(mapperScanAttrs -> registerBeanDefinitions(mapperScanAttrs, registry));
}
}
}

}
44 changes: 44 additions & 0 deletions src/main/java/org/mybatis/spring/annotation/MapperScans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2010-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.annotation;

import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The Container annotation that aggregates several {@link MapperScan} annotations.
*
* <p>Can be used natively, declaring several nested {@link MapperScan} annotations.
* Can also be used in conjunction with Java 8's support for repeatable annotations,
* where {@link MapperScan} can simply be declared several times on the same method,
* implicitly generating this container annotation.
*
* @author Kazuki Shimizu
* @since 2.0.0
* @see MapperScan
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.RepeatingRegistrar.class)
public @interface MapperScans {
MapperScan[] value();
}
34 changes: 34 additions & 0 deletions src/test/java/org/mybatis/spring/annotation/MapperScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,26 @@ void testScanWithExplicitSqlSessionTemplate() throws Exception {

}

@Test
void testScanWithMapperScanIsRepeat() {
applicationContext.register(AppConfigWithMapperScanIsRepeat.class);

startContext();

applicationContext.getBean("ds1Mapper");
applicationContext.getBean("ds2Mapper");
}

@Test
void testScanWithMapperScans() {
applicationContext.register(AppConfigWithMapperScans.class);

startContext();

applicationContext.getBean("ds1Mapper");
applicationContext.getBean("ds2Mapper");
}

@Configuration
@MapperScan("org.mybatis.spring.mapper")
public static class AppConfigWithPackageScan {
Expand Down Expand Up @@ -290,6 +310,20 @@ public static class AppConfigWithNameGenerator {
public static class AppConfigWithCustomMapperFactoryBean {
}

@Configuration
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1")
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds2")
public static class AppConfigWithMapperScanIsRepeat {
}

@Configuration
@MapperScans({
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1")
,@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds2")
})
public static class AppConfigWithMapperScans {
}

public static class BeanNameGenerator implements org.springframework.beans.factory.support.BeanNameGenerator {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2010-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.annotation.mapper.ds1;

public interface Ds1Mapper {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2010-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.annotation.mapper.ds2;

public interface Ds2Mapper {
}