一、从默认配置文件中取值:
今天介绍三种配置文件手动取值的方式:
springboot配置文件信息保存在application.properties中,默认可以spring.开头的进行spring进行一些常用参数的配置,但是很多时候我们需要手动配置一些配置,这就需要我们自己手动取值了,
application.propertis配置文件默认存在classpath/classpaht/config中。我们也可以通过配置在启动服务器时通过启动程序时通过手动配置参数置顶该路径,这里就暂时不做演示,今天主要学习的是如何取得配置文件中的值。
方式1:
通过 SpringApplication.run(Application.class, args);返回的上下文信息ConfigurableApplicationContext取的
Environment,然后通过getProperty(key);取得value,直接上代码:
配置文件key-value:
1
server.host=127.0.0.1
java代码:
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
context.getEnvironment().getProperty("server.host");
System.out.println("post" + context.getEnvironment().getProperty("server.host"));
输出信息:
这里的Environment也可以通过
@Autowired
Environment env;
自动装配。
方式2:
通过@Value(“${key}”)取值,
配置文件key-value:
1
server.post=8080
java代码
package com.wangx.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* 取值
* @author Administrator
*
*/
@Component("users")
public class User {
@Value("${server.post}")
private String post;
public void print() {
System.out.println(post);
}
}
Applilcation.java
User user = (User)context.getBean(“users”);
user.print();
控制台输出:
方式3:
在配置文件中通过${key}引用配置信息
配置文件:
name=springboot
server.name=this is a ${name}
User.java
package com.wangx.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* 取值
* @author Administrator
*
*/
@Component("users")
public class User {
@Autowired
Environment env;
@Value("${server.name}")
private String post;
public void print() {
System.out.println(post);
}
}
控制台信息:
‘
OK,三次取值 演示完毕。
**
二、在其它文件中写属性配置:
**
1、配置文件:
ylxy.properties:
mail.default.from=admin@ylxy.com
mail.debug=false
mail.transport.protocol=smtp
mail.host=192.168.75.100
mail.username=ylxy
mail.password=1234
2、启动类中引入配置文件
加入注解:@PropertySource(“ylxy.properties”),
如果想引入多个配置文件,则可以引入多个:@PropertySource({“aa.properties”,”bb.properties”,”cc.properties”),
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(value = "cn.com.ylxy.business")
@PropertySource("ylxy.properties")
@ImportResource("/spring/spring-data.xml")
public class App {
public static void main( String[] args ){
SpringApplication.run(App.class, args);
System.out.println( "启动完成!" );
}
}
3、启动项目后,直接可以在其他项目中引用即可
public class UserController {
@Value("${mail.default.from}")
private String mailFrom;
@Value("${mail.host}")
private String mailHost;
@Value("${mail.username}")
private String mailName;
/**
* 跳转到登陆页面
* @return
*/
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String toIndex(){
System.out.println(mailFrom);//会输出 admin@ylxy.com
System.out.println(mailHost);//会输出 192.168.75.100
System.out.println(mailName);//会输出 ylxy
return "index";
}
}