怎么可以取到配置文件里面的值|springmvc中如何从配置文件中读取信息

Ⅰ java中spring读取配置文件的几种方法

Java中spring读取配置文件的几种方法如下:一、读取xml配置文件 (一)新建一个java beanpackage chb.demo.vo;public class HelloBean { private String helloWorld; public String getHelloWorld() { return helloWorld; } public void setHelloWorld(String helloWorld) { this.helloWorld = helloWorld; }} (二)构造一个配置文件<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" ><beans> <bean id="helloBean" class="chb.demo.vo.HelloBean"> <property name="helloWorld"> <value>Hello!chb!</value> </property> </bean></beans> (三)读取xml文件 1.利用ApplicationContext context = new ("beanConfig.xml");//这种用法不够灵活,不建议使用。 HelloBean helloBean = (HelloBean)context.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); 2.利用FileSystemResource读取Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml"); BeanFactory factory = new XmlBeanFactory(rs); HelloBean helloBean = (HelloBean)factory.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); 值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。 二、读取properties配置文件 这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取 (一)利用spring读取properties 文件 我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:helloBean.class=chb.demo.vo.HelloBeanhelloBean.helloWorld=Hello!chb! 属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。 然后利用org.springframework.beans.factory.support.来读取属性文件 BeanDefinitionRegistry reg = new DefaultListableBeanFactory(); reader = new (reg); reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties")); BeanFactory factory = (BeanFactory)reg; HelloBean helloBean = (HelloBean)factory.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); (二)利用java.util.Properties读取属性文件 比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:ip=192.168.0.1port=8080 则,我们可以用如下程序来获得服务器配置信息: InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties"); Properties p = new Properties(); try { p.load(inputStream); } catch (IOException e1) { e1.printStackTrace(); }System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port")); 三 、用接口类WebApplicationContext来取。 private WebApplicationContext wac; wac =WebApplicationContextUtils.( this.getServletContext()); wac = WebApplicationContextUtils.getWebApplicationContext( this.getServletContext()); JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");其中,jdbcTemplate为spring配置文件中的一个bean的id值。这种用法比较灵活,spring配置文件在web中配置启动后,该类会自动去找对应的bean,而不用再去指定配置文件的具体位置。

Ⅱ 如何在spring中读取properties配置文件里面的信息

首先我使用的是注解的方式。

创建properties配置文件Key=value的形式

在spring的配置文件中进行导入专代码如下:属

<util:properties id="test" location="classpath:test.properties"/>

提示:路径问题自己把握

3.在你需要使用的类中这样:

private @Value("#{test['isOpen']}") String isOpen;

记得写getset方法

isOpen这个在test.properties中是这样的:

isOpen=true

如果有任何问题,可追加。望采纳

Ⅲ springmvc中如何从配置文件中读取信息

1、第一来步,先新建一个.properties文件源,app.properties里面内容admin=admintest=test2、第二步,新建一个xml文件,在applicationContext.xml,<!– 用来解析Java Properties属性文件值(注意class指定的类)–><bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:app.properties</value></list></property></bean><!– 把properties里面的信息读进来: –><bean id="report" class="java.lang.String"><constructor-arg value="${admin}"/></bean>

Ⅳ 如何从Properties配置文件读取值

最常用读取properties文件的方法InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");Java中获取路径方法获取路径的一个简单实现反射方式获取properties文件的三种方式1 反射方式获取properties文件最常用方法以及思考:Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:InputStream in = getClass().getResourceAsStream("资源Name");这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊-- 取所有类的父类Object,用Object.class难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。 import java.util.Properties; import java.io.InputStream; import java.io.IOException;/** * 读取Properties文件的例子 * File: TestProperties.java * User: leimin * Date: 2008-2-15 18:38:40 */ public final class TestProperties {private static String param1;private static String param2;static {Properties prop = new Properties();InputStream in = Object. class .getResourceAsStream( "/test.properties" );try {prop.load(in);param1 = prop.getProperty( "initYears1" ).trim();param2 = prop.getProperty( "initYears2" ).trim();} catch (IOException e) {e.printStackTrace();}}/*** 私有构造方法,不需要创建对象*/private TestProperties() {}public static String getParam1() {return param1;}public static String getParam2() {return param2;}public static void main(String args[]){System.out.println(getParam1());System.out.println(getParam2());} } 运行结果:151 152 当然,把Object.class换成int.class照样行,呵呵,大家可以试试。另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法2 获取路径的方式:File fileB = new File( this .getClass().getResource( "" ).getPath());System. out .println( "fileB path: " + fileB); 2.2获取当前类所在的工程名:System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span> /**

Ⅳ springmvc中读取配置文件中参数问题

Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中一些动态设定的值(value),在XML中替换为占位该键($key$)的值,.properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性。<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false"> <property name="locations"> <list> <value>classpath:conf/probiz.properties</value> <value>classpath:conf/mail_config.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true" /> <property name="" value="true" /> </bean>

Ⅵ web.xml中如何读取properties配置文件中的值

不是有个config对象吗,这个应该可以获取配置参数的值….你可以试试,jsp和servlet中都可以获取这个对象。

Ⅶ 请教如何可以方便的获取ini文件的所有键值

如果你的配置文件里面所有的键值数据类型都是一样的话,可以用“获取段名”获得所有的段,然后用for循环获取所有键,再根据段和键来取得所有键值。

Ⅷ C#如何读取config配置文件数据

代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Configuration;usingSystem.ServiceModel;usingSystem.ServiceModel.Configuration;namespaceNetUtilityLib{publicstaticclassConfigHelper{//依据连接串名字connectionName返回数据连接字符串(stringconnectionName){//指定config文件读取stringfile=System.Windows.Forms.Application.ExecutablePath;System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);stringconnectionString=config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();returnconnectionString;}///<summary>///更新连接字符串///</summary>///<paramname="newName">连接字符串名称</param>///<paramname="newConString">连接字符串内容</param>///<paramname="newProviderName">数据提供程序名称</param>(stringnewName,stringnewConString,stringnewProviderName){//指定config文件读取stringfile=System.Windows.Forms.Application.ExecutablePath;Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);boolexist=false;//记录该连接串是否已经存在//如果要更改的连接串已经存在if(config.ConnectionStrings.ConnectionStrings[newName]!=null){exist=true;}//如果连接串已存在,首先删除它if(exist){config.ConnectionStrings.ConnectionStrings.Remove(newName);}//新建一个连接字符串实例=newConnectionStringSettings(newName,newConString,newProviderName);//将新的连接串添加到配置文件中.config.ConnectionStrings.ConnectionStrings.Add(mySettings);//保存对配置文件所作的更改config.Save(ConfigurationSaveMode.Modified);//强制重新载入配置文件的ConnectionStrings配置节ConfigurationManager.RefreshSection("ConnectionStrings");}///<summary>///返回*.exe.config文件中appSettings配置节的value项///</summary>///<paramname="strKey"></param>///<returns></returns>(stringstrKey){stringfile=System.Windows.Forms.Application.ExecutablePath;Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);foreach(stringkeyinconfig.AppSettings.Settings.AllKeys){if(key==strKey){returnconfig.AppSettings.Settings[strKey].Value.ToString();}}returnnull;}///<summary>///在*.exe.config文件中appSettings配置节增加一对键值对///</summary>///<paramname="newKey"></param>///<paramname="newValue"></param>(stringnewKey,stringnewValue){stringfile=System.Windows.Forms.Application.ExecutablePath;Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);boolexist=false;foreach(stringkeyinconfig.AppSettings.Settings.AllKeys){if(key==newKey){exist=true;}}if(exist){config.AppSettings.Settings.Remove(newKey);}config.AppSettings.Settings.Add(newKey,newValue);config.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("appSettings");}//修改system.serviceModel下所有服务终结点的IP地址(stringconfigPath,stringserverIP){Configurationconfig=ConfigurationManager.OpenExeConfiguration(configPath);ConfigurationSectionGroupsec=config.SectionGroups["system.serviceModel"];=secasServiceModelSectionGroup;ClientSectionclientSection=serviceModelSectionGroup.Client;foreach(.Endpoints){[email protected]"d{1,3}.d{1,3}.d{1,3}.d{1,3}";stringaddress=item.Address.ToString();stringreplacement=string.Format("{0}",serverIP);address=Regex.Replace(address,pattern,replacement);item.Address=newUri(address);}config.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("system.serviceModel");}//修改applicationSettings中App.Properties.Settings中服务的IP地址publicstaticvoidUpdateConfig(stringconfigPath,stringserverIP){Configurationconfig=ConfigurationManager.OpenExeConfiguration(configPath);ConfigurationSectionGroupsec=config.SectionGroups["applicationSettings"];=sec.Sections["DataService.Properties.Settings"];=;if(clientSettingsSection!=null){SettingElementelement1=clientSettingsSection.Settings.Get("DataService_SystemManagerWS_SystemManagerWS");if(element1!=null){clientSettingsSection.Settings.Remove(element1);stringoldValue=element1.Value.ValueXml.InnerXml;element1.Value.ValueXml.InnerXml=GetNewIP(oldValue,serverIP);clientSettingsSection.Settings.Add(element1);}SettingElementelement2=clientSettingsSection.Settings.Get("DataService_EquipManagerWS_EquipManagerWS");if(element2!=null){clientSettingsSection.Settings.Remove(element2);stringoldValue=element2.Value.ValueXml.InnerXml;element2.Value.ValueXml.InnerXml=GetNewIP(oldValue,serverIP);clientSettingsSection.Settings.Add(element2);}}config.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("applicationSettings");}privatestaticstringGetNewIP(stringoldValue,stringserverIP){[email protected]"d{1,3}.d{1,3}.d{1,3}.d{1,3}";stringreplacement=string.Format("{0}",serverIP);stringnewvalue=Regex.Replace(oldValue,pattern,replacement);returnnewvalue;}}}

Ⅸ springmvc中怎么从配置文件中读取信息

springmvc中如何从配置文件中读取信息<!– 系统配置参数. –> <bean value="true" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="location" value="classpath:/fynetAdminSettings/app.properties" /> </bean> <bean id="sysUsersConfigBean" name="code">package com.fyard.fynet.core.settings.admin;import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Component;/** * 系统用户对象 * */@Componentpublic class SysUsersConfigBean { private Map<String, String> sysUserInfo = new HashMap<String, String>(); public Map<String, String> getSysUserInfo() { return sysUserInfo; } public void setSysUserInfo(Map<String, String> sysUserInfo) { this.sysUserInfo = sysUserInfo; } public String getPassword(String username) { return sysUserInfo.get(username); }}以上三步就可以直接读取配置文件中的数据,.properties文件中的值会自动映射到xml文件中的bean中,SysUsersConfigBean该类已经被标注为@Component,在service层就可以直接调用即可

Ⅹ php的类怎么读取到配置文件里面的配置项

1、新建一个PHP文档,该文档的目的是检测PHP的环境配置,示例:<?php phpinfo()。


赞 (0)