简体   繁体   中英

Spring Boot : YAML configuration not loaded to bean

role-permissions-mappings.yml

Content:

role-mapping-permissions: 
 rolePermissions: 
  - 
   role : 'role1'
   permissions: 
    -'role1'
    -'perm1'
    -'perm2'
  -     
   role: 'role2'
   permissions: 
    -'role2'
    -'perm1'
    -'perm2'

Configuration class:

@Configuration
@ConfigurationProperties(prefix="role-mapping-permissions")
@EnableConfigurationProperties
@Slf4j
public class RolePermissionMapping {
    
    private List<RoleDetails> rolePermissions = new ArrayList<>();
    
    @PostConstruct
    public void init(){
        if(!rolePermissions.isEmpty()){
            log.info("Role -Permission size {}",rolePermissions.size());
        }else{
            log.info("role-permission-mapping is not configured");
        }
    }
      
  
    public List<RoleDetails> getRolePermissions() {
        return rolePermissions;
    }

    public void setRolePermissions(List<RoleDetails> rolePermissions) {
        this.rolePermissions = rolePermissions;
    }
    
    public static class RoleDetails {
        private String role;
        private List<String> permissions = new ArrayList<String>();
        public RoleDetails(String role,List<String> permissions){
            this.role= role;
            this.permissions = permissions;
        }
        public String getRole() {
            return role;
        }
        public void setRole(String role) {
            this.role = role;
        }
        public List<String> getPermissions() {
            return permissions;
        }
        public void setPermissions(List<String> permissions) {
            this.permissions = permissions;
        }
        
    }
}

Expected Output should be: Above code show output role-permission-mapping is not configured instead of loading configuration and execute log.info("Role -Permission size {}",rolePermissions.size());

It need to use @Value annotation even after adding @PropertySource annotation. Is there any way to use without @Value annotation?

Finally I got solution:

It need to implement YamlPropertySourceFactory

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)
            throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());
        Properties properties = factory.getObject();
        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }

    
}

Config file seems changes with YamlPropertySourceFactory to use for @PropertySource and empty constructor implemented.

Update configuration class:

@Configuration
@ConfigurationProperties(prefix="role-mapping-permissions")
@PropertySource(value="classpath:role-permissions-mapping.yml",factory = YamlPropertySourceFactory.class)
@Slf4j
public class RolePermissionMapping {
    
    private List<RoleDetails> rolePermissions = new ArrayList<>();
    private Map<String,List<String>> rolePermissionMap;
   
    /*Empty constructor*/
    public RolePermissionMapping() {
    }
    
    @PostConstruct
    public void init(){
        if(!rolePermissions.isEmpty()){
            rolePermissionMap = new HashMap<String, List<String>>();
            rolePermissions.stream().forEach(rolePermissions->{
                rolePermissionMap.put(rolePermissions.getRole(), rolePermissions.getPermissions());
            });
        }else{
            log.debug("role-permission-mapping is not configured");
        }
    }

    public List<String> getPermissions(String role) {
        if(StringUtils.isEmpty(role) || !rolePermissionMap.entrySet().contains(role)){
            throw new ResourceNotFoundException("Role Name '"+role+"' not found.");
        }
        return rolePermissionMap.get(role);
        
    }


    public List<RoleDetails> getRolePermissions() {
        return rolePermissions;
    }

    public void setRolePermissions(List<RoleDetails> rolePermissions) {
        this.rolePermissions = rolePermissions;
    }

    @Configuration
    public static class RoleDetails {
        private String role;
        private List<String> permissions = new ArrayList<String>();
        
        /*Empty constructor*/
        public RoleDetails(){
            
        }
        
        public String getRole() {
            return role;
        }
        public void setRole(String role) {
            this.role = role;
        }
        public List<String> getPermissions() {
            return permissions;
        }
        public void setPermissions(List<String> permissions) {
            this.permissions = permissions;
        }
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM