- 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