Spring Boot 学习笔记(二)——配置文件

application.properties

1、默认路径:src/main/java/resources;

2、功能——定义常量:

使用时,com.example.name;在文件中加入变量,变量名前面加路径?包名,区别变量,一般用包名,可以是任意前缀;

com.example.name=reshale
com.example.age=100

然后在控制类中,在变量上注解声明;

@Value("${com.example.name}") //留意$的用法。
private String name;
@Value("${com.example.age}") //留意$的用法。
private int age;

@RequestMapping("/")
public String index(){
	return name+" "+age;
}

3、作为2的续,另一种配置方法:

1)这些变量可以统一写在一个类里,统一配置;在domain包中定义的pojo类中,有get和set方法。
2)然后在pojo类上注解:@ConfigurationProperties(prefix = “com.example”),前缀和application.properties里的一致即可。但是一般用包名。
3)再在spring boot的入口类上注解:@EnableConfigurationProperties(Pojo.class)
4)最后在控制类中创建Pojo对象即可使用。

@RestController
public class HelloController {
	@Autowired
	Pojo pojo;

	@RequestMapping("/")
	public String index(){
		return pojo.getName()+" "+pojo.getAge();
	}
}

4、作为3的续,建立多个properties文件

如test.properties。
在Pojo上除了上面的@ConfigurationProperties(prefix = “com.example.test”)注解,还有
@Configuration
@PropertySource(“classpath:test.properties”)
第二个注解指定路径,classpath相当于src/main/resources。
那么问题来了,为什么3中的情况不用加@Configuration呢?

5、${random}生成随机值

${random.value}、${random.int}、${random.long}、${random.uuid};
${random.int(10)}、${random.int[1024,65536]};

6、命令参数配置

1、以下命令可以修改Tomcat的端口号:

java -jar xx.jar --server.port=9090

2、–表示对application.properties的属性赋值。
也可以用SpringApplication.setAddCommandLineProperties(false)禁用命令行。这个在哪里设置呢?
3、总之,spring boot 用很多地方可以设置属性,优先级也很复杂。以下简单记一下application.properties application.yml的配置位置和优先级:
一般都在resources目录里面吧,下面再新建config文件夹,然后config包里的会覆盖resources根目录的;
如果在一块,yml会覆盖properties;

7、Profile多环境配置properties

1、命名符合application-prod.properties规范,就可以在不同的配置文件中添加不同的属性值(环境变量);
2、使用时,在application.properties里激活:

spring.profiles.active=prod

或者添加命令行参数:

java -jar xxx.jar --spring.profiles.active=prod

8、@Profile注解配置不同的类

以数据库连接为例:
1、先定义接口:

public  interface DBConnector { 
	public  void  configure(); 
}

2、两个实现类:

/**
  * 测试数据库
  */
@Component
@Profile("testdb")
public class TestDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("testdb");
        return "test";
    }
}
/**
 * 生产数据库
 */
@Component
@Profile("proddb")
public class DevDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("proddb");
        return "prod";
    }
}

3、依然在application.properties里激活:

spring.profiles.active=testdb

4、最后在Controller类中使用:

@RestController
@RequestMapping("/task")  //这是指定类的一级路径
public class TaskController {

    @Autowired DBConnector connector ; //注入接口,面向接口编程

    @RequestMapping(value = {"/",""}) //指定方法的二级路径
     public String hellTask(){
     	connector.configure(); //根据application.properties里的配置打印testdb     
    }
}

还可以用spring.profiles.include来叠加profile:

spring.profiles.active: testdb
spring.profiles.include: proddb,prodmq

这又是什么玩意儿?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值