Day04:高級話題

  • 1、Spring Aware
    依賴注入->bean對spring容器的存在沒有意識->容器可替換
    實際->使用spring容器本身的功能資源->需要意識到spring,即spring aware->bean與spring框架耦合
    Aware接口
  • BeanNameAware:獲取bean的名稱
  • BeanFactoryAware:獲取bean factory
  • ApplicationContextAware*:可調(diào)服務
  • MessageSourceAware:
    -ResourceLoaderAware:獲得資源加載器
  • 2、多線程
  • spring通過taskexecutor實現(xiàn)多編程和并發(fā)
  • ThreadPoolTaskExecutor:線程池
  • @EnableAsync:開啟對異步任務的支持,并在Bean方法中@Async注解聲明是異步任務
  • 3、計劃任務
  • 注解開啟 @EnableScheduling
  • 計劃任務的類型:@Scheduled(xxx="yyy") 可選項:cron指定時間,fixedRate間隔固定時間,fixDelay延遲指定時間
  • 4、@Conditional 基于條件創(chuàng)建bean
    @Bean @Conditional(windowsCondition.class) ...

  • 4.1、判斷win
    context.getEnvironment().getProperty("os.name").contains("windows");

  • 5、組合注解與元注解

  • 組合注解

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration //組合1 @ComponentScan //組合2 public @interface WiselyConfiguration { String[] value() default {}; //覆蓋value參數(shù) }

使用: @WiselyConfiguratipn("packageName")

  • 6、@Enable*注解,開啟某項功能的支持
  • 直接導入配置類:@Import(SchedulingCOnfiguration.class)
  • 依據(jù)條件選擇配置類:@Import(AsyncConfigurationSelect.class),需要重寫selectImports方法
  • 動態(tài)注冊bean:@Import(AspectJAutoProxyRegistrar.class),實現(xiàn)了ImportBeanDefinitionRegistrar接口,參數(shù)AnnotationMedata(獲得當前配置類上的注解)、BeanDefinitionRegistry(注冊bean)
  • 7、@RunWith(SpringJUnit4ClassRunner.class),提供sping testcontext framework的功能
    @ContextConfiguration //配置Application Context @ActiveProfile //配置活動的profile
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容