Spring Boot属性注入方式

2021/8/18 23:36:43

本文主要是介绍Spring Boot属性注入方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Spring Boot属性注入方式

目标:能够使用@ConfigurationProperties实现Spring Boot配置文件配置项读取和应用

需求:

将配置文件中的配项读取到一个对象中;

实现:

可以使用Spring Boot提供的注解@ConfigurationProperties ,该注解可以将Spring Boot的配置文件(默认

必须为application.properties或application.yml )中的配项读取到-个对象中。

实现步骤:

1.创建配置项JdbcProperties类,在该类名上面添加@ConfigurationProperties ;

@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    private String url;
    private String driverClassName;
    private String username;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

2.将jdbc.properties修改名称为 application.properties ;

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3307/test
jdbc.username=root
jdbc.password=123456

3.将JdbcProperties注入到JdbcConfig ;

@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
@Bean
	public DataSource dataSource(JdbcProperties jdbcProperties){
	DruidDataSource dataSource= new DruidDataSource();
	dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
	dataSource.setUsername(jdbcProperties.getUsername());
	dataSource.setPassword(jdbcProperties.getPassword());
	dataSource.setUrl(jdbcProperties.getUrl());
	return dataSource;
  }

4.测试

package test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class HelloController {
    @Autowired
    private DataSource dataSource;
    @GetMapping("hello")
    public String hello(){
        System.out.println("DataSource="+dataSource);
        return "hello,spring boot!";
    }
}

启动springboot,断点查看结果

第二种更简洁的方式:

1.创建配置项JdbcProperties类:在原来的基础上 注释配置

//@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    private String url;
    private String driverClassName;
    private String username;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

2.将jdbc.properties修改名称为 application.properties ;

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3307/test
jdbc.usename=root
jdbc.password=123456

3.将JdbcProperties注入到JdbcConfig ;

@Configuration
public class JdbcConfig {
		@ConfigurationProperties(prefix = "jdbc")
    @Bean
    public DataSource dataSource() {
       return  new DruidDataSource();
    }
}

4.测试

启动springboot,断点查看结果

小结:
●使用@ConfigurationProperties编写配置项类将配置文件中的配置项设置到对象中
●使用@ConfigurationProperties在方法 上面使用



这篇关于Spring Boot属性注入方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程