2019独角兽企业重金招聘Python工程师标准>>>
配置文件路径:
配置内容:
方法一:不使用自动装配,使用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
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);
}