Spring多线程
- Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程
- ThreadPoolTaskExecutor实现一个基于线程池的TaskExecutor
- 配置类中@EnableAsync开启对异步任务的支持
- 使用@Async声明该任务为异步
①、配置类
java">@Configuration
@ComponentScan("com.xxx.taskExecutor")
@EnableAsync //开启异步任务
public class TaskExecutorConfig implements AsyncConfigurer{//获取一个基于线程池的TaskExecutor@Overridepublic Executor getAsyncExecutor(){ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();taskExecutor.setCorePoolSize(5);taskExecutor.setMaxPoolSize(10);taskExecutor.setQueueCapacity(25);taskExecutor.initialize();return taskExecutor;}@Overridepubic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){return null;}
}
②、任务执行类
这里的方法自动被注入使用ThreadPoolTaskExecutor作为TaskExecutor
java">@Service
public class AsyncTaskService{@Async //该方法是异步方法,如果注解到类上,标识该类所有方法都是异步的public void executeAsyncTask(Integer i){System.out.println("执行异步任务"+i);}@Asyncpublic void executeAsyncTaskPlus(Integer i){System.out.println("执行异步任务+1"+(i+1));}
}
③、运行
结果是并发执行,而不是顺序执行
java">public class Main{public static void main(String[] args){AnnotationConfigApplicationContext context = AnnotationConfigApplicationContext(TaskExecutorConfig.class);AsyncTaskService asyncTaskService = context.getBean(TaskExecutorConfig.class);for(int i = 0;i < 10;i++){asyncTaskService.executeAsyncTask(i);asyncTaskService.executeAsyncTaskPlus(i);}context.close();}
}
异步任务执行服务ExecutorService
任务的提交和任务的执行相分离
- 执行服务封装了任务执行的细节(线程创建、关闭,任务调度)
- 提交关注任务本身(提交任务、获取结果、取消任务)
java">public class BasicDemo{static class Task implements Callable<Integer>{int sleepSeconds = new Random().nextInt(1000);Thread.sleep(sleepSeconds);return sleepSeconds;}public static void main(String[] args){ExecutorService executor = Executors.newSingleThreadExecutor();Future<Integer> future = executor.submit(new Task());//模拟其他任务Thread.sleep(100);try{System.out.println(future.get());}catch(ExecutionException e){e.printStackTrace();}executor.shutdown();}
}
@Enable*注解
@EnableAspectJAutoProxy 开启对AspectJ自动代理的支持
@EnableAsync 开启异步方法的支持
@EnableScheduling 开启计划任务的支持
@EnableWebMvc 开启Web MVC的配置支持
@EnableConfigurationProperties开启对@ConfigurationProperties注解配置Bean的支持
@EnableJpaRepositories开启对Spring Data Repository的支持
@EnableTransactionManagement开启对注解式事务的支持
@EnableCaching开启注解式的缓存支持
以上所有开启功能的共性,都有一个@Import用来导入配置类
一、直接导入配置类
java">@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)//直接导入配置类
@Documented
public @interface EnableScheduling{}
二、依据条件选择配置类
java">@Target(ElementType.TYPE)
@Retention(RetentionPlicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)//通过条件来选择需要导入的
public @interface EnableAsync{Class<? extends Annotation> annotation() default Annotation.class;boolean proxyTargetClass() default false;AdviceMode mode() default AdviceMode.PROXY;int order() default Order.LOWEST_PRECEDENCE;
}
三、动态注册Bean
java">@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)//运行时自动添加Bean到已有的配置类
public @interface EnableAspectJAutoProxy{boolean proxyTargetClass() default false;
}
Spring测试
Spring通过Spring TestContext Framework对集成测试提供顶级支持
不依赖特定框架,既可以用Junit,也可以用TestNG
Spring提供了一个SpringJUnit4ClassRunner类
该类提供了Spring TestContext Framework的功能,通过@ContextConfiguration来配置Application Context
通过@ActiveProfiles确定活动的profile
①、依赖
spring-test junit
②、业务代码
java">public class TestBean{private String content;public TestBean(String content){super();this.content = content;}public String getContent(){return content;}public void setContent(String content){this.content = content;}
}
③、配置类
java">@Configuration
public class TestConfig{@Bean@Profile("dev")public TestBean devTestBean(){return new TestBean("from development profile");}@Bean@Profile("prod")public TestBean prodTestBean(){return new TestBean("from production profile");}
}
④、测试```java
@RunWith(SpringJUnit4ClassRunner.class)//JUnit环境下提供Spring TestContext Framework的功能
@ContextConfiguration(classes = {TestConfig.class})//用来加载配置ApplicationContext其中classes属性用来加载配置类
@ActiveProfiles("prod")//用来声明活动的profile
public class DemoBeanIntegrationTests{@Autowiredprivate TestBean testBean;@Testpublic void prodBeanShouldInject(){String expected = "from production profile";String actual = testBean.getContent();Assert.assertEquals(expected,actual);}
}