spring boot 依靠其强大的annotation大大提高开发效率,下面我们看个简单的例子:
项目结构如图:
hello.txt和 test.properties文件内容如下:
你好,spring boot
hello spring boot
此示例演示spring el表达式和读取properties
book.author=\u5B8B\u4F1F\u5B81
book.name=spring boot
book.price=99
pom.xml文件中需要引入如下依赖:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
其它代码如下:
package bw.swn.boot.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Value("其它属性值")
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}
package bw.swn.boot.service;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
@Configuration
@ComponentScan("bw.swn.boot.service")
@PropertySource("classpath:bw/swn/boot/test.properties")
public class ElConfig {
@Value("I LOVE YOU")
private String normal;
@Value("#{systemProperties['os.name']}")
private String osName;
@Value("#{userService.another}")
private String fromAnthor;
@Value("classpath:bw/swn/boot/hello.txt")
private Resource testFile;
@Value("http://www.baidu.com")
private Resource url;
@Value("${book.name}")
private String bookName;
@Value("${book.author}")
private String bookAuthor;
@Value("${book.price}")
private Integer bookPrcie;
@Autowired
private Environment environment;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
}
public void outputReource() {
try {
System.out.println(this.normal);
System.out.println(this.osName);
System.out.println(fromAnthor);
System.out.println(org.apache.commons.io.IOUtils.toString(this.testFile.getInputStream()));
System.out.println(IOUtils.toString(this.url.getInputStream()));
System.out.println(this.bookAuthor);
System.out.println(this.bookName);
System.out.println(this.bookPrcie);
System.out.println(this.environment.getProperty("book.author"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
package bw.swn.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import bw.swn.boot.service.ElConfig;
@SpringBootApplication
public class SpringElPropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringElPropertiesApplication.class, args);
AnnotationConfigApplicationContext ctx=
new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig service=ctx.getBean(ElConfig.class);
service.outputReource();
ctx.close();
}
}