Springc常用注解详解

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值