美文网首页Spring Annotation
Spring bean 相关Annotation收集整理

Spring bean 相关Annotation收集整理

作者: 沥人土土 | 来源:发表于2017-04-11 17:59 被阅读0次
    @Component:

    声明类为component, 并告知Spring要为这个类创建bean。
    将bean标识为标识名:
    @Component("标识名")

    @Named("标识名"):

    Java DI规范提供的annotation,为bean设置id。

    Spring 支持将@Named作为@Component注解的替代方案。大多数场景可以互相替换。

    @ComponentScan:

    没有其他配置,默认扫描与配置类相同的包,查找本包及子包的@Component注解类。
    指定不同基础包:
    @ComponentScan("标识名")
    设置多个基础包:

    1. 使用String表示,(not type-safe)
      @ComponentScan(basePackages={"标识名","标识名"})
      2)指定为保重所包含的类或接口:
      @ComponentScan(basePackages={类名.class,类名.class})
    @Autowired:

    1)实例化,传入bean
    2)自动装配Setter等其他方法,如

    public void setXX(XX xx){
      this.xx = xx;
    }```
    如果没有匹配的bean,在应用上下文创建的时候Spring会抛出异常。避免异常可将@Autowired的required属性设置为false:
    ``@Autowired(required=false)``
    如果代码中没有进行null检查,这个处于未装配状态的属性有可能出现NullPointerException。
    
    @Autowired可替换为:@Inject(同@Named,来源于Java依赖注入规范)
    
    ######@Configuration:
    表明此类为配置类,该类应该包含在Spring应用上下文中如何创建bean的细节。
    
    ######@Bean:
    使用JavaConfig装配时,声明bean:
    

    @Bean
    public xx xX(){
    return new xX();
    }

    如果想将bean设置为不同的名字,可以使用:
    

    @Bean(name="yY")
    public xx xX(){
    return new xX();
    }

    在JavaConfig中装配声明依赖于某类的bean:
    

    @Bean
    public zz zZ(){
    return new zz(xX());
    }

    
    ######@Import:
    组合两个类
    1)``@Import(A.class)``于Bclass
    2)``@Import({A.class,B.class})``于Cclass
    
    ######@ImportResource:
    使Spring同时加载某类和其他基于Java的配置
    ``@ImportResource("classpath:xx-config.xml")
    
    ######@Profile:
    指定某个bean属于哪一个profile,为解决环境相关的问题。比如:
    

    @Configuration
    @profile("dev")
    public class DevelopmentProfileConfig{

    @Bean(destroyMethod="shutdown")
    public DataSource dataSource(){
        return new EmbeddedDatabaseBuilder()
              .setType(EmbeddedDatabaseType.H2)
              .addScript("classpath:schema.sql")
              .addScript("classpath:test-data.sql")
              .build();
    }
    

    }

    此例中代码会告诉Spring这个配置类中的bean只有在`dev`profile激活时才会创建。
    @profile 在Spring 3.1中启用,且只能在类级别上使用;在3.1开始,在方法级别上也可以使用。
    没有指定profile的bean始终会被创建。
    在Spring 4 中 @Profile的实现:
    

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Documented
    @Conditional(ProfileCondition.class)
    public @interface Profile{
    String[] value();
    }

    ######@Conditional:
    Spring 4引入的用到带有`@bean`注解方法上,判断条件结果为`true`就创建这个bean的annotation。用于希望一个或多个bean只有在应用的类路径下包含特定库时才创建,或只有某个特定环境变量设置后才会创建某个bean:
    ``@Bean
     @Conditional(XX.class)``
    @Conditional将通过Condition接口进行条件对比:
    

    public interface Condition{
    boolean matches(ConditionContext ctxt,
    AnnotatedTypeMetadata metadata);
    }

    实现类:
    

    public class MagicExistsCondition implements Condition{
    public boolean matches(
    ConditionContext ctxt,AnnotatedTypeMetadata metadata){
    Environment env = ctxt.getEnvironment();
    return env.containsProperty("magic"); //检查magic属性
    }
    }

    
    ######@Primary:
    用于消除自动装配的歧义性,同一接口下不能有两个或以上的primary:
    1)与@Component连用:
    

    @Component
    @Primary
    public class BB implements AA{...}

    2)与Bean连用:
    

    @Bean
    @Primary
    public AA BB(){
    return new BB;
    }

    
    @Qualifier:
    使用限定符消除自动装配的歧义性,可以与'@Autowired'和`@Inject`协同使用:
    

    @Autowired
    @Qualifier("自定义限定符名")
    public void setAA(AA aa){
    this.aa = aa;
    }

    
    @Scope:
    用于选择。指定bean的作用域,声明原型bean:
    与`@Component`:
    

    @Component
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class Notepad{ ... }

    使用ConfigurableBeanFactory类的SCOPE_PROTOTYPE常量设置了原型作用域。
    与`@Bean`指定作用域:
    

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Notepad notepad(){
    return new Notepad();
    }

    这样每次操作都能得到自己的Notepad实例。
    
    @Value
    用于在依赖于组件扫毛和自动装配来创建和初始化应用组件的情况,与@Autowired非常相似:
    

    public Book{
    @Value("${book.title}") String title,
    @Value("${book.artist}") String artist){
    this.title = title;
    this.artist = artist;
    }
    }

    相关文章

      网友评论

        本文标题:Spring bean 相关Annotation收集整理

        本文链接:https://www.haomeiwen.com/subject/jbdyattx.html