Spring常用配置
来源:互联网 发布:商城app源码下载 编辑:程序博客网 时间:2024/05/17 03:05
一、前言
这一篇讲解一些基础常见配置。司空见惯,只为复习与新人学习之用。
二、单例多例
2.1 默认单例
我们默认的bean就是单例
import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;/** * Created by ycy on 16/4/8. */@Service//相当于 @Scope("singleton")public class DemoSingleService {}
2.2 多例(每次新建一个bean)
/** * Created by ycy on 16/4/8. */@Service@Scope("prototype")//1public class DemoPrototypeService {}
三、文件注入
一般都是properties文件的注入,以及其他文件注入等待。一般不建议这么玩耍,除非万不得已。因为毕竟配置的还是交给同意配置好了。这样不利于维护
@Configuration@ComponentScan("com.ycy")@PropertySource("classpath:test/testproper.properties")public class ElConfig { @Value(" i love you") private String normal;//注入普通字符 @Value("${book.name}") private String bookName;//注入配置文件}
四、jvm参数使用代替classpath等文件配置
还在习惯使用classpath 的小伙伴,你已经out了,你想过没有,你的项目文件的配置如果基于标准化。那么你应该是不知道properties文件里面到底配置了什么参数,这样即可以统一管理我们的项目配置文件,又安全。因为如果项目代码被盗,至少配置不在项目里面!!!
这里只是做一些基本的jvm的value配置
-ea-Dmercury.conf.file=file:/Users/ycy/IdeaProjects/branches/up2_index_ycy/mercury-web/src/main/webapp/WEB-INF/properties/development.properties-Dlogger.root=/Users/logycy-Djetty.port=9090解释:例子中我们定义了三个value:
1、定义了一个文件 (我们无论是什么环境都使用mercury.conf.file作为配置文件就没错了)
2、定义log根目录
3、定义jetty端口号
五、使用spring的@Async异步执行方法
应用场景:
1、某些耗时较长的而用户不需要等待该方法的处理结果
2、某些耗时较长的方法,后面的程序不需要用到这个方法的处理结果时
@Async public static void testAsyncMethod(){ try { //让程序暂停100秒,相当于执行一个很耗时的任务 //或者记录日志就不要占用客户端的时间了 Thread.sleep(100000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
0 0
- 【Spring】Spring常用配置-Profile
- Spring常用的配置
- ssh_1 常用spring配置
- 常用Spring配置项
- Spring 常用配置小结
- spring常用配置
- spring mvc常用配置
- Spring常用配置解析
- Spring常用配置
- Spring中常用配置
- spring 常用配置
- spring常用注解配置
- Spring常用配置示例
- Spring常用配置
- Spring常用配置
- Spring 常用配置
- Spring常用配置
- Spring常用配置(二)
- lintCode(453)——将二叉树拆分成链表
- 多线程的3中实现方式,以及有返回值线程池Demo
- 将dic里的数据写到model里,自动处理null数据
- Linux 常用命令简记
- doxygen注释
- Spring常用配置
- iOS 数据缓存
- Linux useradd --建立用户账号和用户起始目录
- 详解spring 每个jar的作用
- 完美解决Android SDK Manager无法更新
- 离线配置ubuntu 14.04 web服务环境
- centos搭建git服务器(上)
- 省市区三级下拉选择器
- 在eclipse中生成Android的数字证书keystore文件