Spring boot properties to be loaded at initialization and respect all and control @Aspect based on the value from property file

后端 未结 1 596
庸人自扰
庸人自扰 2021-01-22 05:44

We are loading properties from an external file using @PropertySources. Now I want to enable/disable @Aspect based on a property. I tried using @

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 06:41

    It seems Spring by default loads some properties at initialization and unless until you specifically write logic to overwrite them (like the one I wrote in MainClass.java) there is no option to override those. Some of these include (logging.file, key used in @ConditionalonExpression).

    Some tricks with their own challenges:

    1. Specify the properties in application.properties in your classpath. The variables loaded at the earlier stages are always read from this file. challenge: I've tight coupled all my properties into the jar and in order to change the values I've to recompile and relaunch the Jar.
    2. Use profiles and define application.properties as application-profile.properties. challenge: I've to create so many profiles and still the previous challenge exists.
    3. Pass the property value as JVM parameter as -Dproperty.key=value. challenge:seriously? How many properties am I supposed to send as JVM parameter?
    4. Implement ApplicationContextInitialize and override initialize method.challenge:Overriding Spring's default behaviour is not recommended as well as isn't it an overkill to use this just for reading property file?

    Solution:

    Use -Dspring.config.location to specify the property files. In this case, always spring reads the properties only from the specified location(s). You can provide multiple property files as well. Refer this for much more details. It seems if you give property locations as Directories spring loads them in reverse order. But if you specify files it follows the order specified.

    Note: All these can be combined together. To know about precedence refer this.

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