配置文件路徑:java
配置內容:mysql
方法一:不使用自動裝配,使用ClassLoaderweb
Action內被調用的函數添加下段代碼:spring
Properties props = new Properties(); props.load(UploadFileAction.class.getClassLoader().getResourceAsStream("/struts/struts-config.properties")); System.out.println(props.getProperty("destPath"));
執行效果:sql
爲了便於其餘Action重用,能夠將此部分寫成一個工具類:mybatis
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裏如此調用便可:app
Properties props = new StrutsConfig().GetProperties(UploadFileAction.class); System.out.println(props.getProperty("destPath"));
方法二:@ConfigurationProperties + @PropertySourceide
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); }