cw网澳洲海淘直邮网站,要是被退运怎么办,山西的,到哪处理呀?

在项目中我们常常会使用Constants常量类,达到系统全局配置的目的。
但是有些常量需要动态的配置,如果项目上线后,每次修改Constants.java然后再编译,再上传Constants.class文件,再重启服务器。这样导致很繁琐。
如果将需要修改的配置项写成properties文件,将会在项目后期维护带来很大的方便~!
root.url=http://localhost:8080/BNCAR2/
root.path=E:/ws2/BNCAR2/rel/
mall.partstype.url=mall.jsp?rowid=0&typeFlag=0&pid=35
mall.carproduct.url=mall.jsp?rowid=0&typeFlag=1&pid=74
mall.partstype.typename1=\u4FDD\u517B\u
mall.partstype.typename2=\u7CFB\u7EDF\u517B\u62A4
mall.partstype.typename3=\u8F6E\u80CE\u8F6E\u6BC2
network.province.name1=\u4E0A\u6D77
network.province.name2=\u6C5F\u82CF
pageutil.persize=10
注意名=值之间不要留空格,注意名的命名习惯,一般为小写,层级关系之间用点号分隔。
阅读(...) 评论()Spring 中引用Properties文件 - 百行孝为先,万恶懒为首
- ITeye技术网站
博客分类:
其中部分配置信息(邮件发送相关):
#邮件发送的相关配置
email.host =
email.port = xxx
email.username = xxx
email.password = xxx
email.sendFrom =
在Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下:
&!-- spring的属性加载器,加载properties文件中的属性 --&
&bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&
&property name="location"&
&value&/WEB-INF/configInfo.properties&/value&
&/property&
&property name="fileEncoding" value="utf-8" /&
属性信息加载后其中一种使用方式是在其它bean定义中直接根据属性信息的key引用value,如邮件发送器bean的配置如下:
&!-- 邮件发送 --&
&bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl"&
&property name="host"&
&value&${email.host}&/value&
&/property&
&property name="port"&
&value&${email.port}&/value&
&/property&
&property name="username"&
&value&${email.username}&/value&
&/property&
&property name="password"&
&value&${email.password}&/value&
&/property&
&property name="javaMailProperties"&
&prop key="mail.smtp.auth"&true&/prop&
&prop key="sendFrom"&${email.sendFrom}&/prop&
&/property&
另一种使用方式是在代码中获取配置的属性信息,可定义一个javabean:ConfigInfo.java,利用注解将代码中需要使用的属性信息注入;如属性文件中有如下信息需在代码中获取使用:
#生成文件的保存路径
file.savePath = D:/test/
#生成文件的备份路径,使用后将对应文件移到该目录
file.backupPath = D:/test bak/
ConfigInfo.java 中对应代码:
@Component("configInfo")
public class ConfigInfo {
@Value("${file.savePath}")
private String fileSaveP
@Value("${file.backupPath}")
private String fileBakP
public String getFileSavePath() {
return fileSaveP
public String getFileBakPath() {
return fileBakP
业务类bo中使用注解注入ConfigInfo对象:
@Autowired
private ConfigInfo configI
需在bean.xml中添加组件扫描器,用于注解方式的自动注入:
&context:component-scan base-package="com.my.model" /&
(上述包model中包含了ConfigInfo类)。
通过get方法获取对应的属性信息,优点是代码中使用方便,缺点是如果代码中需用到新的属性信息,需对ConfigInfo.java做相应的添加修改。
浏览: 875236 次
来自: 上海
这个挺好JavaScript实现input输入框控件只允许输入 ...
东西太好啦受教啊
naiyizute 写道编码表好像不全,能提供全一点的吗htt ...
你这个 EmojiConverter 是来自这里吧,一个日本人 ...
写的真好,根据这篇搭建文章,我自己整理了一下,完整的包和可以在 ...> java读取配置文件properties的步骤(很多不会用框架s2sh的开发者,喜欢用此方法)
java读取配置文件properties的步骤(很多不会用框架s2sh的开发者,喜欢用此方法)
jianning_168 & &
发布时间: & &
浏览:45 & &
回复:0 & &
悬赏:0.0希赛币
java读取配置文件properties的方法(很多不会用框架s2sh的开发者,喜欢用此方法)  示例:
  属性文件:beans.properties
  .leadfar.cms.backend.dao.impl.ArticleDaoImpl
.leadfar.cms.backend.dao.impl.ChannelDaoImpl
  使用此属性时类的配置如下:
  PropertiesBeanFactory.java
  .leadfar.cms.
import java.io.IOE
import java.util.P
.leadfar.cms.backend.dao.ArticleD
.leadfar.cms.backend.dao.ChannelD
public class PropertiesBeanFactory implements BeanFactory {
Properties props = new Properties();
public PropertiesBeanFactory(){
//读取配置文件,得到具体DAO的实现类名
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(&beans.properties&));
} catch (IOException e) {
e.printStackTrace();
public ArticleDao getArticleDao() {
String className = props.getProperty(&articleDao&);
Class clz = Class.forName(className);
return (ArticleDao)clz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
public ChannelDao getChannelDao() {
String className = props.getProperty(&channelDao&);
Class clz = Class.forName(className);
return (ChannelDao)clz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
  使用时如下:
还没看明白的,可以参看视频 尚学堂的学生李腾飞cms视频33讲
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&Spring中配置和读取多个Properties文件
一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
Properties代码
database.url=jdbc:mysql://localhost/smaple &
database.driver=com.mysql.jdbc.Driver &
database.user=root
database.password=123
2.消息服务配置文件demo-mq.properties:
Properties代码
#congfig of ActiveMQ
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
mq.java.naming.security.principal=
mq.java.naming.security.credentials= &
jms.MailNotifyQueue.consumer=5
3.远程调用的配置文件demo-remote.properties:
Properties代码
remote.ip=localhost
remote.port=16800
remote.serviceName=test
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
&?xml version="1.0"
encoding="UTF-8"?& &
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
& xsi:schemaLocation=" &
& http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&
& &!-- 将多个配置文件读取到容器中,交给Spring管理
& &bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&
&property name="locations"&
&!-- 这里支持多种寻址方式:classpath和file --&
&value&classpath:/opt/demo/config/demo-db.properties&/value&
&!-- 推荐使用file的方式引入,这样可以将配置和代码分离 --&
&value&file:/opt/demo/config/demo-mq.properties&/value&
&value&file:/opt/demo/config/demo-remote.properties&/value&
&/property& &
& &!-- 使用MQ中的配置 --&
& &bean id="MQJndiTemplate"
class="org.springframework.jndi.JndiTemplate"&
&property name="environment"&
key="java.naming.factory.initial"&${mq.java.naming.factory.initial}&/prop&
key="java.naming.provider.url"&${mq.java.naming.provider.url}&/prop&
key="java.naming.security.principal"&${mq.java.naming.security.principal}&/prop&
key="java.naming.security.credentials"&${mq.java.naming.security.credentials}&/prop&
key="userName"&${mq.java.naming.security.principal}&/prop&
key="password"&${mq.java.naming.security.credentials}&/prop&
&/props& &
&/property& &
&/beans& &
&我们也可以将配置中的List抽取出来:
&?xml version="1.0"
encoding="UTF-8"?& &
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
& xsi:schemaLocation=" &
& http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&
& &!-- 将多个配置文件位置放到列表中
& &bean id="propertyResources"
class="java.util.ArrayList"&
&constructor-arg&
&!-- 这里支持多种寻址方式:classpath和file --&
&value&classpath:/opt/demo/config/demo-db.properties&/value&
&!-- 推荐使用file的方式引入,这样可以将配置和代码分离 --&
&value&file:/opt/demo/config/demo-mq.properties&/value&
&value&file:/opt/demo/config/demo-remote.properties&/value&
&/constructor-arg&
& &!-- 将配置文件读取到容器中,交给Spring管理
& &bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&
&property name="locations" ref="propertyResources"
& &!-- 使用MQ中的配置 --&
& &bean id="MQJndiTemplate"
class="org.springframework.jndi.JndiTemplate"&
&property name="environment"&
key="java.naming.factory.initial"&${mq.java.naming.factory.initial}&/prop&
key="java.naming.provider.url"&${mq.java.naming.provider.url}&/prop&
key="java.naming.security.principal"&${mq.java.naming.security.principal}&/prop&
key="java.naming.security.credentials"&${mq.java.naming.security.credentials}&/prop&
key="userName"&${mq.java.naming.security.principal}&/prop&
key="password"&${mq.java.naming.security.credentials}&/prop&
&/props& &
&/property& &
&/beans& &
二、整合多工程下的多个分散的Properties
应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。
配置如下:
&?xml version="1.0"
encoding="UTF-8"?& &
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
& xsi:schemaLocation=" &
& http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&
& &!-- 将DB属性配置文件位置放到列表中
& &bean id="dbResources"
class="java.util.ArrayList"&
&constructor-arg&
&value&file:/opt/demo/config/demo-db.properties&/value&
&/constructor-arg&
& &!-- 将MQ属性配置文件位置放到列表中
& &bean id="mqResources"
class="java.util.ArrayList"&
&constructor-arg&
&value&file:/opt/demo/config/demo-mq.properties&/value&
&/constructor-arg&
& &!-- 用Spring加载和管理DB属性配置文件
& &bean id="dbPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&
&property name="order" value="1" /&
&property name="ignoreUnresolvablePlaceholders"
value="true" /&
&property name="locations" ref="dbResources"
& &!-- 用Spring加载和管理MQ属性配置文件
& &bean id="mqPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&
&property name="order" value="2" /&
&property name="ignoreUnresolvablePlaceholders"
value="true" /&
&property name="locations" ref="mqResources"
& &!-- 使用DB中的配置属性
& &bean id="rmsDataSource"
class="mons.dbcp.BasicDataSource"
destroy-method="close" &&
p:driverClassName="${demo.db.driver}" p:url="${demo.db.url}"
p:username="${demo.db.username}"
p:password="${demo.db.password}"
pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"
p:poolPreparedStatements="true"
p:default&
& &!-- 使用MQ中的配置 --&
& &bean id="MQJndiTemplate"
class="org.springframework.jndi.JndiTemplate"&
&property name="environment"&
key="java.naming.factory.initial"&${mq.java.naming.factory.initial}&/prop&
key="java.naming.provider.url"&${mq.java.naming.provider.url}&/prop&
key="java.naming.security.principal"&${mq.java.naming.security.principal}&/prop&
key="java.naming.security.credentials"&${mq.java.naming.security.credentials}&/prop&
key="userName"&${mq.java.naming.security.principal}&/prop&
key="password"&${mq.java.naming.security.credentials}&/prop&
&/props& &
&/property& &
&/beans& &
&注意:其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的
Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。这里一定需要按照这种方式设置这两个参数。
三、Bean中直接注入Properties配置文件中的值
应用场景:Bean中需要直接注入Properties配置文件中的值
。例如下面的代码中需要获取上述demo-remote.properties中的值:
Java代码 &
public class Client() {
& private S &
& private S &
& private S &
&配置如下:
&?xml version="1.0"
encoding="UTF-8"?& &
href="http://www.springframework.org/schema/beans"&http://www.springframework.org/schema/beans&/a&"
&xmlns:xsi="&a
href="http://www.w3.org/2001/XMLSchema-instance"&http://www.w3.org/2001/XMLSchema-instance&/a&"
&xmlns:util="&a
href="http://www.springframework.org/schema/util"&http://www.springframework.org/schema/util&/a&"
&xsi:schemaLocation=" &
href="http://www.springframework.org/schema/beans"&http://www.springframework.org/schema/beans&/a&
href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&/a&
href="http://www.springframework.org/schema/util"&http://www.springframework.org/schema/util&/a&
href="http://www.springframework.org/schema/util/spring-util-3.0.xsd"&http://www.springframework.org/schema/util/spring-util-3.0.xsd&/a&"&
&&!-- 这种加载方式可以在代码中通过@Value注解进行注入,
&可以将配置整体赋给Properties类型的类变量,也可以取出其中的一项赋值给String类型的类变量
&util:properties/&
标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签 --&
&&util:properties
id="remoteSettings"
location="file:/opt/demo/config/demo-remote.properties"
&util:properties/&
标签的实现类是PropertiesFactoryBean, &
&直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的
&&bean id="settings"
&class="org.springframework.beans.factory.config.PropertiesFactoryBean"&
&&property
name="locations"& &
&value&file:/opt/rms/config/rms-mq.properties&/value&
&value&file:/opt/rms/config/rms-env.properties&/value&
&&/property&
&/beans& &
&Client类中使用Annotation如下:
Java代码 &
org.springframework.beans.factory.annotation.V
public class Client() {
& @Value("#{remoteSettings['remote.ip']}")
& private S &
& @Value("#{remoteSettings['remote.port']}")
& private S &
@Value("#{remoteSettings['remote.serviceName']}")
& private S &
四、Bean中存在Properties类型的类变量
应用场景:当Bean中存在Properties类型的类变量需要以注入的方式初始化
配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下
Java代码 &
org.springframework.beans.factory.annotation.V
org.springframework.beans.factory.annotation.A
public class Client() {
& @Value("#{remoteSettings}")
& private Properties remoteS
2. 配置方式:也可以使用xml中声明Bean并且注入
&?xml version="1.0"
encoding="UTF-8"?& &
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
& xsi:schemaLocation=" &
& http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&
可以使用如下的方式声明Properties类型的FactoryBean来加载配置文件,这种方式就只能当做Properties属性注入,而不能获其中具体的值
& &bean id="remoteConfigs"
class="org.springframework.beans.factory.config.PropertiesFactoryBean"&
&property name="locations"&
&value&file:/opt/demo/config/demo-remote.properties&/value&
&/property& &
& &!-- 远端调用客户端类 --&
& &bean id="client"
class="com.demo.remote.Client"&
&property name="properties" ref="remoteConfigs"
&/beans& &
代码如下:
Java代码 &
org.springframework.beans.factory.annotation.A
public class Client() {
& //@Autowired也可以使用 &
& private Properties remoteS
& //getter setter &
上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Java中动态加载properties文件,而不需要重启应用的解决办法 - 互联网当前位置:& &&&Java中动态加载properties文件,而不需要重启应用的Java中动态加载properties文件,而不需要重启应用的解决办法&&网友分享于:&&浏览:328次Java中动态加载properties文件,而不需要重启应用的解决方法
在Java项目中,如果需要使用.properties类型的文件作为某些配置信息存放介质的时候,一般都是将.properties文件放在src目录下,代码大部分都是这样写的:
[java] view plaincopy
Properties prop = new Properties();
InputStream is = CommonUtils.class.getClassLoader().getResourceAsStream("config.properties");//假设当前这个方法是在CommonUtils类下面
prop.load(is);
在系统启动之后,config.properties中的key-value信息都可以获取,但是某一天,你想改变一下config.properties中的相关配置,但是又不能重启应用,你就会发现,明明已经修改了config.properties文件内容,为什么读出来的信息还是原先的?
经过google后发现,原来使用
[java] view plaincopy
CommonUtils.class.getClassLoader().getResourceAsStream("config.properties")
这种加载方法会将config.properties文件加载到内存中,在下次需要读取时直接从内存中获取文件信息,而不是再次读取!既然以上方法会将文件信息缓存,那么我只要改变一下文件的输入流获取方式就行了。改成如下方式就行了:
[java] view plaincopy
Properties prop = new Properties();
String path = CommonUtils.class.getClassLoader().getResource("config.properties").getPath();
InputStream is = new FileInputStream(path);
prop.load(is);
[java] view plaincopy
String dirPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取config.properties文件所在的父目录
File file = new File(dirPath,"config.properties");
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有

我要回帖

更多关于 澳洲海淘直邮网站 的文章

 

随机推荐