Spring高级话题
Posted bigfly277
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring高级话题相关的知识,希望对你有一定的参考价值。
所有代码示例必须配置好Spring Spring项目的快速搭建.
1、Apring Aware
1.1、理论
Spring Aware的目的就是为了让Bean获得Spring容器的服务。
Spring 提供的Aware接口:
- BeanNameAware:获得容器中的Bean的名称;
- BeanFactoryAware:获得当前bean factory,这样容易调用容器服务;
- ApplicationContextAware*:获得当前application context,这样容易调用容器服务;
- MessageSourceAware: 获得message source,这样可以获得文本信息;
- ApplicationEventPublisher Aware:应用实践发布器,可以发布事件;
- ResourceLoaderAware: 获得资源加载器,可以获得外部资源文件;
1.2、示例
1)准备
新建一个text.tex,内容随意,用于测试资源加载
2)演示Bean
package com.wisely.highlight_spring4.ch3.aware;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{//实现接口,分别获得Bean名称和资源加载的服务
private String beanName;
private ResourceLoader loader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {//重写ResourceLoaderAware的setResourceLoader方法
this.loader = resourceLoader;
}
@Override
public void setBeanName(String name) {//重写BeanNameAware的setBeanName方法
this.beanName = name;
}
public void outputResult(){
System.out.println("Bean的名称是:" + beanName);
Resource resource =
loader.getResource("classpath:com/wisely/highlight_spring4/ch3/aware/test.txt");
try{
System.out.println("ResourceLoader加载的文件的内容为: " + IOUtils.toString(resource.getInputStream()));
}catch(IOException e){
e.printStackTrace();
}
}
}
3)配置类
package com.wisely.highlight_spring4.ch3.aware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.aware")
public class AwareConfig {
}
4)测试
package com.wisely.highlight_spring4.ch3.aware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AwareConfig.class);
AwareService awareService = context.getBean(AwareService.class);
awareService.outputResult();
context.close();
}
}
2、多线程
2.1、理论
Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程。使用TreadPoolTaskExecutor可以实现一个基于线程池的TaskExecutor。而实际开发中任务一般是非常阻碍的,即异步的,所以我们要在配置类中通过@EnableAsync开启对异步任务的支持,并通过在实际执行的Bean方法中使用@Asynv注解来声明其实一个异步任务。
2.2、示例
1)配置类
package com.wisely.highlight_spring4.ch3.taskexecutor;
import java.util.concurrent.Executor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch3.taskexecutor")
@EnableAsync //开启异步任务支持
public class TaskExecutorConfig implements AsyncConfigurer{//实现AsyncConfigurer接口
@Override
public Executor getAsyncExecutor() {//重写getAsyncExecutor返回一个ThreadPoolTaskExecutor,这样我们就获得了一个基于线程池的TaskExecutor
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
2)业务执行类
package com.wisely.highlight_spring4.ch3.taskexecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncTaskService {
@Async //表明这个方法是个异步方法,如果注解在类级别,则表明该类所有的方法都是异步方法,而这里的方法自动被注入使用ThreadPoolTaskExecutor作为TaskExecutor
public void executeAsyncTask(Integer i){
System.out.println("执行异步任务: "+i);
}
@Async
public void executeAsyncTaskPlus(Integer i){
System.out.println("执行异步任务+1: "+(i+1));
}
}
4)测试
package com.wisely.highlight_spring4.ch3.taskexecutor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
for(int i =0 ;i<5;i++){
asyncTaskService.executeAsyncTask(i);
asyncTaskService.executeAsyncTaskPlus(i);
}
context.close();
}
}
以上是关于Spring高级话题的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot实战笔记-- Spring高级话题(组合注解与元注解)
Spring Boot实战笔记-- Spring高级话题(计划任务)
Spring Boot实战笔记-- Spring高级话题(Spring Aware)