spring boot中的 spring el使用和 读取properties

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值