Improve type determination for factory beans during condition evaluation
Previously, BeanTypeRegistry did not correctly determine the type that would be created by a factory bean if that factory bean was returned from a bean method with arguments on a configuration class found via component scanning. The key difference is that bean definitions for bean methods on configuration classes found via component scanning use ASM-based metadata rather than reflection-based metadata. The ASM-based method data does not provide direct access to the Method that will create the bean. In this case, BeanTypeRegistry was falling back to looking for a method with the matching name and no arguments. Therefore, if the bean method had any arguments it would fail to find the method and would, therefore, be unable to determine the type of bean produced by the factory bean. This commit updates BeanTypeRegistry to use logic that is very similar to Spring Framework's ConstructorResolver's resolveFactoryMethodIfPossible method to locate the method that will produce the factory bean. It looks for a single method with the required name with any number of arguments. If it finds multiple methods with the required name and different arguments it returns null, just as ConstructorResolver does. Closes gh-6755pull/6855/head
parent
1dc231f771
commit
75c1e50c5a
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.springframework.boot.autoconfigure.condition.scan;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBeanTests.ExampleBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBeanTests.ExampleFactoryBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Configuration for a factory bean produced by a bean method on a configuration class
|
||||
* found via component scanning.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@Configuration
|
||||
public class ScannedFactoryBeanConfiguration {
|
||||
|
||||
@Bean
|
||||
public FactoryBean<ExampleBean> exampleBeanFactoryBean() {
|
||||
return new ExampleFactoryBean("foo");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.springframework.boot.autoconfigure.condition.scan;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBeanTests.ExampleFactoryBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Configuration for a factory bean produced by a bean method with arguments on a
|
||||
* configuration class found via component scanning.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@Configuration
|
||||
public class ScannedFactoryBeanWithBeanMethodArgumentsConfiguration {
|
||||
|
||||
@Bean
|
||||
public Foo foo() {
|
||||
return new Foo();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExampleFactoryBean exampleBeanFactoryBean(Foo foo) {
|
||||
return new ExampleFactoryBean("foo");
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue