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接口读取属性配置文件用的,标识在@ ...
随机推荐
- hdu 5278 Geometric Progression 高精度
Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...
- BZOJ 4236: JOIOJI MAP
4236: JOIOJI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.ph ...
- delphi 文件或目录转换成 TreeView
//文件或目录转换成 TreeViewprocedure DirToTreeView(Tree: TTreeView; Directory: string; Root: TTreeNode; Incl ...
- win7中USB音箱没有声音解决的方法
Win7装好后,原来在XP中工作正常的USB小音箱却不工作了,重装了声卡驱动还是一样,后来通过下面尝试最终好用了. 1.右键右下角喇叭button. 2.点击"播放设备". 3.设 ...
- Servlet---JavaWeb技术的核心基础,JavaWeb框架的基石(二)
一.Servlet之Request Web服务器会对收到的每一次客户端http请求分别创建一个用于代表请求的request对象和代表响应的response对象.要获取客户端提交的数据需 ...
- php生成CSV格式(转)
参考网址: php对csv文件的常用操作集合 http://blog.xhbin.com/archives/748 1,下载CSV格式文档 唯一需要特别注意的是编码. <? include_on ...
- Android:TabHost实现Tab切换
TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...
- jenkis编译报错:需要class,interface或enum
现象: 1.jenkis编译报错:需要class,interface或enum 2.使用ant进行编译ok. 解决方法: 1. Jenkis重新编译一个以前成功的svn版本,直至编译成功. 2.Jen ...
- qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新
qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新 Widget wit ...
- Android开发之神奇的Fading Edge,让你的View更有层次感!
最近在研究Android Framework层源码,发现我们对源码的理解应该建立在对API的理解之上,如果有一些API你没用过,那么即使你在源码中见到这个东西都不知道是干嘛的,更谈不上理解了.一直以来 ...