zoukankan      html  css  js  c++  java
  • 使用springboot配置和注入数据源属性的方法和步骤

      /**

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

    **/

        文件:application.properties

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://192.168.28.128
    jdbc.username=orcl
    jdbc.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")
    @Data
    public 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
    */
    @SpringBootApplication
    public 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
    */
    @RestController
    public class HelloController {
    @Autowired
    private DataSource dataSource;

    @GetMapping("hello")
    @ResponseBody
    public String Hello(){
    return "hello zmy,你好";
    }
    }
     
     
     
     
  • 相关阅读:
    NiosII软处理器快速入门- 10分钟学会NiosII(2)
    FFT算法的一种FPGA实现
    32个最热CPLDFPGA论坛
    NiosII软处理器快速入门- 10分钟学会NiosII(3)
    基于FPGA/CPLD设计与实现UART
    NiosII软处理器快速入门- 10分钟学会NiosII(1)
    LCD 的分类和显示原理
    iis6.0重写成html设置
    p标签之间的行距问题
    ie6 png图片透明方法
  • 原文地址:https://www.cnblogs.com/newcityboy/p/12049026.html
Copyright © 2011-2022 走看看