Use `@AliasFor` when applicable
This commit adds `@AliasFor` meta-data to annotations that declare an alias attribute. `@ConditionalOnProperty` and `@AutoconfigureRestDocs` were not migrated due to the use of `AnnotationMetadata#getAnnotationAttributes`. Closes gh-5187
This commit is contained in:
parent
62a41aeed7
commit
06b81cf16f
|
@ -220,8 +220,7 @@ public class ConfigurationPropertiesReportEndpoint
|
||||||
annotation = override;
|
annotation = override;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (StringUtils.hasLength(annotation.value()) ? annotation.value()
|
return annotation.prefix();
|
||||||
: annotation.prefix());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -30,7 +30,6 @@ import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A registry for all {@link MvcEndpoint} beans, and a factory for a set of generic ones
|
* A registry for all {@link MvcEndpoint} beans, and a factory for a set of generic ones
|
||||||
|
@ -102,9 +101,7 @@ public class MvcEndpoints implements ApplicationContextAware, InitializingBean {
|
||||||
ConfigurationProperties configurationProperties = AnnotationUtils
|
ConfigurationProperties configurationProperties = AnnotationUtils
|
||||||
.findAnnotation(endpoint.getClass(), ConfigurationProperties.class);
|
.findAnnotation(endpoint.getClass(), ConfigurationProperties.class);
|
||||||
if (configurationProperties != null) {
|
if (configurationProperties != null) {
|
||||||
String prefix = StringUtils.hasText(configurationProperties.prefix())
|
return environment.getProperty(configurationProperties.prefix() + ".path");
|
||||||
? configurationProperties.prefix() : configurationProperties.value();
|
|
||||||
return environment.getProperty(prefix + ".path");
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Annotation for externalized configuration. Add this to a class definition or a
|
* Annotation for externalized configuration. Add this to a class definition or a
|
||||||
* {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate
|
* {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate
|
||||||
|
@ -44,6 +46,7 @@ public @interface ConfigurationProperties {
|
||||||
* for {@link #prefix()}.
|
* for {@link #prefix()}.
|
||||||
* @return the name prefix of the properties to bind
|
* @return the name prefix of the properties to bind
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("prefix")
|
||||||
String value() default "";
|
String value() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,6 +54,7 @@ public @interface ConfigurationProperties {
|
||||||
* for {@link #value()}.
|
* for {@link #value()}.
|
||||||
* @return the name prefix of the properties to bind
|
* @return the name prefix of the properties to bind
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("value")
|
||||||
String prefix() default "";
|
String prefix() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -325,10 +325,8 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
|
factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
|
||||||
factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
|
factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
|
||||||
factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
|
factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
|
||||||
String targetName = (StringUtils.hasLength(annotation.value())
|
if (StringUtils.hasLength(annotation.prefix())) {
|
||||||
? annotation.value() : annotation.prefix());
|
factory.setTargetName(annotation.prefix());
|
||||||
if (StringUtils.hasLength(targetName)) {
|
|
||||||
factory.setTargetName(targetName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -346,8 +344,7 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
StringBuilder details = new StringBuilder();
|
StringBuilder details = new StringBuilder();
|
||||||
details.append("prefix=").append((StringUtils.hasLength(annotation.value())
|
details.append("prefix=").append(annotation.prefix());
|
||||||
? annotation.value() : annotation.prefix()));
|
|
||||||
details.append(", ignoreInvalidFields=").append(annotation.ignoreInvalidFields());
|
details.append(", ignoreInvalidFields=").append(annotation.ignoreInvalidFields());
|
||||||
details.append(", ignoreUnknownFields=").append(annotation.ignoreUnknownFields());
|
details.append(", ignoreUnknownFields=").append(annotation.ignoreUnknownFields());
|
||||||
details.append(", ignoreNestedProperties=")
|
details.append(", ignoreNestedProperties=")
|
||||||
|
|
|
@ -88,8 +88,7 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector {
|
||||||
ConfigurationProperties annotation = AnnotationUtils.findAnnotation(type,
|
ConfigurationProperties annotation = AnnotationUtils.findAnnotation(type,
|
||||||
ConfigurationProperties.class);
|
ConfigurationProperties.class);
|
||||||
if (annotation != null) {
|
if (annotation != null) {
|
||||||
return (StringUtils.hasLength(annotation.value()) ? annotation.value()
|
return annotation.prefix();
|
||||||
: annotation.prefix());
|
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,9 +29,7 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
import org.springframework.core.annotation.AnnotationAttributes;
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base {@link ImportBeanDefinitionRegistrar} used to collect the packages to scan for a
|
* A base {@link ImportBeanDefinitionRegistrar} used to collect the packages to scan for a
|
||||||
|
@ -88,17 +86,9 @@ public abstract class AbstractEntityScanRegistrar
|
||||||
protected Set<String> getPackagesToScan(AnnotationMetadata metadata) {
|
protected Set<String> getPackagesToScan(AnnotationMetadata metadata) {
|
||||||
AnnotationAttributes attributes = AnnotationAttributes
|
AnnotationAttributes attributes = AnnotationAttributes
|
||||||
.fromMap(metadata.getAnnotationAttributes(this.annotationType.getName()));
|
.fromMap(metadata.getAnnotationAttributes(this.annotationType.getName()));
|
||||||
String[] value = attributes.getStringArray("value");
|
|
||||||
String[] basePackages = attributes.getStringArray("basePackages");
|
String[] basePackages = attributes.getStringArray("basePackages");
|
||||||
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
|
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
|
||||||
if (!ObjectUtils.isEmpty(value)) {
|
|
||||||
Assert.state(ObjectUtils.isEmpty(basePackages),
|
|
||||||
String.format(
|
|
||||||
"@%s basePackages and value attributes are mutually exclusive",
|
|
||||||
this.annotationType.getSimpleName()));
|
|
||||||
}
|
|
||||||
Set<String> packagesToScan = new LinkedHashSet<String>();
|
Set<String> packagesToScan = new LinkedHashSet<String>();
|
||||||
packagesToScan.addAll(Arrays.asList(value));
|
|
||||||
packagesToScan.addAll(Arrays.asList(basePackages));
|
packagesToScan.addAll(Arrays.asList(basePackages));
|
||||||
for (Class<?> basePackageClass : basePackageClasses) {
|
for (Class<?> basePackageClass : basePackageClasses) {
|
||||||
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
|
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.neo4j.ogm.annotation.NodeEntity;
|
||||||
import org.neo4j.ogm.session.SessionFactory;
|
import org.neo4j.ogm.session.SessionFactory;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the {@link SessionFactory} to scan for Neo4J {@link NodeEntity} classes in
|
* Configures the {@link SessionFactory} to scan for Neo4J {@link NodeEntity} classes in
|
||||||
|
@ -58,6 +59,7 @@ public @interface NodeEntityScan {
|
||||||
* {@code @NodeEntityScan(basePackages="org.my.pkg")}.
|
* {@code @NodeEntityScan(basePackages="org.my.pkg")}.
|
||||||
* @return the base packages to scan
|
* @return the base packages to scan
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("basePackages")
|
||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,6 +70,7 @@ public @interface NodeEntityScan {
|
||||||
* package names.
|
* package names.
|
||||||
* @return the base packages to scan
|
* @return the base packages to scan
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("value")
|
||||||
String[] basePackages() default {};
|
String[] basePackages() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,6 +56,7 @@ public @interface EntityScan {
|
||||||
* {@code @EntityScan(basePackages="org.my.pkg")}.
|
* {@code @EntityScan(basePackages="org.my.pkg")}.
|
||||||
* @return the base packages to scan
|
* @return the base packages to scan
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("basePackages")
|
||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,6 +67,7 @@ public @interface EntityScan {
|
||||||
* package names.
|
* package names.
|
||||||
* @return the base packages to scan
|
* @return the base packages to scan
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("value")
|
||||||
String[] basePackages() default {};
|
String[] basePackages() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -27,6 +27,7 @@ import javax.servlet.annotation.WebListener;
|
||||||
import javax.servlet.annotation.WebServlet;
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables scanning for Servlet components ({@link WebFilter filters}, {@link WebServlet
|
* Enables scanning for Servlet components ({@link WebFilter filters}, {@link WebServlet
|
||||||
|
@ -55,6 +56,7 @@ public @interface ServletComponentScan {
|
||||||
* {@code @ServletComponentScan(basePackages="org.my.pkg")}.
|
* {@code @ServletComponentScan(basePackages="org.my.pkg")}.
|
||||||
* @return the base packages to scan
|
* @return the base packages to scan
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("basePackages")
|
||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,6 +67,7 @@ public @interface ServletComponentScan {
|
||||||
* package names.
|
* package names.
|
||||||
* @return the base packages to scan
|
* @return the base packages to scan
|
||||||
*/
|
*/
|
||||||
|
@AliasFor("value")
|
||||||
String[] basePackages() default {};
|
String[] basePackages() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -28,14 +28,13 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
import org.springframework.core.annotation.AnnotationAttributes;
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ImportBeanDefinitionRegistrar} used by {@link ServletComponentScan}.
|
* {@link ImportBeanDefinitionRegistrar} used by {@link ServletComponentScan}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
|
class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
|
||||||
|
|
||||||
|
@ -77,16 +76,9 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
|
||||||
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
|
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
|
||||||
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
|
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
|
||||||
metadata.getAnnotationAttributes(ServletComponentScan.class.getName()));
|
metadata.getAnnotationAttributes(ServletComponentScan.class.getName()));
|
||||||
String[] value = attributes.getStringArray("value");
|
|
||||||
String[] basePackages = attributes.getStringArray("basePackages");
|
String[] basePackages = attributes.getStringArray("basePackages");
|
||||||
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
|
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
|
||||||
if (!ObjectUtils.isEmpty(value)) {
|
|
||||||
Assert.state(ObjectUtils.isEmpty(basePackages),
|
|
||||||
"@ServletComponentScan basePackages and value attributes are"
|
|
||||||
+ " mutually exclusive");
|
|
||||||
}
|
|
||||||
Set<String> packagesToScan = new LinkedHashSet<String>();
|
Set<String> packagesToScan = new LinkedHashSet<String>();
|
||||||
packagesToScan.addAll(Arrays.asList(value));
|
|
||||||
packagesToScan.addAll(Arrays.asList(basePackages));
|
packagesToScan.addAll(Arrays.asList(basePackages));
|
||||||
for (Class<?> basePackageClass : basePackageClasses) {
|
for (Class<?> basePackageClass : basePackageClasses) {
|
||||||
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
|
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EntityScan test annotation.
|
* EntityScan test annotation.
|
||||||
|
@ -35,8 +36,10 @@ import org.springframework.context.annotation.Import;
|
||||||
@Import(TestEntityScanRegistrar.class)
|
@Import(TestEntityScanRegistrar.class)
|
||||||
public @interface TestEntityScan {
|
public @interface TestEntityScan {
|
||||||
|
|
||||||
|
@AliasFor("basePackages")
|
||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
|
|
||||||
|
@AliasFor("value")
|
||||||
String[] basePackages() default {};
|
String[] basePackages() default {};
|
||||||
|
|
||||||
Class<?>[] basePackageClasses() default {};
|
Class<?>[] basePackageClasses() default {};
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.springframework.boot.context.scan.TestEntityScanRegistrar.TestFactory
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.annotation.AnnotationConfigurationException;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -75,10 +76,11 @@ public class TestEntityScanTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void valueAndBasePackagesThrows() throws Exception {
|
public void valueAndBasePackagesThrows() throws Exception {
|
||||||
this.thrown.expect(IllegalStateException.class);
|
this.thrown.expect(AnnotationConfigurationException.class);
|
||||||
this.thrown.expectMessage("@TestEntityScan basePackages and value "
|
this.thrown.expectMessage("attribute 'value' and its alias 'basePackages' are declared");
|
||||||
+ "attributes are mutually exclusive");
|
this.thrown.expectMessage("com.mycorp.entity");
|
||||||
this.context = new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
|
this.thrown.expectMessage("com.mycorp");
|
||||||
|
new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.annotation.AnnotationConfigurationException;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -74,14 +75,9 @@ public class ServletComponentScanRegistrarTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void packagesConfiguredWithBothValueAndBasePackages() {
|
public void packagesConfiguredWithBothValueAndBasePackages() {
|
||||||
this.thrown.expect(IllegalStateException.class);
|
this.thrown.expect(AnnotationConfigurationException.class);
|
||||||
this.thrown.expectMessage("@ServletComponentScan basePackages and value"
|
this.thrown.expectMessage("attribute 'value' and its alias 'basePackages' are declared");
|
||||||
+ " attributes are mutually exclusive");
|
new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
|
||||||
this.context = new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
|
|
||||||
ServletComponentRegisteringPostProcessor postProcessor = this.context
|
|
||||||
.getBean(ServletComponentRegisteringPostProcessor.class);
|
|
||||||
assertThat(postProcessor.getPackagesToScan())
|
|
||||||
.contains(getClass().getPackage().getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue