在配置文件中,还提供了随机数供我们使用,即在配置文件中使用${random}来生成不同类型的随机数,大致分为随机数、随机uuid、随机字符串等。在配置文件内添加几种利用随机数创建的属性,如代码
server.port=8089
book.name=spring boot 2
book.author=yangyang
#随机字符串
book.value=${random.value}
#随机int值
book.intValue=${random.int}
#随机long值
book.longValue=${random.long}
#随机uuid
book.uuid=${random.uuid}
#随机1000以内
book.randomNumber=${random.int(1000)}
#自定义属性引用
book.title= The book name is ${book.name}
在配置了这么多属性后,可以使用JavaBean模式来给属性赋值,创建一个BookConfigBean实体类。
由于自定义属性的前缀都是由book开头的,因此我们可以在实体类上加入注解@ConfigurationProperties(prefix = "book"),同时需要在启动类上加入注解@EnableConfigurationProperties(BookConfigBean.class),表明启动这个配置类。
package com.shrimpking;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(BookConfigBean.class)
@SpringBootApplication
public class Springboot69Application
{
public static void main(String[] args)
{
SpringApplication.run(Springboot69Application.class, args);
}
}
实体类内容如代码
package com.shrimpking;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/12/20 22:33
*/
@ConfigurationProperties(prefix = "book")
public class BookConfigBean
{
private String name;
private String author;
private String value;
private int intValue;
private long longValue;
private String uuid;
private int randomNumber;
private String title;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public int getIntValue()
{
return intValue;
}
public void setIntValue(int intValue)
{
this.intValue = intValue;
}
public long getLongValue()
{
return longValue;
}
public void setLongValue(long longValue)
{
this.longValue = longValue;
}
public String getUuid()
{
return uuid;
}
public void setUuid(String uuid)
{
this.uuid = uuid;
}
public int getRandomNumber()
{
return randomNumber;
}
public void setRandomNumber(int randomNumber)
{
this.randomNumber = randomNumber;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
}
到这里,配置就完成了。接下来在Controller中利用@Autowired注解注入BookConfigBean类,并且创建一个test2方法进行测试。test2方法及注入BookConfigBean类的内容如代码
package com.shrimpking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/12/20 22:36
*/
@RestController
public class BookController
{
@Autowired
private BookConfigBean bookConfigBean;
@GetMapping("/test2")
public BookConfigBean test2(){
return bookConfigBean;
}
}
在浏览器上访问http://localhost:8089/test2进行测试,显示结果如下:

3953

被折叠的 条评论
为什么被折叠?



