Java 元注解
@Target 表示该注解用于什么地方,可能的值在枚举类 ElemenetType 中,包括:
ElemenetType.CONSTRUCTOR-----------------------------构造器声明
ElemenetType.FIELD ----------------------------------域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE------------------------- 局部变量声明
ElemenetType.METHOD ---------------------------------方法声明
ElemenetType.PACKAGE --------------------------------包声明
ElemenetType.PARAMETER ------------------------------参数声明
ElemenetType.TYPE----------------------------------- 类,接口(包括注解类型)或enum声明
@Retention 表示在什么级别保存该注解信息。可选的参数值在枚举类型 RetentionPolicy 中,包括:
RetentionPolicy.SOURCE-------------注解将被编译器丢弃
RetentionPolicy.CLASS -------------注解在class文件中可用,但会被VM丢弃
RetentionPolicy.RUNTIME ---------VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
@Documented 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同。相当与@see,@param 等。
@Inherited 允许子类继承父类中的注解。
Spring 注解
- @SpringBootApplication注释等同于同时使用@Configuration,@EnableAutoConfiguration和@ComponentScan及其默认属性。
- @SpringBootConfiguration 标记次类是一个配置类。
- @EnableAutoConfiguration 里的@import注解默认将使用AutoConfigurationImportSelector加载spring-boot-autoconfigure jar下META-INF下的配置如:spring.factories,spring-configuration-metadata.json,spring-autoconfigure-metadata.properties,从而对应用进行自动配置.
- @ComponentScan, 指定应用扫描包的路径,如果没用指定,那么它默认将扫描标注该应用的class所在的package及其子package, 所以默认应用class一般都放在到工程package的顶层package.
- exclude, excludeName, 如果不想自动配置某些配置,可以用它们来排除,有些时候本地测试很有用,如下边配置,应用将不去配置org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,避免数据库连接检查导致应用无法启动
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class MyApplication {
}
@SpringBootApplication code:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "nameGenerator"
)
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
@Configuration 等价于, bean工厂用于生产bean, 使用此注解与@Bean配合更能灵活的生产bean
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
@EnableAutoConfiguration 见名知意,自动配置应用所需要的一些配置,其中@import注解将使用AutoConfigurationImportSelector加载spring-boot-autoconfigure jar下META-INF下的配置如:spring.factories,spring-configuration-metadata.json,spring-autoconfigure-metadata.properties,从而对应用进行自动配置
- exclude, excludeName, 如果不想自动配置某些配置,可以用它们来排除
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@ComponentScan 扫描某些包下或者指定的标有compnent注解的bean将其加载到Ioc容器中。
- basePackages 指定加载的包路径
- basePackageClasses 指定加载的类
- lazyInit 是否执行懒加载,默认不开启。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
String resourcePattern() default "**/*.class";
boolean useDefaultFilters() default true;
ComponentScan.Filter[] includeFilters() default {};
ComponentScan.Filter[] excludeFilters() default {};
boolean lazyInit() default false;
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Filter {
FilterType type() default FilterType.ANNOTATION;
@AliasFor("classes")
Class<?>[] value() default {};
@AliasFor("value")
Class<?>[] classes() default {};
String[] pattern() default {};
}
}
@EnableConfigurationProperties, 通过@Import 的EnableConfigurationPropertiesRegistrar将value中指定的配置注入到Ioc容器中,此配置一般与ConfigurationProperties配合使用
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EnableConfigurationPropertiesRegistrar.class})
public @interface EnableConfigurationProperties {
String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";
Class<?>[] value() default {};
}
@ConfigurationPropertiesScan
它可以扫描特定包下所有的被@ConfigurationProperties标记的配置类,使用Import将它们注入到Ioc容器中。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ConfigurationPropertiesScanRegistrar.class})
@EnableConfigurationProperties
public @interface ConfigurationPropertiesScan {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
}
@ConfigurationProperties 标记在配置类文件上,将配置类的属性与应用程序的配置相互匹配。
- prefix 配置信息的包前缀。
- ignoreInvalidFields 是否忽略无效的属性值,验证配置文件中属性值与配置类中属性的类型是否匹配
- ignoreUnknownFields 是否忽略未知属性。
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
@AliasFor("prefix")
String value() default "";
@AliasFor("value")
String prefix() default "";
boolean ignoreInvalidFields() default false;
boolean ignoreUnknownFields() default true;
}
@Bean, 注解一般放到注解标记为Configuration的类中标记方法上,一般用于配置第三方Bean, 将其载入到IOC容器中.
- name bean名称,如果不写会默认为注解的方法名称
- value – bean别名和name是相互依赖关联的,value,name如果都使用的话值必须要一致
- autowireCandidate 自动注入是是否作为候选bean,如果为false, @Autowired byType将无法获得bean, 但是通过byName可以获得bean, 可以通过@Resource的byName方式获得bean.
- initMethod – bean的初始化之前的执行方法,该参数一般不怎么用,因为可以完全可以在代码中实现
- destroyMethod – 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 */
@Deprecated
Autowire autowire() default Autowire.NO;
boolean autowireCandidate() default true;
String initMethod() default "";
String destroyMethod() default "(inferred)";
}
@Autowired, 注解通过类型byType进行依赖注入,如果注入中查找到多个实现bean,将依据变量的名字作为bean名字在IOC中查找bean进行注入,此注解可以在构造函数,方法,参数,属性和注解中使用,可以结合@Qualifier来配合使用通过byName来注入Bean.
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
@Resource 默认按照ByName自动注入,由J2EE提供,需要导入包javax.annotation.Resource。@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。
@Resource顺序:
1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。
2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常。
3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或是找到多个,都会抛出异常。
4. 如果既没有指定name,又没有指定type,则按照byName方式进行注入;如果没有匹配,则使用byType进行注入。
@Resource的作用相当于@Autowired,@Autowired按照byType自动注入, @Resource更加灵活。
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Resources.class)
public @interface Resource {
String name() default "";
String lookup() default "";
Class<?> type() default Object.class;
Resource.AuthenticationType authenticationType() default Resource.AuthenticationType.CONTAINER;
boolean shareable() default true;
String mappedName() default "";
String description() default "";
public static enum AuthenticationType {
CONTAINER,
APPLICATION;
private AuthenticationType() {
}
}
}
@Lazy, 懒加载bean,只有在调用时才载入bean, 此方法可以用于某些类避免启动时检查出错造成应用无法启动。
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
boolean value() default true;
}
@Qualifier, 此注解通常月@Autowired配合使用,byType找到多个实现类无法确定使用哪个bean,这时就通过@Qualifier中指定的名字来定位某一个bean.
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {
String value() default "";
}