Properties in Spring Boot

47 Views Asked by At

I'm using a properties bean in Spring Boot. The output I always get is that Spring Boot could not call the property from the application.properties file. I have all my profile properties files in a separate folder and activated them in appliction.properties file, pasted the path to spring.config.name with the correct location:

spring.config.name = properties
spring.profiles.active = optimistic,

I guess if I want to have access to the application.properties file now, I have to define a new Properties object using my Bean classes:

  1. first one for FileInputstream: '''
package com.starter.tool.character.details;

import java.io.FileInputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class Streams {
    
    private FileInputStream input;
    
    Streams(@Value("src/main/resources/application.properties" )String filePath)throws IOException{
        this.input = new FileInputStream(filePath);
    }
    
    public FileInputStream getStream() {
        return input;
    }
    
    

}

'''

  1. second one for the Properties: '''

    package com.starter.tool.character.details;


import java.io.IOException;
import java.util.Properties;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class PropertyLoad {
    private final Properties props;

    
    PropertyLoad(Properties props){
        this.props = props;
        
    }

    @Bean
    @ConditionalOnBean(Streams.class)
    Properties defaultFormula(Streams input) {
        try {
            props.load(input.getStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return props;
    }
}

'''

I know that Spring Boot has its own Properties Object (startProperty), but this problem I solved with @Qualifier() Annotation. Please help me understand the problem, why I can't access the Properties object?

0

There are 0 best solutions below