使用springboot配置和注入数据源属性的方法和步骤

/**

1、书写一个名为resources/application.properties的属性文件---->书写一个配置属性类,类名为:

**/

文件:application.properties

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.28.128jdbc.username=orcljdbc.password=123456

文件:JdbcProperties
package com.hope.config;

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

/** * @author newcityman * 使用lombok插件 * @date 2019/12/16 - 0:59 */@ConfigurationProperties(prefix = "jdbc")@Datapublic class JdbcProperties {    String driverClassName;    String url;    String username;    String password;}
package com.hope.config;

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

import javax.sql.DataSource;

/** * @author newcityman * @Bean 把类的返回值注入到spring核心容器便于调用 * @date 2019/12/15 - 23:52 */@Configuration/*@PropertySource("classpath:application.properties")*/@EnableConfigurationProperties(JdbcProperties.class)public class JdbcConfig {    @Bean    public DataSource dataSource(JdbcProperties jp){        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName(jp.getDriverClassName());        dataSource.setUrl(jp.getUrl());        dataSource.setUsername(jp.getUsername());        dataSource.setPassword(jp.getPassword());        return dataSource;    }}
package com.hope;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RestController;

/** * @author newcityman * @date 2019/12/15 - 23:09 */@SpringBootApplicationpublic class BootDemoApplication {    public static void main(String[] args) {        SpringApplication.run(BootDemoApplication.class,args);    }}
package com.hope.web;

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

/** * @author newcityman * @date 2019/12/15 - 23:28 */@RestControllerpublic class HelloController {    @Autowired    private DataSource dataSource;

    @GetMapping("hello")    @ResponseBody    public String Hello(){        return "hello zmy,你好";    }}
 
 
 
 

原文地址:https://www.cnblogs.com/newcityboy/p/12049026.html

时间: 2024-07-30 21:29:52

使用springboot配置和注入数据源属性的方法和步骤的相关文章

037.[转] springboot 配置多个数据源

1.在application.properties文件 配置两个数据源 #默认使用 tomcat-jdbc spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource spring.datasource.data1.url=jdbc:mysql://127.0.0.1:3306/mysql-boot spring.datasource.data1.username=root spring.datasource.data1.passw

SpringBoot配置Druid数据源

在我刚开始接触JDBC的时候,用的是DriveManager驱动来连接数据库的.而现在大多是用DataSource. 这里先简单说一下区别: 1.datasource是与连接池获取连接,而DriverManager是获取与数据库的连接!DriverManager类的主要作用是管理注册到DriverManager中的JDBC驱动程序,并根据需要使用JDBC驱动程序建立与数据服务器的网络连接.但是建立与数据库的连接是一项较耗资源的工作,频繁的进行数据库连接建立操作会产生较大的系统开销,为了解决上述问

SpringBoot配置属性之Migration

SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之NOSQL SpringBoot配置属性之MQ SpringBoot配置属性之Security SpringBoot配置属性之Migration SpringBoot配置属性之其他 另外附上个人关于springboot的一些文章 SpringBoot前世今生 SpringBoot集成mybatis S

SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC 版项目里面,所有的项目都一定需要满足于如下几点要求: · 访问的端口不能够是 8080,应该使用默认的 80 端口: · 在项目之中为了方便进行数据的维护,建议建立一系列的*.properties 配置文件,例如:提示消息.跳转路径: · 所有的控制器现在都采用了 Rest 风格输出,但是正常来讲

SpringBoot入门之基于Druid配置Mybatis多数据源

上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,SpringBoot就会基于约定根据配置信息实例化对象,但是一般大型的项目都是有多个子系统或者多个数据源组成,那怎么使用SpringBoot进行Mybatis多数据源配置呢? 一.数据库准备 我们这里准备使用主从两个数据库来进行演示多数据源配置.一个主库用来写write,一个从库用来读read.至于两个

【转】SpringBoot配置属性系列-之DataSource

SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之NOSQL SpringBoot配置属性之MQ SpringBoot配置属性之Security SpringBoot配置属性之Migration SpringBoot配置属性之其他 另外附上个人关于springboot的一些文章 SpringBoot前世今生 SpringBoot集成mybatis S

myibtais配置数据源属性生效优先级

datasource属性生肖顺序为:优先级最高的是  直接指点在dtatsource里的属性: eg: <dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /> </dataSource> 其次为 :<properties resource="com/config/Config.prope

SpringBoot配置属性之NOSQL

SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之NOSQL SpringBoot配置属性之MQ SpringBoot配置属性之Security SpringBoot配置属性之Migration SpringBoot配置属性之其他 另外附上个人关于springboot的一些文章 SpringBoot前世今生 SpringBoot集成mybatis S

SpringBoot配置属性之MQ

SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之NOSQL SpringBoot配置属性之MQ SpringBoot配置属性之Security SpringBoot配置属性之Migration SpringBoot配置属性之其他 另外附上个人关于springboot的一些文章 SpringBoot前世今生 SpringBoot集成mybatis S