Spring.Reading from properties file

Any beanFactoryPostProcessor should be declared in @Configuration file with static, because configuration is being initialized in very begining.

Nothing needed, props a read from application.properties / application.yml file, but if you want to custmomize the file you can add annotation

@PropertySource("myapplication.properties")

Also we can use PropertySourcesPlaceholderConfigurer

By default we can read from resources/application.properties file, but we also can configure another one like this

create file myapplication.properties

mymessage=hello from app properties

configure spring

@Configuration
@PropertySource("myapplication.properties")
public class Config {
    @Bean
    public PropertySourcesPlaceholderConfigurer configurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

call mymessage from someService

@Service
public class SomeService {
    @Value("${mymessage}")
    private void init(String msg) {
        System.out.println(msg);
    }
}

and here we are

Also @ApplicationProperties can be used

This entry was posted in Без рубрики. Bookmark the permalink.