Spring Boot(四)-- 中的application.properties配置简介 2

  根据上文的介绍,我们已经对Spring Boot中的application.properties配置有了一定的了解,下面我们对此进行最后的介绍。
  紧接上文的讲解,如下:

普通属性的注入

  由于 Spring Boot 源自 Spring ,所以 Spring 中存在的属性注入,在 Spring Boot 中一样也存在。由于 Spring Boot 中,默认会自动加载 application.properties 文件,所以简单的属性注入可以直接在这个配置文件中写。
1、我们首先定义一个bean:

public class Book {

    private Integer id;

    private String author;

    private String name;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", author='" + author + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2、然后我们在Spring Boot 的application.properties配置文件中进行一下配置:

book.name=三体
book.author=刘慈欣
book.id=11

3、按照spring的方式,我们可以根据注解:@Value将这些属性注入到容器中去管理。

@Component
public class Book {

    @Value("${book.id}")
    private Integer id;

    @Value("${book.author}")
    private String author;

    @Value("${book.name}")
    private String name;
    
    //此处省略get和set方法

注意:既然要使用spring 的方式的话,那么Book对象本身也是要交给Spring容器管理的,如果Book没有交给Spring容器管理的话,那么Book的属性也是无法从Spring容器中获取的到。
4、最后,我们进行测试,启动项目,即可看到注入的属性的值

@RestController
public class HelloController {
    @Autowired
    Book book;
    @GetMapping("/hello")
    public String hello() {
        System.out.println(book);
        return "hello spring boot!";
    }
}

浏览器访问:
在这里插入图片描述
后台数据:
在这里插入图片描述
这里我们会发现数据可以获取到,但是乱码了,这时我们需要修改配置文件的编码格式:打开 Setting ,修改如下:
在这里插入图片描述
我们再次修改 application.properties 文件,再次启动项目,访问 http://localhost:8080/hello 后台显示数据如下,我们会发现数据没有再乱码了:
在这里插入图片描述

注意:一般来说,我们在 application.properties 文件中主要存放系统配置,这种自定义配置不建议放在该文件中,可以自定义 properties 文件来存在自定义配置。

  例如在 resources 目录下,自定义 book.properties 文件,内容如下:
在这里插入图片描述
使用这种方式,项目启动并不会自动的加载该配置文件,首先我们要配置项目启动时加载此配置文件:如果是在 XML 配置中,可以通过如下方式引用该 properties 文件:

<context:property-placeholder location="classpath:book.properties"/>

如果是在 Java 配置中,可以通过 @PropertySource 来引入配置:

@Component
@PropertySource("classpath:book.properties")
public class Book {

    @Value("${book.id}")
    private Integer id;

    @Value("${book.author}")
    private String author;

    @Value("${book.name}")
    private String name;

	//此处省略get和set方法

使用@PropertySource注解后,启动项目,就会自动加载book.properties文件。
注意:上面介绍的是我们所常用的Spring中属性注入的一种方式,和Spring Boot没有任何关系。

类型安全的属性注入

  Spring Boot 引入了类型安全的属性注入,如果采用 Spring 中的配置方式,当配置的属性非常多的时候,工作量就很大了,而且容易出错。

使用类型安全的属性注入,可以有效的解决这个问题。

@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix = "book")
public class Book {

    private Integer id;

    private String author;

    private String name;
    
	//此处省略get和set方法

注意:在这里,我们添加了@configurationProperties后,可能会报以下错误:
出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProperties这个注解时,所以问题出现在ConfigurationProperties注解。
根据提示的not found in classpath,查询此注解的使用关于怎么指定classpath,进而查询location,Spring Boot1.5以上版本@ConfigurationProperties取消location注解

在这里插入图片描述

官方解决方案:Maven引入依赖

<dependency>
   <groupId> org.springframework.boot </groupId>
   <artifactId> spring-boot-configuration-processor </artifactId>
   <optional> true </optional>
</dependency>

Spring Boot 中的这种方式,主要是引入 @ConfigurationProperties(prefix = “book”) 注解,并且配置了属性的前缀,此时会自动将 Spring 容器中对应的数据注入到对象对应的属性中,就不用通过 @Value 注解挨个注入了,减少工作量并且避免出错。

注意:使用这种方式时,如果需要注入的属性是在自定义配置文件中,那么 @PropertySource 这个注解还是不能省略的,如果你需要注入的属性在默认的 application.properties 中,那么则不需要了。

Spring Boot中application.properties配置文件介绍到此结束,感谢阅览!!!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值