spring 获取配置文件properties中参数
作者:gleaner 发布:2014-02-28 17:38 分类:工作学习 阅读:12,883 views 评论关闭
<iframe id="cproIframe_u728416_1" style="border-width: 0px; margin: 0px; padding: 0px;" src="http://pos.baidu.com/acom?adn=3&at=134&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=16&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0&layout_filter=rank%2Ctabcloud&ltr=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D8%26rsv_bp%3D1%26tn%3Dbaidu%26wd%3Dspring%2520mvc%2520PropertyPlaceholderConfigurer%26rsv_pq%3D857a1d3400010732%26rsv_t%3D3e3dkpSNLQpYm%252Bzw8TbjldufYNimprz532c5sHfxK7e9mMRZaeb7FK0wiYA%26bs%3Dspring%2520mvc%2520propertisutil&ltu=http%3A%2F%2Fblog.94gleaner.com%2F445.html&lunum=6&n=74015068_cpr&pcs=1366x665&pis=10000x10000&ps=169x878&psr=1366x768&pss=1366x170&qn=2af92e2cbaa81964&rad=&rsi0=336&rsi1=280&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=728416&tn=text_default_336_280&tpr=1423746810626&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u728416&tt=1423746809723.906.2326.2340" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center,center" width="336" height="280"></iframe>
Spring中config属性文件的读取与使用 PropertyPlaceholderConfigurer 注解@Component
1.配置文件:/WEB-INF/configInfo.properties
配置文件内容:
email.host = www.94gleaner.com
email.port = xxx
email.username = gleaner
email.password = xxx
email.sendFrom = 94gleaner@94gleaner.com
2.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>
3.其它bean中引用,例:
<property name=”host”>
- <value>${email.host}</value>
</property>
<property name=”port”>
- <value>${email.port}</value>
</property>
4.JAVA代码中获取方法:创建ConfigInfo.java
@Component(“configInfo”)
public class ConfigInfo {
- @Value(“${email.host}”)
- private String host;
- @Value(“${email.port}”)
- private String port;
- public String getHost() {
- return host;
- }
- public String getPort() {
- return port;
- }
}
5.代码中调用处
@Autowired
private ConfigInfo configInfo;
public byte[] Test(HttpServletResponse response) {
- string host = configInfo.getHost();
}
相关推荐
该xml文件不仅仅是Spring框架初次使用时的主配置文件,而且该文件里面包含了java.util里面的List,Set,Map,Properties等的命名空间,支持在配置文件中定义外部的集合,便于参数注入的复用性
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...
即Redis和文件缓存中都找不到配置项时,客户端会从配置文件(app.properties)中加载。 在本地开发环境,可以不连接Redis,只使用配置文件(app.properties),以便于测试。 服务端 GUI尚未实现,暂时只能在key=config:{...
由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中: 代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
自己整理结合网上的诸多资料开发的一个中间件,当然这个是demo,想做成完整的通讯中间件的话需要在handler处做处理,并对加载配置文件处做修改方可导出成jar集成到其他系统。 这里简单说下: 1.ij.properties配置...
3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
配置Redis连接:在application.properties或application.yml文件中配置Redis服务器的地址、端口以及其他相关参数,如密码、数据库索引等。 使用RedisTemplate:Spring Data Redis提供了一个名为RedisTemplate的类,...
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...
Mybatis已经有开发了一个“mybatis-generator-maven-plugin”插件供大众使用了,但该插件仅能通过配置固定的常量参数来使用数据源信息,无法满足从spring-cloud-config配置文件中读取数据源配置信息来生成代码。...
- 在Controller中使用@PreAuthorize等注解需要在spring-web配置文件中扫描security包下的类 6. 引用application.properties中的属性的方式:@ConfigurationProperties(prefix = "spring.mail") + @Component + ...
该属性指定加载Struts 2配置文件的配置文件管理器.该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现自己的配置管理器,开发者则可以实现一个实现...
并且期望有application.properties或application.yml文件之一,并且文件中需要有spring.application.name参数;import com.mylomen.mybatis.domain.XxqMybatisBO;import com.mylomen.mybatis.helper.DbInfoHelper;...
改变应用程序外部配置文件的位置 iii. 63.3. 使用'short'命令行参数 iv. 63.4. 使用YAML配置外部属性 v. 63.5. 设置生效的Spring profiles vi. 63.6. 根据环境改变配置 vii. 63.7. 发现外部属性的内置选项 iii. 64....
配置Spring和MyBatis:根据实际情况修改mybatis-config.xml和spring-mvc.xml文件中的配置信息,以便系统能够正确读取数据库表结构和执行SQL语句。启动项目:在命令行中进入项目根目录,执行./mvnw spring-boot:run...
配置简单,只有一个配置文件generator.properties 生成器的运行 在eclipse中运行 配置classpath,将generator/lib中的rapid-generator.jar及其它数据库驱动加入classpath 修改generator.xml的数据库连接属性及其它属性...