Spring boot externalize config properties/messages on Java annotations

后端 未结 4 473
借酒劲吻你
借酒劲吻你 2021-01-04 12:37

Is there a way to read text from external properties file in spring without using the @Value annotation. Ex: application.properties

var.foo=\"he         


        
相关标签:
4条回答
  • 2021-01-04 13:21

    You can use a placeholder when you declare a bean in your configuration file.Here you can try an example : https://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/

    0 讨论(0)
  • Using the @Value("${property}") annotation to inject configuration properties can sometimes be cumbersome, so spring boot introduced the @ConfigurationProperties, this should be the solution what you want.

    Here is the reference doc.

    0 讨论(0)
  • 2021-01-04 13:27

    If you are using Spring Boot then you can try this method, without doing any special configuration

    @environment.getProperty('property')
    
    0 讨论(0)
  • 2021-01-04 13:41

    I don't think you can achieve this unless the underlying implementation of the annotations you're using either come with their own standard for i18n (like ValidationMessages.properties), or support Spring's MessageSource.

    In the case of the latter alternative, all you should have to do is to add your key/value pairs inside a messages.properties file and let the framework do the rest:

    messages.properties

    my.validation.error.message=Something went terribly wrong!
    

    SomeClass.java

    @MySpringCompatibleValidationAnnotation("my.validation.error.message")
    private String someVariable;
    

    Bottom line is, depending on the framework you're trying to use, it may or may not support this out-of-the-box.

    Now, as far as Swagger goes, i18n support for documentation was proposed as a new feature, but it was shut down or put on hold while they put some more thought into how it should be implemented. See https://github.com/OAI/OpenAPI-Specification/issues/274 for more details.

    0 讨论(0)
提交回复
热议问题