Skip to content
Advertisement

Spring Boot – Dynamically creating prototype Beans using properties

Is it possible to create prototype beans using some pattern in @Value annotation with properties / yaml configuration?

There is an example what I mean:
Example object:

@Component
@Scope("prototype")
public class SomeObject {

    @Value("${someobject.key.name}")
    String name;

    @Value("${someobject.key.address}")
    String address;

    @Value("${someobject.key.phone}")
    int phone;

    getters and setters
}

Example properties:

someobject.first.name = Phil
someobject.first.address = Berlin
someobject.first.phone = 123

someobject.second.name = Bill
someobject.second.address = New-York
someobject.second.phone = 321

I need to create two prototype beans
first with key “first”
second with key “second”
Ideal – if they will initialized like singletons on startup application

Advertisement

Answer

Yes you want to use Configuration Properties to allow binding of custom properties to a POJO.

package com.darrenforsythe.configurationpropertiesexample;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "properties")
public class MyProperties {



    private Map<String, Person>  someobject = new HashMap<>();

    public static class Person {
        /** Name of the Person */
        private String name;
        /** Address of the Person */
        private String address;
        /** Phone Number of the Person */
        private String phone;

        String getName() {
            return name;
        }

        void setName(String name) {
            this.name = name;
        }

        String getAddress() {
            return address;
        }

        void setAddress(String address) {
            this.address = address;
        }

        String getPhone() {
            return phone;
        }

        void setPhone(String phone) {
            this.phone = phone;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + ''' +
                    ", address='" + address + ''' +
                    ", phone='" + phone + ''' +
                    '}';
        }
    }

    Map<String, Person> getSomeobject() {
        return someobject;
    }

    void setSomeobject(Map<String, Person> someobject) {
        this.someobject = someobject;
    }

    @Override
    public String toString() {
        return "MyProperties{" +
                "someobject=" + someobject +
                '}';
    }
}

And enable add @EnableConfigurationProperties(MyProperties.class) to your main application or a @Configuration class.

AFter this you can inject the MyProperties to any Spring bean and use it as any other java object.

For example,

    @Bean
    ApplicationRunner printProperties(MyProperties myProperties) {
        return args -> myProperties
                .getSomeobject()
                .forEach((key, person) -> System.out.println(key + " - " + person));
    }

Will print each of the items.

Working example can be found here,

https://github.com/DarrenForsythe/configuration-properties-example

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement