Spring小结
一.环境搭建
创建Maven项目
- 一般pom.xml会出错,本地若无相应版本的jar包,则无法下载或下载速度非常慢,我的解决方案是,查找本地仓库的jar,修改为本地仓库有的jar即可
- pom.xml的依赖jar可以从Maven搜索
二.Spring的 IOC(控制反转)与DI(依赖注入)
IOC(把Bean 的创建与管理交给Spring容器)
- 使用@Component,@Service,@Controller,@Repository 注解Bean ,让spring管理,四个功能等效,但应该在相对应的组件上标识
- @Bean方式,在配置类中(有@Configuration注解的类) 用@Bean注解的方法的返回值类型的Bean会被spring管理
DI 注入相应的Bean
- @Autowired,@Inject, @Resource,可以注解在属性或者Set方法上(我一般注解在属性上,这样可以省去set方法)
三.配置类的注解
- @Configuration标识该类为配置类
- @ComponentScan(“包名”)让spring扫描该包下的被注解为Bean的类
四.AOP(切面编程,基于cglib动态代理或者JDK动态代理)
- @Aspect标注类为切面类
- @PoinCut(“execution( com.aop.serivce.*(..))”) 定义切点,被注解的方法名为切点名称
- @Before前置通知,被标注的方法会在目标方法前执行
- @After后置通知,被注解的方法会在目标方法执行过执行
- @AfterReturning返回通知,在目标方法执行后切没有异常时执行
- @AfterThrowing 异常通知,在目标方法执行抛异常时执行
- 使用时要在配置类上注解@EnableAspectJAutoProxy,开启spring对AspectJ 的支持
五.常用配置
@Scope常用 有两种取值 Singleton 和 Prototype
- Singleton 为默认值 Bean我单例
- Prototype Bean为多例
配置文件读取
- 在配置类上注解@ProperSource(“文件路径”)
- 用@Value(“${key}”)注入配置文件的值
Bean的初始化和销毁
- @PostConstruct 注解的方法会在Bean的构造方法执行后执行
- @PreDestroy 注解的方法会在Bean销毁前执行
- @profile(“参数值”)注解Bean,可更具@ActiveProfiles(“参数值”)的参数值创建相应的Bean
六.多线程
- 使用@EnableAsync 注解配置类开启对异步任务的支持
- 并且要实现AsyncConfiguer接口
- 创建一个线程池
- 在方法上注解@Async 就说明这个方法为异步方法
@Configuration
@ComponentScan("com.sjx.spring2.multithread")
@EnableAsync//开启异步支持
public class TaskExecutorConfig implements AsyncConfigurer{
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
七.计划任务
- 在配置类添加注解@EnableSchedule 开启对计划任务的支持
- 在方法上注解@Scheduled申明方法为计划方法
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("当前时间为"+dateFormat.format(new Date()));
}
@Scheduled(cron="0 17 10 ? * *")//每天的10点17
public void fixTimeExecution() {
System.out.println("当定时时间"+dateFormat.format(new Date())+"执行");
}
八.集成Junit
- @RunWith(SpringJUnit4ClassRunner.class)提供spring测试的上下文环境
- @Test标识方法为测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AwareConfig.class})
public class AwareTest {
@Autowired
private AwareService service;
@Testpublic void test1(){
service.outputResult();
}
}
Spring小结的更多相关文章
- spring 小结
第一步:配置 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...
- Spring mvc中@RequestMapping 6个基本用法小结(转载)
小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...
- Spring boot中使用springfox来生成Swagger Specification小结
Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...
- spring security 3中的10个典型用法小结
spring security 3比较庞大,但功能很强,下面小结下spring security 3中值得 注意的10个典型用法 1)多个authentication-provide可以同时使用 &l ...
- spring mvc中的拦截器小结 .
在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下. preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Control ...
- Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结
Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...
- Spring mvc中@RequestMapping 6个基本用法小结
Spring mvc中@RequestMapping 6个基本用法小结 小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMa ...
- Spring归纳小结(山东数漫江湖)
前言 如果说有什么框架是Java程序员必然会学习.使用到的,那么Spring肯定是其中之一.本篇博客,将根据博主在日常工作中对Spring的使用做一个系统的归纳小结. Spring的一些概念和思想 S ...
- 转:Spring mvc中@RequestMapping 6个基本用法小结
Spring mvc中@RequestMapping 6个基本用法小结 发表于3年前(2013-02-17 19:58) 阅读(11698) | 评论(1) 13人收藏此文章, 我要收藏 赞3 4 ...
随机推荐
- pcr free library 介绍
一句话:illumina的建库方法,建库时间段,质量还好... the adapters are different in the PCR-free kit compared to the stand ...
- Sublime Text插件:HTML+CSS+JAVASCRIPT+JSON快速格式化
安装过程简要说明如下: 在Sublime Text中,按下Ctrl+Shift+P调出命令面板; 输入install 调出 Install Package 选项并回车; 输入pretty,并在列表中选 ...
- poi 合并单元格 无边框问题
public void merge(int startrow,int endstartrow,int startColumn,int endColumn){ sht.addMergedRegion(n ...
- flexbox的术语
在详细阅读这篇文章之前,我们很有必要先了解flexbox的几个常用术语,这样有助于大家对后文的理解. 伸缩容器:一个设有“display:flex”或“display:inline-flex”的元素 ...
- c 深度剖析 5
1,指针没有指向一块合法的区域 1指针没有初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string. ...
- Websphere发布时遇到的问题
在发布时遇到了data source配置的问题,搞了好久没搞定,最后问题出现在 JDBC providers那边,Implementation class name 改成: com.ibm.db2.j ...
- html5的程序接口与元素变化
除了原先的DOM接口,HTML5增加了更多API,如:1. 用于即时2D绘图的Canvas标签2. 定时媒体回放3. 离线数据库存储4.文档编辑5. 拖拽控制6. 浏览历史管理元素变化新的解析顺序新的 ...
- 一个方法中的ajax在success中renturn一个值,但是方法的返回值是undefind?
https://segmentfault.com/q/1010000003762379 A页面 console.log(handleData("search_list", &quo ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- timus 1210 Kind Spirits(最短路)(动态规划)
Kind Spirits Time limit: 1.0 secondMemory limit: 64 MB Ivanushka the Fool lives at the planet of 0-l ...