spring注解使用

  • @Component/Service/Controller:管理bean
  • @Scope:bean范围,可以指定单例,多例,session,request,也可以指定动态代理模式。
@Component
@Scope(value = DefaultListableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ScopeProxyBean {

    public void test(){
        System.out.println("aaaa");
    }
}
  • @Conditional:定义类实现Conditional接口,bean被spring管理条件
public class MyCondition implements Condition {

    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment env = context.getEnvironment();
        String system = env.getProperty("os.name");
        System.out.println("系统环境为 ==="+system);
        // 系统环境在Windows才加载该bean到容器中
        if(system.contains("Windows")){
            return false;
        }
        return true;
    }
}

@Component
@Conditional(MyCondition.class)
public class TestCondition {
}
  • @AliasFor
  • @PropertySources与PropertySource
@Configuration
@ComponentScan(basePackages = {"com.example.demo.bean"})
//@Conditional(MyCondition.class)
@PropertySource("classpath:application.properties")
public class MainConfig {

    @Bean(autowire = Autowire.BY_NAME)
    public AwareBean awareBean(){
        return new AwareBean();
    }

    @Component
    class TestMainConfig{

    }

}

@Component
//@Conditional(MyCondition.class)
public class TestCondition {

    @Value("${server.port}")
    private String port;

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}
  • @Import/ImportSelector/DeferredImportSelector与ImportAware
    ImportAware:获取注解上的值,作用参考EnableAsync注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ProxyConfig.class)
public @interface ImportTest {
    String importTest() default "";
}

@Component
public class ProxyConfig implements ImportAware {

  AnnotationAttributes annotationAttributes;

  @Override
  public void setImportMetadata(AnnotationMetadata importMetadata) {
      annotationAttributes = AnnotationAttributes.fromMap(importMetadata.getAnnotationAttributes(ImportTest.class.getName()));
      System.out.println("=====" + annotationAttributes.getString("importTest"));
  }
}

Import与ImportSelector/DeferredImportSelector,可以导入bean,实现DeferredImportSelector有延迟效果。

public class ImportSelectorTest implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        System.out.println("ImportSelectorTest");
        return new String[]{"com.example.demo.bean.importAware.test1"};
    }
}
public class DeferredImportSelectorTest implements DeferredImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        System.out.println("DeferredImportSelectorTest");
        return new String[]{"com.example.demo.bean.importAware.test2"};
    }
}
// 启动类添加@Import({ImportSelectorTest.class, DeferredImportSelectorTest.class})

@DependsOn

@Component
@DependsOn({"dependB"})
public class DependA {

    @Autowired
    private DependB dependB;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 接上文,接着学习Spring的注解。本篇博客所学习注解基本都是spring-context对应jar包下的注...
    骑着乌龟去看海阅读 2,274评论 0 14
  • 什么是基于Java的Spring注解配置? 给一些注解的例子. 基于Java的配置,允许你在少量的Java注解的帮...
    jiangmo阅读 438评论 0 1
  • spring-context模块笔记,该模块是Spring容器的核心部分,继承关系和结构也很复杂,值得细细看 核心...
    begonia_rich阅读 3,858评论 0 1
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,606评论 28 53
  • 首先介绍下自己的背景: 我11年左右入市到现在,也差不多有4年时间,看过一些关于股票投资的书籍,对于巴菲特等股神的...
    瞎投资阅读 5,788评论 3 8