load resource file [ioutils]

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

方法1

1
2
3
//load file from classpath(resource path)
InputStream is = getClass().getResourceAsStream("/aaa.rule.properties");
String st=IOUtils.toString(is);

方法2

1
2
3
4
5
6
7
8
9
10
11
12
13

<bean id="contents2" class="org.apache.commons.io.IOUtils" factory-method="toString">
<constructor-arg value="classpath:aaa.rule.properties" type="java.io.InputStream" />
</bean>

#controler
@Autowired
private String contents2;

@RequestMapping("/index")
public void helloWorld(ModelMap model) {
System.out.println(contents2);
}