Skip to content

Commit

Permalink
Merge pull request #20929 from encircled
Browse files Browse the repository at this point in the history
* pr/20929:
  Polish 'Allow beans without public constructors to load'
  Allow beans without public constructors to load

Closes gh-20929
  • Loading branch information
philwebb committed Jun 6, 2020
2 parents a0518d3 + c11abf4 commit 6e02049
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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 @@ -17,6 +17,7 @@
package org.springframework.boot;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -31,8 +32,6 @@
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Expand All @@ -41,9 +40,9 @@
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
Expand All @@ -53,6 +52,7 @@
* {@link SpringApplication} for the types of sources that are supported.
*
* @author Phillip Webb
* @author Vladislav Kisel
* @see #setBeanNameGenerator(BeanNameGenerator)
*/
class BeanDefinitionLoader {
Expand Down Expand Up @@ -153,7 +153,7 @@ private int load(Class<?> source) {
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader);
}
if (isComponent(source)) {
if (isEligible(source)) {
this.annotatedReader.register(source);
return 1;
}
Expand Down Expand Up @@ -273,16 +273,23 @@ private Package findPackage(CharSequence source) {
return Package.getPackage(source.toString());
}

private boolean isComponent(Class<?> type) {
// This has to be a bit of a guess. The only way to be sure that this type is
// eligible is to make a bean definition out of it and try to instantiate it.
if (MergedAnnotations.from(type, SearchStrategy.TYPE_HIERARCHY).isPresent(Component.class)) {
return true;
}
// Nested anonymous classes are not eligible for registration, nor are groovy
// closures
return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass()
&& type.getConstructors() != null && type.getConstructors().length != 0;
/**
* Check whether the bean is eligible for registration.
* @param type candidate bean type
* @return true if the given bean type is eligible for registration, i.e. not a groovy
* closure nor an anonymous class
*/
private boolean isEligible(Class<?> type) {
return !(type.isAnonymousClass() || isGroovyClosure(type) || hasNoConstructors(type));
}

private boolean isGroovyClosure(Class<?> type) {
return type.getName().matches(".*\\$_.*closure.*");
}

private boolean hasNoConstructors(Class<?> type) {
Constructor<?>[] constructors = type.getDeclaredConstructors();
return ObjectUtils.isEmpty(constructors);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import sampleconfig.MyComponentInPackageWithoutDot;

import org.springframework.boot.sampleconfig.MyComponent;
import org.springframework.boot.sampleconfig.MyNamedComponent;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.ClassPathResource;

Expand All @@ -31,6 +32,7 @@
* Tests for {@link BeanDefinitionLoader}.
*
* @author Phillip Webb
* @author Vladislav Kisel
*/
class BeanDefinitionLoaderTests {

Expand All @@ -53,6 +55,22 @@ void loadClass() {
assertThat(this.registry.containsBean("myComponent")).isTrue();
}

@Test
void anonymousClassNotLoaded() {
MyComponent myComponent = new MyComponent() {

};
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
assertThat(loader.load()).isEqualTo(0);
}

@Test
void loadJsr330Class() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class);
assertThat(loader.load()).isEqualTo(1);
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}

@Test
void loadXmlResource() {
ClassPathResource resource = new ClassPathResource("sample-beans.xml", getClass());
Expand Down Expand Up @@ -83,8 +101,9 @@ void loadGroovyResourceWithNamespace() {
@Test
void loadPackage() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage());
assertThat(loader.load()).isEqualTo(1);
assertThat(loader.load()).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}

@Test
Expand Down Expand Up @@ -113,8 +132,9 @@ void loadGroovyName() {
@Test
void loadPackageName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage().getName());
assertThat(loader.load()).isEqualTo(1);
assertThat(loader.load()).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}

@Test
Expand All @@ -131,8 +151,9 @@ void loadPackageNameWithoutDot() {
void loadPackageAndClassDoesNotDoubleAdd() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(),
MyComponent.class);
assertThat(loader.load()).isEqualTo(1);
assertThat(loader.load()).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2012-2020 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
*
* https://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.springframework.boot.sampleconfig;

import javax.inject.Named;

@Named
public class MyNamedComponent {

MyNamedComponent() {
}

}

0 comments on commit 6e02049

Please sign in to comment.