- 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