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

相关推荐
石榴树下的七彩鱼19 分钟前
图片修复 API 接入实战:网站如何自动去除图片水印(Python / PHP / C# 示例)
图像处理·后端·python·c#·php·api·图片去水印
我叫黑大帅31 分钟前
为什么TCP是三次握手?
后端·网络协议·面试
我叫黑大帅1 小时前
如何排查 MySQL 慢查询
后端·sql·面试
techdashen1 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
一 乐1 小时前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
消失的旧时光-19431 小时前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
Rust研习社1 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
夕颜1112 小时前
Skill 机器人 vs Hermes Agent:两种「AI 越用越聪明」的路径
后端
IT_陈寒3 小时前
SpringBoot自动配置把我都整不会了
前端·人工智能·后端
一 乐3 小时前
旅游|基于springboot + vue旅游信息推荐系统(源码+数据库+文档)
java·vue.js·spring boot·论文·旅游·毕设·旅游信息推荐系统