Struts2学习:Action获取properties文件的值
配置文件路径:

配置内容:

方法一:
Action内被调用的函数添加下段代码:
Properties props = new Properties();
props.load(UploadFileAction.class.getClassLoader().getResourceAsStream("/struts/struts-config.properties"));
System.out.println(props.getProperty("destPath"));
执行效果:

为了便于其他Action重用,可以将此部分写成一个工具类:
package com.struts2demo.demo.util;
import com.opensymphony.xwork2.Action;
import java.io.IOException;
import java.util.Properties;
public class StrutsConfig {
public Properties GetProperties(Class actionClass) throws IOException {
Properties props = new Properties();
props.load(actionClass.getClassLoader().getResourceAsStream("/struts/struts-config.properties"));
return props;
}
}
然后Action里如此调用即可:
Properties props = new StrutsConfig().GetProperties(UploadFileAction.class);
System.out.println(props.getProperty("destPath"));
方法二:@ConfigurationProperties + @PropertySource
package com.example.spdemo.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@ConfigurationProperties
@PropertySource("/struts/struts-config.properties")
public class Index {
private String destPath;
@RequestMapping("/")
public String getProps() {
return destPath;
}
public String getDestPath() {
return destPath;
}
public void setDestPath(String destPath) {
this.destPath = destPath;
}
}
对于非application.properties的配置,需@PropertySource指明配置文件的路径;否则默认是从application.properties中读取配置,不需要写该注解。
方法三:spring的xml配置中配置上properties,然后通过@Value注入(该方法在Spring MVC中验证过,但是不知道集成struts2后是否受影响)
spring-context.xml
<util:properties id="APP_PROP" local-override="true" location="classpath:owlforest.properties" />
owlforest.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/linfu_test_db?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root
代码样例:
package com.owlforest.www.config.dao;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
public class DataSourceConfiguration {
@Value("#{APP_PROP['jdbc.driver']}")
private String driver;
@Value("#{APP_PROP['jdbc.url']}")
private String url;
@Value("#{APP_PROP['jdbc.username']}")
private String username;
@Value("#{APP_PROP['jdbc.password']}")
private String password;
public DruidDataSource createDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDefaultAutoCommit(true);
return dataSource;
}
}
方法四:使用spring的PropertiesFactoryBean
@Bean
public PropertiesFactoryBean getProperties() {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocations(new ClassPathResource("application.properties"));
return propertiesFactoryBean;
}
调用:
@Autowired
private PropertiesFactoryBean propertiesFactoryBean;
public String getProperties(String key) throws IOException {
Properties properties = propertiesFactoryBean.getObject();
return properties.getProperty(key);
}
方法五:不使用自动装配,使用classLoader获取Properties
MailUtil.class.getClassLoader().getResourceAsStream("/config.properties")
Struts2学习:Action获取properties文件的值的更多相关文章
- Java学习-021-Properties 获取配置项对应的值
在日常的脚本编写过程中,通常会获取配置文件中的配置项,以执行相应的业务逻辑. 小二上码...若有不足之处,敬请大神指正,不胜感激! 获取配置项值的源码如下所示: /** * Get value fro ...
- 【Properties】获取Properties文件
获取Properties文件 package com.chinamobile.epic.tako.v2.query.commons; import org.springframework.core.i ...
- 获取properties文件的内容
获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...
- spring Controller 层注解获取 properties 里面的值
前言:最近在做一个项目,想要在 controller 层直接通过注解 @Value("")来获取 properties 里面配置的属性. 这个其实和 springmvc.sprin ...
- struts2,action上传文件
通过servlet实现文件上传,可以用用servlet接受到request的值的话:主要是这句话 List<?> items = upload.parseRequest(request); ...
- Spring获取properties文件中的属性
1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...
- 获取.properties配置文件属性值
public class TestProperties { /** * * @Title: printAllProperty * @Description: 输出所有配置信息 * @param pro ...
- spring通过静态方法获得properties文件的值
获得spring bean方法 @Component public class BeanUtils implements ApplicationContextAware { private stati ...
- struts2学习笔记之十:文件上传
Struts2的上传 1.Struts2默认采用了apache commons-fileupload 2.Struts2支持三种类型的上传组件 3.需要引入commons-fileupload相关依赖 ...
随机推荐
- http 2.0 新特性
http 2.0 新特性: 二进制分帧 首部压缩 流量控制 多路复用 请求优先级 服务器推送 出处:https://juejin.im/post/5a4dfb2ef265da43305ee2d0
- Jmeter --- 组件执行顺序与作用域
一.Jmeter重要组件: 1)配置元件---Config Element: 用于初始化默认值和变量,以便后续采样器使用.配置元件大其作用域的初始阶段处理,配置元件仅对其所在的测试树分支有效,如,在同 ...
- [转]使用 Angular CLI 和 ng-packagr 构建一个标准的 Angular 组件库
使用 Angular CLI 构建 Angular 应用程序是最方便的方式之一. 项目目标 现在,我们一起创建一个简单的组件库. 首先,我们需要创建一个 header 组件.这没什么特别的,当然接下来 ...
- Git的一些东西(后续补充)
查看帮助,要装git-doc,另外推荐git的图形客户端gitg,比gitk好看多了,用apt-get install就可 HEAD是当前工作版本的指针 --global保存的是当前用户的配置 ...
- sql server 拼接字段
方式一: --select @p_AllPARTOFCHECK = (select CAST(t.PARTOFCHECK as varchar)+'|' from QUEUEDETAIL t wher ...
- 下载goland解压错误
把连接里面的 download.jetbrains.8686c.com 换成 download-cf.jetbrains.com
- PCM简介
1. 差分脉冲编码调制 如果两个相邻抽样值之间的相关性很大,那么它们的差值就较小,这样,仅对差值量化可以使用较少的比特数,此即差分PCM,或DPCM. 为了理论方便,我们将采样和量化分开,并用不带上三 ...
- kafka产生的数据通过Flume存到HDFS中
试验目标: 把kafka的生产者发出的数据流经由Flume放到HDFS来存储. 试验环境: java:1.8 kafka:2.11 flume:1.6 hadoop:2.8.5 试验流程: 1.进入z ...
- 服务网关zuul之一:入门介绍
通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 我们使用Spring Cloud Netflix中的Eureka实现了服务 ...
- Python——函数,模块,简单文件读写(python programming)
函数(function)定义原则: 最大化代码重用,最小化代码冗余,流程符合思维逻辑,少用递归; 函数的定义方法: def function_name(param_1, param_2): ..... ...