PropertySourcesPlaceholderConfigurer打破春天

时间:2019-02-09 14:11:23

标签: java spring spring-boot

编辑:已解决,请在下面查看我的评论。

项目组织:

my.package
   MyApplication.java
   spring
       MyConfiguration.java

MyApplication.java

@SpringBootApplication
class MyApplication {
    private final ApplicationContext applicationContext;

    MyApplication(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    ...

    public static void main(final String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

MyConfiguration.java

@Configuration
@ComponentScan(
        useDefaultFilters = false,
        basePackages = "...",
        includeFilters = @Filter(
                type = FilterType.REGEX,
                pattern = "..."
        )
)
class MyConfiguration {
    private final ApplicationContext context;

    MyConfiguration(final ApplicationContext context) {
        this.context = context;
    }

    @Bean
    PropertySourcesPlaceholderConfigurer configurer() {
        // For test purpose
        return new PropertySourcesPlaceholderConfigurer();
    }
}

此设置抛出:

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at my.package.MyApplication.main(MyApplication.java:163) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [my.package.spring.MyConfiguration$$EnhancerBySpringCGLIB$$d0d9c3b2]: No default constructor found; nested exception is java.lang.NoSuchMethodException: my.package.spring.MyConfiguration$$EnhancerBySpringCGLIB$$d0d9c3b2.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 26 common frames omitted
Caused by: java.lang.NoSuchMethodException: my.package.spring.MyConfiguration$$EnhancerBySpringCGLIB$$d0d9c3b2.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 27 common frames omitted

使用其他所有可能的Bean类型都会导致错误。 我要疯了。不明白为什么。

2 个答案:

答案 0 :(得分:0)

这里的问题不是PropertySourcesPlaceholderConfigurer,而是您覆盖了Configuration类的默认构造函数。 Spring需要一个至少具有包私有访问权限的默认构造函数来实例化该类。添加

MyConfiguration() {
}

进入MyConfiguration,一切将开始工作。

答案 1 :(得分:0)

配置中的构造函数可以具有参数,并且类可以是程序包私有的。

但是,您的PropertySourcesPlaceholderConfigurer bean必须是公共的和静态的。

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
    return new PropertySourcesPlaceholderConfigurer();
}

这对我有用(如果我从PropertySourcePlaceholderConfigurer中删除静态和/或公共属性,则失败):

@Configuration
public class WebConfiguration {

    private final ApplicationContext context;

    WebConfiguration(ApplicationContext context) {
       this.context = context;
    }      

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }
}