TheDoctor
TheDoctor

Reputation: 2512

Use application.properties to initialize an object

I use @Autowired in my spring boot application to initialize some properties. I know how i can initialize primitive datatypes but i don't know how to initialize objects.

That's my Component:

@Component
@ConfigurationProperties(prefix = "bitmovin.bitmovin")
public class BitmovinConfig {

    private S3InputConfig S3InputConfig = new S3InputConfig();
    private int threadPoolSize;
    private ArrayList<String> testFiles;

    public BitmovinConfig() {
    }

    public S3InputConfig getS3InputConfig() {
        return S3InputConfig;
    }

    public void setS3InputConfig(S3InputConfig s3InputConfig) {
        S3InputConfig = s3InputConfig;
    }

    public int getThreadPoolSize() {
        return threadPoolSize;
    }

    public void setThreadPoolSize(int threadPoolSize) {
        this.threadPoolSize = threadPoolSize;
    }

    public ArrayList<String> getTestFiles() {
        return testFiles;
    }

    public void setTestFiles(ArrayList<String> testFiles) {
        this.testFiles = testFiles;
    }
}

And that's my application.propertiesfile:

bitmovin.bitmovin.threadPoolSize = 30
bitmovin.bitmovin.S3InputConfig = ??
bitmovin.bitmovin.testFiles= ??

How can i initialize an object or a list in application.properties?

UPDATE

The Object, which i didn't create myself, i want to initialize:

public class S3OutputConfig {
    @Expose
    public String name;
    @Expose
    public S3Region region;
    @Expose
    public String accessKey;
    @Expose
    public String secretKey;
    @Expose
    public String bucket;
    @Expose
    public String prefix;
    @Expose
    public boolean makePublic;
}

Upvotes: 3

Views: 8300

Answers (4)

Shawn Clark
Shawn Clark

Reputation: 3440

Another option is to use the @CondtionalOnProperty when constructing the @Bean. This way you can limit which beans are added to the context.

Upvotes: 0

Shawn Clark
Shawn Clark

Reputation: 3440

Normally when working with classes from libraries you are not in control of you can create a bean by doing something like this:

@Bean
S3OutputConfig s3OutputConfig(BitmovinConfig bitmovinConfig) {
    S3OutputConfig s3OutputConfig = new S3OutputConfig();

    // Do whatever else you want to setup the bean

    return s3OutputConfig;
}

This allows the flexibility to include other beans / configuration (they are autowired) that is needed to initialize the bean.

Upvotes: 0

Boris
Boris

Reputation: 512

It pretty simply to create and initialize embedded objects for properties bean.

E.g. if you have such class:

@Component
@ConfigurationProperties(prefix = "bitmovin.bitmovin")
public class BitmovinConfig {

    private S3OutputConfig outputConfig = new S3OutputConfig();

    public S3OutputConfig getOutputConfig() {
        return outputConfig;
    }
}

where S3OutputConfig structure:

public class S3OutputConfig {

    public String name;

    public S3Region region;

    public String accessKey;

    public String secretKey;

    public String bucket;

    public String prefix;

    public boolean makePublic;
}

You can initialize fields of outputConfig in this way:

bitmovin.bitmovin.output-config.name=Config Name
bitmovin.bitmovin.output-config.access-key=XAKJGSDIUGASASD
bitmovin.bitmovin.output-config.region=us-east-1

Upvotes: -1

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

For the list, you have a couple of ways:

bitmovin.bitmovin.testFiles=foo,bar,biz

will set 3 items in testFiles. You can also control things via an index. The code above can be rewritten as

bitmovin.bitmovin.testFiles[0]=foo
bitmovin.bitmovin.testFiles[1]=bar
bitmovin.bitmovin.testFiles[2]=biz

For the object, if you don't create it yourself in the code, you need to make sure it has a public default constructor. Then you navigate your object like any other object, using . to navigate. Assume your S3InputConfig has a name property (with getName and setName):

bitmovin.bitmovin.s3InputConfig.name=the name

You've seen use of lower-case hyphen a lot in Boot's documentation. We support the original format as well as hyphen lower case and others (see relaxed binding). The canonical representation for your config would be as follows:

bitmovin.bitmovin.test-files[0]=foo
bitmovin.bitmovin.test-files[1]=bar
bitmovin.bitmovin.test-files[2]=biz
bitmovin.bitmovin.s3-input-config.name=the name

Upvotes: 2

Related Questions