• 关于spring的常用注解


    1,@Component
        作用:用于声明一个spring的组件。组件及spring容器中的bean,即一个实例。
        声明格式:
            @Target(ElementType.TYPE)   :表明该注解可以用在什么地方。 ElementType.TYPE:该注解可以用在类上
            @Retention(RetentionPolicy.RUNTIME)  该注解是在运行阶段有效
            @Documented     
            public @interface Component {
     
                /**
                * The value may indicate a suggestion for a logical component name,
                * to be turned into a Spring bean in case of an autodetected component.
                * @return the suggested component name, if any (or empty String otherwise)
                */
                String value() default "";  ->可以用于对该Component进行命名,一般情况下default都可以省略,省略的话即为value值,
            }
        用法:@Component(value="abc") @Component("abc")
     
     
    2,@Repository
        作用:用于声明一个spring的数据访问层组件。
        声明格式:
            @Target({ElementType.TYPE})    :表明该注解可以用在类上
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Component                     :Repository被Component修饰,表明该注解同Component一样,为spring的一个组件
            public @interface Repository {
     
                /**
                * The value may indicate a suggestion for a logical component name,
                * to be turned into a Spring bean in case of an autodetected component.
                * @return the suggested component name, if any (or empty String otherwise)
                */
                String value() default "";  ->可以用于对该Repository进行命名,一般情况下default都可以省略,省略的话即为value值,
            }
        用法:@Repository("aaa") @Repository(value="aaa")
     
    3,@Service
        作用:用于声明一个spring的业务处理层组件。
        声明格式:
            @Target({ElementType.TYPE})  :表明该注解可以用在类上
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Component                   :Service被Component修饰,表明该注解同Component一样,为spring的一个组件
            public @interface Service {
     
                /**
                * The value may indicate a suggestion for a logical component name,
                * to be turned into a Spring bean in case of an autodetected component.
                * @return the suggested component name, if any (or empty String otherwise)
                */
                String value() default "";
     
            }
        用法:@Service(value="aaaService") @Service("aaaService")
     
     
    4,@Configuration
        作用:用于声明一个spring的配置类。
        声明格式:
            @Target(ElementType.TYPE)      :表明该注解可以用在类上
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Component                     :Configuration被Component修饰,表明该注解同Component一样,为spring的一个组件
            public @interface Configuration {
     
                /**
                * Explicitly specify the name of the Spring bean definition associated
                * with this Configuration class. If left unspecified (the common case),
                * a bean name will be automatically generated.
                * <p>The custom name applies only if the Configuration class is picked up via
                * component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}.
                * If the Configuration class is registered as a traditional XML bean definition,
                * the name/id of the bean element will take precedence.
                * @return the suggested component name, if any (or empty String otherwise)
                * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator
                */
                String value() default "";
            }
     
    5,@ComponentScan
        作用:用于指定spring容器扫描的包或类
        声明格式:
            ......
        用法:
                @AliasFor("basePackages")  同basePackges
                String[] value() default {};           
     
                                                 value属性同basePackages作用一样,用于指定要扫描的包,包可以包含多个。
     
                @AliasFor("value")  同value
                String[] basePackages() default {};
                
                Class<?>[] basePackageClasses() default {};     :用于指定扫描的类,可以指定多个
     
                Filter[] includeFilters() default {};    :用于告诉spring容器将要去扫描的注解,即扫描includeFilters指定的注解,该注解可以包含多个
     
                Filter[] excludeFilters() default {};    :用于告诉spring容器将要排除扫描的注解,即排除excludeFilters指定的注解,该注解可以包含多个
     
    6,@Qualifier
        作用:用于限定一个spring容器中bean的标识,以便在自动注入的时候,使用限定的bean进行注入
              该注解配合Autowired使用
        声明格式:
     
            @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
            @Target表明了该注解可以用在什么地方 ->该注解可以用在属性、方法、参数、类、注解类
            @Retention(RetentionPolicy.RUNTIME)
            @Inherited
            @Documented
            public @interface Qualifier {
                String value() default "";
            }
        
        用法:
            @Qualifier(“xxx")  @Qualifier(value="xxx")
     
    7,@Autowired   ->效果等同于原生的java注解 @Resource,该注解可以直接指定限定的bean
        作用:构造器、属性、set方法、config 方法可以通过spring的依赖注入进行自动装配.该注解可以配合@Qualifier自动装配限定的bean.
              如果要装配限定的bean的话,得使用@Qualifier。该限定的bean可以被@Qualifier来指定,也可以被@Component、@Repository、@Service ...等注解的value属性来指定
        声明格式:
                @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
                -> 该注解可以用在构造器、方法、参数、属性、注解
                @Retention(RetentionPolicy.RUNTIME)
                @Documented
                public @interface Autowired {
     
                    /**
                    * Declares whether the annotated dependency is required.
                    * <p>Defaults to {@code true}.
                    */
                    boolean required() default true;    :表明该注解标注的域必须得被装配 ,默认情况下,该注解标注的域必须得被装配
     
                }    
        用法:@Autowired->必须得装配    @Autowired(required=false)->可不装配或可装配
     
    8,@PropertySource
        作用:用于将properties  {(xml、text、yml、grovvy)有待举证}的配置文件导入到spring容器中
        声明格式:
            @Target(ElementType.TYPE)            ->只能用在类上面
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Repeatable(PropertySources.class)
            public @interface PropertySource {
     
                ...
                String[] value();   :用于指定配置文件的路径,可以有多个
                ...
                String encoding() default "";  :用于指定编码格式
            }
     
    9,@ImportResource
        作用:用于将xml、grovvy配置文件导入到spring容器中
        声明格式:
            @Retention(RetentionPolicy.RUNTIME)
            @Target(ElementType.TYPE)
            @Documented
            public @interface ImportResource {
                
                @AliasFor("locations")
                String[] value() default {};      
                                        ===》指定配置文件的位置
                @AliasFor("value")
                String[] locations() default {};
            }
        用法:
            @ImportResource("classpath:/com/acme/properties-config.xml")
     
    10,@Profile
        作用:用于指定bean的使用环境,一般用于数据源,也可以用于控制其他bean的生成
        声明格式:
            @Target({ElementType.TYPE, ElementType.METHOD})  ->用于类或是方法上
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Conditional(ProfileCondition.class)
            public @interface Profile {
     
                /**
                * The set of profiles for which the annotated component should be registered.
                */
                String[] value();   ->用于指定bean的环境名称
     
            }
     
        用法:@Profile("dev") @Profile("prod")
    11,@ActiveProfiles
        作用:用于记过@Profile标明的环境
        声明格式:
            @Target({ElementType.TYPE})
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Inherited
            public @interface ActiveProfiles {
                @AliasFor("profiles")
                String[] value() default {};
     
                @AliasFor("value")
                String[] profiles() default {};
     
                Class<? extends ActiveProfilesResolver> resolver() default ActiveProfilesResolver.class;
     
                boolean inheritProfiles() default true;
            }
        用法:
            @ActiveProfiles("dev") @ActiveProfiles("online")
    12,@ContextConfiguration
        作用:用于指定spring容器加载的配置文件或配置类
        声明格式:
            @Target({ElementType.TYPE})
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            @Inherited
            public @interface ContextConfiguration {
                @AliasFor("locations")
                String[] value() default {};
                                  ======》用于指定配置文件的路径
                @AliasFor("value")
                String[] locations() default {};
     
                Class<?>[] classes() default {};  -> 用于指定加载的配置类 可以用多个
            }    
        用法:
            @ContextConfiguration(classes = DataSourceConfig.class)
            @ContextConfiguration(locations="/app-config.xml")
            
    13,@Bean
        作用:使用java的方式声明一个bean
        声明格式:
            @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})  ->可以用在方法上火是注解上
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            public @interface Bean {
     
                @AliasFor("name")
                String[] value() default {};
                                =====>用于指定bean的名字
                @AliasFor("value")
                String[] name() default {};
                
                Autowire autowire() default Autowire.NO;
                
                String initMethod() default "";   --->用户指定该bean的初始化方法
                
                String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;   ---->用于指定该bean的销毁方法
     
            }
        用法:
            @Bean(name = "aaa",initMethod="init",destroyMethod="destroy")
     
    14,@value
        作用:用于指定属性、方法、参数、注解的值,可以直接指定为字面量{@Value("abc")}或可以通过占位符的形式获取配置文件的属性值或者通过spel表达式获取属性值
        声明格式:
            @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
            @Retention(RetentionPolicy.RUNTIME)
            @Documented
            public @interface Value {
     
                /**
                * The actual value expression: e.g. "#{systemProperties.myProp}".
                */
                String value();
     
            }
        用法:字变量                           占位符                                 spel表达式
            @Value("abc")                @Value("${jdbc.driver})                #{systemProperties.myProp}
            String name;                 String jdbcDriver;                     String prop;
     
  • 相关阅读:
    Android开发需要注意的地方
    Android开发 --微信支付开发(转载!)(开发工具:Eclipse)
    Android开发---支付宝功能接口(支付功能)(转载!)
    Android开发--推送
    ASP.NET如何发布更新
    如何用visual studio控件(repeater)绑定数据库(SQL server)信息并显示
    SQL Server 的远程连接(转载)
    设置如何自动备份数据库并删除2周前的自动备份数据
    如何禁止网页内容被复制以及如何解除禁止(转载并更新!)
    条码生成
  • 原文地址:https://www.cnblogs.com/Iron-1995/p/9594406.html
Copyright © 2020-2023  润新知