Struts2学习:Action获取properties文件的值

配置文件路径:

配置内容:

方法一:不使用自动装配,使用ClassLoader

Action内被调用的函数添加下段代码:

Properties props = new Properties();
props.load(UploadFileAction.class.getClassLoader().getResourceAsStream("/struts/struts-config.properties"));
System.out.println(props.getProperty("destPath"));

执行效果:

为了便于其他Action重用,可以将此部分写成一个工具类:

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

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

代码样例:

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);  
}

转载于:https://my.oschina.net/u/4108765/blog/3059596

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值