Day04:高級(jí)話題

  • 1、Spring Aware
    依賴注入->bean對(duì)spring容器的存在沒(méi)有意識(shí)->容器可替換
    實(shí)際->使用spring容器本身的功能資源->需要意識(shí)到spring,即spring aware->bean與spring框架耦合
    Aware接口
  • BeanNameAware:獲取bean的名稱
  • BeanFactoryAware:獲取bean factory
  • ApplicationContextAware*:可調(diào)服務(wù)
  • MessageSourceAware:
    -ResourceLoaderAware:獲得資源加載器
  • 2、多線程
  • spring通過(guò)taskexecutor實(shí)現(xiàn)多編程和并發(fā)
  • ThreadPoolTaskExecutor:線程池
  • @EnableAsync:開(kāi)啟對(duì)異步任務(wù)的支持,并在Bean方法中@Async注解聲明是異步任務(wù)
  • 3、計(jì)劃任務(wù)
  • 注解開(kāi)啟 @EnableScheduling
  • 計(jì)劃任務(wù)的類型:@Scheduled(xxx="yyy") 可選項(xiàng):cron指定時(shí)間,fixedRate間隔固定時(shí)間,fixDelay延遲指定時(shí)間
  • 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*注解,開(kāi)啟某項(xiàng)功能的支持
  • 直接導(dǎo)入配置類:@Import(SchedulingCOnfiguration.class)
  • 依據(jù)條件選擇配置類:@Import(AsyncConfigurationSelect.class),需要重寫(xiě)selectImports方法
  • 動(dòng)態(tài)注冊(cè)bean:@Import(AspectJAutoProxyRegistrar.class),實(shí)現(xiàn)了ImportBeanDefinitionRegistrar接口,參數(shù)AnnotationMedata(獲得當(dāng)前配置類上的注解)、BeanDefinitionRegistry(注冊(cè)bean)
  • 7、@RunWith(SpringJUnit4ClassRunner.class),提供sping testcontext framework的功能
    @ContextConfiguration //配置Application Context @ActiveProfile //配置活動(dòng)的profile
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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