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接口读取属性配置文件用的,标识在@ ...
随机推荐
- delphi CoolBar这个怎么弄没了
CoolBar这个怎么弄没了 像Windows这样的 procedure TForm1.Button1Click(Sender: TObject); begin CoolBar1.Fix ...
- XML Helper XML操作类
写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...
- iOS开发——设备篇Swift篇&判断设备类型
判断设备类型 1,分割视图控制器(UISplitViewController) 在iPhone应用中,使用导航控制器由上一层界面进入下一层界面. 但iPad屏幕较大,通常使用SplitViewCo ...
- 阿里技术嘉年华(ADC2013)总结与感悟
上周末刚参加了ADC2013(2013.7.13-14),我报的是TCon测试论坛和UCAN用户体验设计论坛,因为我目前从事的是测试工作,但是还是想往用户体验(主要是用研)方向发展,所以 ...
- 转:打造DropDownList,TreeView,ListBox无限极分类目录树
[csharp] view plaincopyprint? #region DropDownList无限递归显示层次关系 /// <summary> /// 创建无限分级下拉列表框 /// ...
- 超级强大的vim配置(vimplus)
vimplus vimplus是vim的超级配置安装程序 github地址:https://github.com/chxuan/vimplus.git,欢迎star和fork. 接触vim到现在也有几 ...
- 琐碎-关于hadoop2.X那些端口
此文转载http://www.aboutyun.com/thread-7513-1-1.html Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访 ...
- B - Plane of Tanks: Pro
Description Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to ...
- AJAX XMLHttpRequest
<html> <head> <title>XMLHTTPRequest对象的说明DEMO</title> <script language=&qu ...
- Android(java)学习笔记105:Map集合的遍历之键值对对象找键和值
package cn.itcast_01; import java.util.HashMap; import java.util.Map; import java.util.Set; /* * Map ...