Spring Boot无法启动-ConfigurationProperties引发UnboundConfigurationPropertiesException

时间:2020-05-24 18:53:34

标签: spring-boot

我已将应用程序从spring-boot 1.5.9迁移到2.2.4,现在遇到以下问题:

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@1f5150ed type = com.domain.config.RolesConfig, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=false, ignoreUnknownFields=false, prefix=role, value=role)]] failed:

    Property: role.rolebuctravelwatchers
    Value: Admin
    Origin: "role.roleBucTravelWatchers" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolecommandcenter
    Value: Admin
    Origin: "role.roleCommandCenter" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.roleemployee
    Value: Student
    Origin: "role.roleEmployee" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolefacility
    Value: Admin
    Origin: "role.roleFacility" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolegm
    Value: Admin
    Origin: "role.roleGM" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolehr
    Value: Admin
    Origin: "role.roleHR" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolehrwatchers
    Value: Admin
    Origin: "role.roleHRWatchers" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolemanager
    Value: Admin
    Origin: "role.roleManager" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.roleofficead
    Value: Admin
    Origin: "role.roleOfficeAD" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    Property: role.rolewatcherpm
    Value: Admin
    Origin: "role.roleWatcherPM" from property source "class path resource [application-dev.properties]"
    Reason: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.

Action:

Update your application's configuration

21:27:24,446 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 76) MSC000001: Failed to start service jboss.deployment.unit."App-0.0.1-SNAPSHOT.war".undertow-deployment: org.jboss.msc.service.StartException in service jboss.deployment.unit."App-0.0.1-SNAPSHOT.war".undertow-deployment: java.lang.RuntimeException: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'rolesConfig': Could not bind properties to 'RolesConfig' : prefix=role, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Thread.java:748)
    at org.jboss.threads.JBossThread.run(JBossThread.java:485)
Caused by: java.lang.RuntimeException: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'rolesConfig': Could not bind properties to 'RolesConfig' : prefix=role, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:252)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:96)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:78)
    ... 8 more
Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'rolesConfig': Could not bind properties to 'RolesConfig' : prefix=role, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:110)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:96)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:152)
    at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:132)
    at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:92)
    at com.domain.Application.onStartup(Application.java:34)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
    at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:203)
    at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:185)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:250)
    ... 10 more
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:364)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:324)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:308)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:238)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:225)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBinder.bind(ConfigurationPropertiesBinder.java:89)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:107)
    ... 42 more
Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound.
    at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.checkNoUnboundElements(NoUnboundElementsBindHandler.java:83)
    at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.onFinish(NoUnboundElementsBindHandler.java:71)
    at org.springframework.boot.context.properties.bind.Binder.handleBindResult(Binder.java:340)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:321)
    ... 47 more

21:27:24,485 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "App-0.0.1-SNAPSHOT.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"App-0.0.1-SNAPSHOT.war\".undertow-deployment" => "java.lang.RuntimeException: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'rolesConfig': Could not bind properties to 'RolesConfig' : prefix=role, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    Caused by: java.lang.RuntimeException: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'rolesConfig': Could not bind properties to 'RolesConfig' : prefix=role, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'rolesConfig': Could not bind properties to 'RolesConfig' : prefix=role, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'role' to com.domain.config.RolesConfig
    Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [role.rolebuctravelwatchers,role.rolecommandcenter,role.roleemployee,role.rolefacility,role.rolegm,role.rolehr,role.rolehrwatchers,role.rolemanager,role.roleofficead,role.rolewatcherpm] were left unbound."}}

我有我的application-dev.properties文件:

#application users roles
role.roleGM=Admin
role.roleOfficeAD=Admin
role.roleFacility=Admin
role.roleManager=Admin
role.roleHR=Admin
role.roleHRWatchers=Admin
role.roleWatcherPM=Admin
role.roleEmployee=Student

role.roleCommandCenter=Admin
role.roleBucTravelWatchers=Admin

还有我的RolesConfig java类:

package com.domain.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.core.context.SecurityContextHolder;

@Configuration
@PropertySource("classpath:${spring.profiles.active}.properties")
@ConfigurationProperties(prefix = "role", ignoreUnknownFields = false)
public class RolesConfig {

    private final static String rolePrefix = "ROLE_";

    public static String roleGM;
    public static String roleOfficeAD;
    public static String roleFacility;
    public static String roleManager;
    public static String roleHR;
    public static String roleHRWatchers;
    public static String roleWatcherPM;
    public static String roleExternalWatcher;
    public static String roleEmployee;
    public static String roleCommandCenter;
    public static String roleBucTravelWatchers;

    public Set<String> getAllRoles() {
        return Arrays.asList(this.getClass().getDeclaredFields()).stream().map(field -> {
            try {
                return field.get(new String());
            } catch (IllegalArgumentException | IllegalAccessException e) {
                return null;
            }
        }).filter(p -> {
            return p != null && !p.equals("ROLE_");
        }).map(p -> {
            return p.toString();
        }).collect(Collectors.toCollection(HashSet<String>::new));
    }

    private static String getFormatedRole(String role) {
        return rolePrefix + role.toUpperCase();
    }

    public boolean isUserInRoles(String... arg) {
        List<String> roles = new ArrayList<String>(Arrays.asList(arg));
        return SecurityContextHolder.getContext()
                                    .getAuthentication()
                                    .getAuthorities()
                                    .stream()
                                    .map(p -> {
                                        return p.toString();
                                    }).anyMatch(p -> roles.contains(p));
    }

    public static String getRoleGM() {
        return roleGM;
    }

    public static void setRoleGM(String roleGM) {
        RolesConfig.roleGM = getFormatedRole(roleGM);
    }

    public static String getRoleOfficeAD() {
        return roleOfficeAD;
    }

    public static void setRoleOfficeAD(String roleOfficeAD) {
        RolesConfig.roleOfficeAD = getFormatedRole(roleOfficeAD);
    }

    public static String getRoleFacility() {
        return roleFacility;
    }

    public static void setRoleFacility(String roleFacility) {
        RolesConfig.roleFacility = getFormatedRole(roleFacility);
    }

    public static String getRoleManager() {
        return roleManager;
    }

    public static void setRoleManager(String roleManager) {
        RolesConfig.roleManager = getFormatedRole(roleManager);
    }

    public static String getRoleHR() {
        return roleHR;
    }

    public static void setRoleHR(String roleHR) {
        RolesConfig.roleHR = getFormatedRole(roleHR);
    }

    public static String getRoleHRWatchers() {
        return roleHRWatchers;
    }

    public static void setRoleHRWatchers(String roleHRWatchers) {
        RolesConfig.roleHRWatchers = getFormatedRole(roleHRWatchers);
    }

    public static String getRoleWatcherPM() {
        return roleWatcherPM;
    }

    public static void setRoleWatcherPM(String roleWatcherPM) {
        RolesConfig.roleWatcherPM = getFormatedRole(roleWatcherPM);
    }

    public static String getRoleExternalWatcher() {
        return roleExternalWatcher;
    }

    public static void setRoleExternalWatcher(String roleExternalWatcher) {
        RolesConfig.roleExternalWatcher = getFormatedRole(roleExternalWatcher);
    }

    public static String getRoleEmployee() {
        return roleEmployee;
    }

    public static void setRoleEmployee(String roleEmployee) {
        RolesConfig.roleEmployee = getFormatedRole(roleEmployee);
    }

    public static String getRoleCommandCenter() {
        return roleCommandCenter;
    }

    public static void setRoleCommandCenter(String roleCommandCenter) {
        RolesConfig.roleCommandCenter = getFormatedRole(roleCommandCenter);
    }

    public static String getRoleBucTravelWatchers() {
        return roleBucTravelWatchers;
    }

    public static void setRoleBucTravelWatchers(String roleBucTravelWatchers) {
        RolesConfig.roleBucTravelWatchers = getFormatedRole(roleBucTravelWatchers);
    }

}

删除@Configuration会在整个应用程序中引起其他错误,如我所见,有人建议这样做。 另外,删除ignoreUnknownFields会使应用程序完成构建,但是字段中的值为空。

我不明白为什么这不再起作用了。我可能找到的每个资源都做同样的事情。以前没有任何问题。有人可以指出我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

在春季启动时,不需要@EnableConfigurationProperties。但是,您确实需要为@ConfigurationProperties注释处理器

添加依赖项。

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor