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注解直接作用在类方法上,可直接将同名的配置进行注入并自动设置每个属性。

相关推荐
码事漫谈6 小时前
WorkBuddy 固然很香,但豆姐她免费呀
后端
金銀銅鐵7 小时前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
zark_chuan7 小时前
怎么让 AI 写十万行代码不腐化:我的 AGENTS.md、Pre-Push 门禁与活体 Wiki
后端
Rain的Java大神之路9 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
源码_空间9 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端
AI开源软件10 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
geovindu10 小时前
python: PCA
开发语言·后端·python·算法
冰美式只要糖13 小时前
Prefill 长短请求混批,为什么 128 Token 也要等 2.58 秒?
后端
不甘平凡的小鸟13 小时前
vs code 打开spring boot项目
java·spring boot·后端
忆想不到的晖13 小时前
删掉 1800 行编排代码后,我用 Agent-Native + 薄 AOP 重写了内容工作台
后端·node.js·agent