spring ApplicationContext 使用总结
2016-07-28 15:42
447 查看
1、Spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的
FileSystemXmlApplicationContext方法对路径进行了处理:
下面两种写法是一样的:
2、可以使用注解的方式代替上面方式
使用文件路径注解方式:
3、classPath指的是编译后生成的classes目录
上面的classes目录中包含的文件和目录可以通过maven的build段配置
FileSystemXmlApplicationContext方法对路径进行了处理:
protected Resource getResourceByPath(String path) { if(path != null && path.startsWith("/")) { path = path.substring(1); } return new FileSystemResource(path); } }
下面两种写法是一样的:
ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/resources/spring/applicationContext.xml");
ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/spring/applicationContext.xml");
2、可以使用注解的方式代替上面方式
//@RunWith(SpringJUnit4ClassRunner.class) //使用注解方式 //@ContextConfiguration(locations = {"/spring/applicationContext.xml"}) //使用注解方式 classpath 下面的路径 //或者 //@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //使用注解方式
使用文件路径注解方式:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})
3、classPath指的是编译后生成的classes目录
上面的classes目录中包含的文件和目录可以通过maven的build段配置
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <configuration> <encoding>${build.file.encoding}</encoding> </configuration> </plugin> </plugins> </build>
相关文章推荐
- Android之事件分发与消费机制
- iOS 第三方框架 - CocoaPods的安装、使用、原理
- 谈谈Android中的Rect类
- Android 解决不同进程发送KeyEvent 的问题
- iOS 使用AFNetworking实现多张图片上传
- 微信支付返回-1的问题集锦与各种爬坑过程
- cordova platform add android 报错问题解决
- Android多用户原理和实现
- Swift函数
- Android xutils3.0 使用
- 微信公众号开发教程[017]-网页开发-JSSDK
- 记录一次Android内存泄漏事件和解决过程
- ListView,点击每一个Item,如何获取当前Item的数据?
- Android Studio 如何格式化代码
- Swift:消除Null值
- Swift:消除Null值
- Swift:消除Null值
- Android调试中使用默认的 签名文件
- iOS开发富文本NSAttributedString属性
- iOS 开发 排序问题 (好友列表,按字母排序)