spring读取prperties配置文件(2)
接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件。
这里我先定义好属性文件"user.properties"。
user.name=bingyulei
user.description=没有什么好描述的
然后再spring的配置文件中applicationContext.xml加入如下配置,这里我配置文件就放到classpath下,可以运行junit测试
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启注释配置 -->
<context:annotation-config />
<!-- 自动扫描 包 -->
<context:component-scan base-package="com.bing">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<!-- 不用扫描的目录 -->
<context:exclude-filter type="regex" expression="com\.bing\.vo.*" />
<context:exclude-filter type="regex" expression="com\.bing\.util.*" />
</context:component-scan>
<!-- 自动读取属性文件的配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/user.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
</beans>
创建要注入读取配置文件的类:这里我用”@Component“注释,和“@Controller”、"@Service"、"@Repository"意思差不多一样。
@Component;@Controller;@Service;@Repository
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到
spring的管理中了。而@Controller, @Service,
@Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
package com.bing.test; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("manager")
public class Manager {
@Value("${user.name}")
private String myName;
@Value("${user.description}")
private String description; public void sayHello() {
System.out.println("Hello " + myName);
} public void getDes() {
System.out.println(description); }
}
然后用junit测试一下:
package com.bing.jtest; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.bing.test.Manager; @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class Testr { @Resource(name="manager")
private Manager manager; @Test
public void test() {
manager.sayHello();
manager.getDes();
}
}
运行结果:
Hello bingyulei
没有什么好描述的
另外:在jsp中如果想得到对应的config值,可以直接用下面标签,只需在界面中加入这个tab声明就可以了<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code="url.portal.help"/>
欢迎交流学习:http://www.cnblogs.com/shizhongtao/p/3469323.html
转载请注明出处
spring读取prperties配置文件(2)的更多相关文章
- spring读取prperties配置文件(1)
博客地址http://www.cnblogs.com/shizhongtao/p/3438431.html 属性文件命名是*.properties,在java中,用类java.util.Propert ...
- Spring读取xml配置文件的原理与实现
本篇博文的目录: 一:前言 二:spring的配置文件 三:依赖的第三方库.使用技术.代码布局 四:Document实现 五:获取Element的实现 六:解析Element元素 七:Bean创造器 ...
- spring读取xml配置文件(二)
一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...
- spring 读取yaml配置文件
从Spring框架4.1.0增加了对YAML的支持,Spring框架4.1.0 maven POM具有Snakeyaml依赖性 . 您可以在Spring Boot应用中使用两种方式加载YAML: 1 ...
- Spring 读取XML配置文件的两种方式
import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...
- Java中spring读取配置文件的几种方法
Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
- java web路径和spring读取配置文件
此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...
- Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...
随机推荐
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- 通过GitHub和Hexo搭建个人博客
LinEvan个人博客 最终有自己的个人博客,逼格一下子提高说不少. 网上一搜教程一大堆,非常多已经写得非常好了,我就不凑这个热闹了.推荐一篇博文:怎样搭建一个独立博客--简明Github Pages ...
- 用 UIViewPropertyAnimator 编写动画
[iOS 10 day by day] Day 1:开发 iMessage 的第三方插件 [iOS 10 day by day] Day 2:线程竞态检测工具 Thread Sanitizer < ...
- Windows Service 之 详解(一)
一.Windows 服务简介 Windows 服务是可以在系统启动时自动打开的(不需要任何人登录计算机)的程序. 1.适合创建Windows 服务的场景: [1] 在没有用户交互操作的情况下运行程序: ...
- 想学React Native?你只需要一个App!(11月5号更新)
最近有点空闲时间,顺手研究下react-native,2013年的时候在老师的指导下使用jQuery Mobile做过手机应用,那个运行速度慢呀!让我对WebApp和PhoneGap这一类的跨平台Ap ...
- MeasureSpec介绍
在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...
- oracle 排序
1.ORDER BY 中关于NULL的处理 缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前. 当然,你也可以使用nulls f ...
- vb.net Linq 筛选(像 select distinct) DateTable 日期数据中的年份
Private Sub initDDLByYear(ByVal dt As DataTable) ddlByYear.Items.Clear() ddlByYear.Items.Add(") ...
- [改善Java代码]在明确的场景下,为集合指定初始容量
我们经常使用ArrayList,Vector,Hashmap等集合,一般都是直接用new跟上类名声明出一个集合来,然后使用add,remove,等方法进行操作,而且因为它们是自动管理长度的,所以不用我 ...
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10(转)
在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html&g ...