@ConditionalOnProperty

Reference: Spring Docs - @ConditionalOnProperty

@ConditionalOnProperty ๋ž€?

  • Spring Framework์—์„œ ์–ด๋–ค Bean์ด ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ๊ตฌ์„ฑ๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์„ ์ง€์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์กฐ๊ฑด๋ถ€ annotation

  • ํŠน์ • property ๊ฐ’์˜ ์œ ๋ฌด๋‚˜ ๊ฐ’์— ๋”ฐ๋ผ Bean ์ƒ์„ฑ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

@ConditionalOnProperty ์˜ ์†์„ฑ


@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {

    /**
     * Alias for {@link #name()}.
     * @return the names
     */
    String[] value() default {};

    /**
     * A prefix that should be applied to each property. The prefix automatically ends
     * with a dot if not specified. A valid prefix is defined by one or more words
     * separated with dots (e.g. {@code "acme.system.feature"}).
     * @return the prefix
     */
    String prefix() default "";

    /**
     * The name of the properties to test. If a prefix has been defined, it is applied to
     * compute the full key of each property. For instance if the prefix is
     * {@code app.config} and one value is {@code my-value}, the full key would be
     * {@code app.config.my-value}
     * <p>
     * Use the dashed notation to specify each property, that is all lower case with a "-"
     * to separate words (e.g. {@code my-long-property}).
     * @return the names
     */
    String[] name() default {};

    /**
     * The string representation of the expected value for the properties. If not
     * specified, the property must <strong>not</strong> be equal to {@code false}.
     * @return the expected value
     */
    String havingValue() default "";

    /**
     * Specify if the condition should match if the property is not set. Defaults to
     * {@code false}.
     * @return if the condition should match if the property is missing
     */
    boolean matchIfMissing() default false;

}

Default value

  • property๊ฐ€ Environment ์— ์กด์žฌํ•˜๊ณ ,

  • false๊ฐ€ ์•„๋‹ˆ๋‹ค

Optional Elements

  • name (ํ•„์ˆ˜ ์†์„ฑ) - String[]

    • test ํ•˜๊ณ ์ž ํ•˜๋Š” property ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•œ๋‹ค

  • havingValue - String

    • ํ•ด๋‹น property๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š” attribute๋ฅผ ๋ช…์‹œํ•œ๋‹ค

      • property ๊ฐ’๊ณผ ๋น„๊ตํ•  ๊ฐ’์„ ์ง€์ •!

    • ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, default ๊ฐ’์€ empty string (โ€โ€)์ด๋‹ค

    • ํ•ด๋‹น ๊ฐ’์ด ์ง€์ •ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ empty string์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉด, property ๊ฐ’์ด ์–ด๋–ค ๊ฐ’์ด๋“  ์ƒ๊ด€ ์—†์ด property๊ฐ€ ์„ค์ •๋˜๊ธฐ๋งŒ ํ•˜๋ฉด Bean์ด ์ƒ์„ฑ๋œ๋‹ค

    • ๊ฐ’์ด ์ง€์ •๋œ ๊ฒฝ์šฐ (not empty string), ์ง€์ •ํ•œ ๊ฐ’๊ณผ property ๊ฐ’์ด ์ผ์น˜ํ•ด์•ผ Bean์ด ์ƒ์„ฑ๋œ๋‹ค

  • matchIfMissing - boolean

    • ํ•ด๋‹น property ๊ฐ€ ์•„์˜ˆ Environment์— ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค

    • default ๊ฐ’์€ false ์ด๊ณ , property๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์œผ๋ฉด Bean์ด ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค

    • true ๋กœ ์„ค์ •ํ•˜๋ฉด property๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋„ Bean์ด ์ƒ์„ฑ๋œ๋‹ค

  • prefix - String

    • ๊ฐ๊ฐ์˜ property์— ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” prefix๋ฅผ ์ง€์ •ํ•œ๋‹ค

    • ์ด ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ property๋“ค์— ๋Œ€ํ•ด ๊ณตํ†ต๋œ prefix๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

  • value - String[]

    • name() ์˜ alias

Last updated

Was this helpful?