Spring Boot中的属性安全注入

在spring中也有属性注入,可以通过构造函数或者设置注入等方式进行属性的注入,那么在springboot中怎么注入呢,都知道,在springboot中是不会用xml配置文件的,所以就需要我们通过注解来进行属性的注入,下面我们来看看

创建springboot项目

可以参考Spring Boot项目创建的三种方式进行创建spring boot 项目

在项目的resources目录下的application.properties文件中配置属性

book.bookid=10
book.bookname=三国演义
book.author=罗贯中

定义POJO类

通过@Value注解,在成员变量上面进行属性的注入,application.properties文件默认会被加载,在pojo类中必须要提供属性的get和set方法,不然获取不到对应的属性值

package com.zhouym.propertiesdemo;
import org.springframework.stereotype.Component;

/**
 * 〈〉
 *
 * @author zhouym
 * @create 2019/8/6
 * @since 1.0.0
 */
@Component
public class Book {
	//变量名要与配置文件中的key对应
	@Value("${book.bookid}")
    private Integer bookId;
    @Value("${book.booname}")
    private String bookname;
    @Value("${book.author}")
    private String author;

    public Book() {
    }

    public Book(Integer bookId, String bookname, String author) {
        this.bookId = bookId;
        this.bookname = bookname;
        this.author = author;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public String getAuthor() {
        return author;
    }

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

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

我们在测试类中进行测试

package com.zhouym.propertiesdemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesdemoApplicationTests {
    @Autowired
    private Book book;

    @Test
    public void contextLoads() {
        System.out.println(book);
    }

}



我们来看看结果
在这里插入图片描述
我们会发现,上面通过@value注解,从配置文件中获取值,那么属性值少的话还可以,如果有很多,几十个,几个百个,那么是不是都要一个一个写@Value+属性,所以我们需要改进,利用@ConfigurationProperties注解,参数里面加上prefix前缀,在成员变量上面就不用@Value注解了,下面我们来看看具体的实现

POJO类改进

package com.zhouym.propertiesdemo;

//import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 〈〉
 *
 * @author zhouym
 * @create 2019/8/6
 * @since 1.0.0
 */
@Component
//可以添加自定义的的配置,但是要指定classpath,不然不会找到这个配置文件
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix="book")
public class Book {
    //@Value("${book.bookid}")
    private Integer bookId;
    //@Value("${book.bookname}")
    private String bookname;
    //@Value("${book.author}")
    private String author;

    public Book() {
    }

    public Book(Integer bookId, String bookname, String author) {
        this.bookId = bookId;
        this.bookname = bookname;
        this.author = author;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public String getAuthor() {
        return author;
    }

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

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

其他不变,运行看看
在这里插入图片描述
建议使用属性安全的注入

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值