Spring-boot框架——2.属性自动注入

在spring中注入jdbc配置项是通过@PropertySource和@Value以及@Bean结合jdbc.properties配置文件完成数据库的注入。

1.在springboot中:首先在默认配置文件Application.properties中添加jdbc配置:

XML 复制代码
jdbc.driverClassName=com.mysql.jsbc.Driver
jdbc.url=******************
jdbc.username=root
jdbc.password=123

2.创建一个jdbc配置类:JdbcProperties

java 复制代码
package com.lxj.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="jdbc")//spring容器创建属性读取类
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;
    }
}

@ConfigurationProperties 注解的作用是将 SpringBoot 外部化配置的配置项绑定到其标注的对象,通过访问该对象就能获取配置文件中配置项的值,要设置前缀。

3.写一个JdbcConfig类:

java 复制代码
package com.lxj.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {

    @Autowired
    private JdbcProperties jdbc;
    @Bean
    public DataSource dataSource(JdbcProperties jdbc){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(jdbc.getDriverClassName());
        dataSource.setUrl(jdbc.getUrl());
        dataSource.setUsername(jdbc.getUsername());
        dataSource.setPassword(jdbc.getPassword());
        return dataSource;
    }

}

@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

@Configuration的第二个作用是可以作为配置类,和@Component的作用一样,两者的差别是@Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。

@EnableConfigurationProperties的作用:是将让使用了 @ConfigurationProperties 注解的配置类生效,将该类注入到 IOC 容器中,交由 IOC 容器进行管理。

4.在controller中打印:

java 复制代码
package com.lxj.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 TestController {

    @Autowired
    private DataSource dataSource;

    @GetMapping("/hello")
    public String hello(){
        return "Hello Spring boot!"+dataSource;
    }
}

5.简化写法:

不用写JdbcProperties这个类;

修改JdbcConfig类:

java 复制代码
package com.lxj.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

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

}

@ConfigurationProperties注解直接作用在类方法上,可直接将同名的配置进行注入并自动设置每个属性。

相关推荐
wno704几秒前
Spring Boot整合Flyway
java·spring boot·后端
vx_Biye_Design5 分钟前
springboot旅游管理系统18006-计算机课程设计、毕业设计
java·spring boot·后端·python·elasticsearch·django·课程设计
IT_陈寒14 分钟前
为什么我的React组件一直在意外重渲染?
前端·人工智能·后端
EatFan35 分钟前
2026 Rust 后端技术栈选型:Axum 0.8 + Tokio + SQLx 全链路怎么搭
开发语言·后端·rust·tokio·serde·axum·sqlx
mldong1 小时前
审批详情页高亮的那个节点,"名字"其实是一串 id:jeeflow 工作流引擎的四块数据来源
后端·架构
高频因子挖掘机1 小时前
股票历史数据缺了一天怎么发现?用 Pandas 检查交易日期连续性
后端·github·api
高频因子挖掘机1 小时前
量化回测最危险的错误,不是程序报错,而是数据错了却正常运行
后端·github·api
云浪2 小时前
Go 堆实战:从完全二叉树到 Top-K 与任务调度
后端·go
光依旧9 小时前
MCP实战手记系列(十):MCP Server高可用实战
spring boot·高可用·nginx负载均衡·redis哨兵·mcp·微服务容错·resilience4j熔断