Spring常用注解总结(3)

时间:2023-03-09 04:59:13
Spring常用注解总结(3)

@Configuration

表示该类为“配置类”,可替换xml配置文件。与@Component不同的是,@Configuration会生成CGLIB代理class。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(annotation = Component.class)
String value() default "";
}

@Bean

主要定义在@Configuration注释的类中的方法上,而这些方法将会被AnnotationConfigApplicationContext 或 AnnotationConfigWebApplicationContext类进行扫描。

这个配置等同于在xml配置里配置的bean。

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
@Deprecated
Autowire autowire() default Autowire.NO;
boolean autowireCandidate() default true;
String initMethod() default "";
String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
}

@Lazy

主要用于修饰Spring Bean类,作用是指定该Bean是否取消预初始化。

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
boolean value() default true;
}

积累点点滴滴,一步一脚印,加油