@ConditionalOnClass

Reference: Spring Docs - @ConditionalOnClass

@ConditionalOnClass ๋ž€?

  • ํŠน์ • Class ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด Bean์„ ๋“ฑ๋กํ•œ๋‹ค

  • ์™œ ์“ฐ๋Š”๊ฐ€!

    • ํ•ด๋‹น annotation์ด ์ž‘์„ฑ๋˜๋Š” class ์˜์กด์„ฑ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ์„ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค

@ConditionalOnClass ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

  1. Class Level์— ํ•ด๋‹น annotation์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์„ค์ • Class ์ž์ฒด๊ฐ€ ํŠน์ • class ์กด์žฌ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋”ฐ๋ผ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™”๋  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค

  2. Method Level์— ํ•ด๋‹น annotation์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•ด๋‹น method๋ฅผ ํฌํ•จํ•˜๋Š” Bean์ด Classpath์— ์กด์žฌํ•  ์กฐ๊ฑด์„ ์ง€์ •ํ•œ๋‹ค

@ConditionalOnClass ์†์„ฑ

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

    /**
     * The classes that must be present. Since this annotation is parsed by loading class
     * bytecode, it is safe to specify classes here that may ultimately not be on the
     * classpath, only if this annotation is directly on the affected component and
     * <b>not</b> if this annotation is used as a composed, meta-annotation. In order to
     * use this annotation as a meta-annotation, only use the {@link #name} attribute.
     * @return the classes that must be present
     */
    Class<?>[] value() default {};

    /**
     * The classes names that must be present.
     * @return the class names that must be present.
     */
    String[] name() default {};

}

Optional Elements

  • value - Class<?>[]

    • ์กด์žฌํ•ด์•ผ๋งŒ ํ•˜๋Š” class๋“ค์„ ๋ช…์‹œ

  • name - String[]

    • ์กด์žฌํ•ด์•ผ๋งŒ ํ•˜๋Š” class name๋“ค์„ ๋ช…์‹œ

Last updated