• [springboot 2019-10-23] 自定义springboot自动配置


    1.创建springboot 3个关键类

    1.1 属性读取类:用于从配置文件中读取配置信息

    package com.shijt.springbootdemo.jdbc;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "mysql.jdbc")
    public class JDBCRead {
        private String driver;
        private String url;
        private String username;
        private String password;
    
        public String getDriver() {
            return driver;
        }
    
        public void setDriver(String driver) {
            this.driver = driver;
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        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;
        }
    }

    1.2 核心事件类

    package com.shijt.springbootdemo.jdbc;
    
    import java.sql.*;
    
    public class JDBCUtils {
        private String driver;
        private String url;
        private String username;
        private String password;
    
        public int testJdbc(){
    
            Connection con = null;
            try{
                //加载MySql的驱动类
                Class.forName("com.mysql.jdbc.Driver") ;
                con = DriverManager.getConnection(url , username , password ) ;
           //仅做测试,直接写死了sql String sql
    ="INSERT INTO `smbms_user` (`id`, `userCode`, `userName`) VALUES ('001', '001', 'zhangsan')"; PreparedStatement pstmt = con.prepareStatement(sql) ; int rows = pstmt.executeUpdate() ; pstmt.close(); return rows; }catch(ClassNotFoundException e){ System.out.println("找不到驱动程序类 ,加载驱动失败!"); e.printStackTrace() ; }catch(SQLException se){ System.out.println("数据库连接失败!"); se.printStackTrace() ; }catch (Exception e){ e.printStackTrace(); }finally { if (con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } return 0; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } 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; } }

    1.3 整合类:把属性读取类中的属性赋值给核心事件类

    package com.shijt.springbootdemo.jdbc;
    
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.Resource;
    
    @Configuration
    @EnableConfigurationProperties({JDBCRead.class})
    @ConditionalOnClass({JDBCUtils.class })
    @ConditionalOnProperty(prefix="mysql.jdbc",value = "enabled",matchIfMissing = true)
    public class JDBCAutoConfigration {
        @Resource
        private JDBCRead jdbcRead;
    
        @Bean
        @ConditionalOnMissingBean({JDBCUtils.class})
        public JDBCUtils getJDBCUtils(){
            //确定方法执行的时间(在springboot启动时,而不是调用JDBCUtils时)
            System.out.println("-----step into JDBCAutoConfigration getJDBCUtils-----");
            JDBCUtils jdbcUtils=new JDBCUtils();
            jdbcUtils.setDriver(jdbcRead.getDriver());
            jdbcUtils.setUrl(jdbcRead.getUrl());
            jdbcUtils.setUsername(jdbcRead.getUsername());
            jdbcUtils.setPassword(jdbcRead.getPassword());
            return jdbcUtils;
        }
    }

    2.在resources目录下META-INF文件夹,创建spring.factories文件

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.shijt.springbootdemo.jdbc.JDBCAutoConfigration

    把写好的整合类加入到自动配置中

    3.编写Controller类,调用JDBCUtils

    package com.shijt.springbootdemo.controller;
    
    import com.shijt.springbootdemo.jdbc.JDBCUtils;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.annotation.Resource;
    
    @Controller
    public class UserController {
        @Resource
        private JDBCUtils jdbcUtils;
    
        @RequestMapping("/testJdbc")
        @ResponseBody
        public int testJdbc(){
            return jdbcUtils.testJdbc();
        }
    
    }

    在appliaction.yml中配置数据库连接信息

    mysql:
      jdbc:
        driver: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456

    4.启动springboot,测试

     

  • 相关阅读:
    操作系统 实验一 命令解释程序的编写
    软件工程 《构建之法》1、2、3章读后感
    软件工程实验一 复利计算 第四次实验
    软件工程实验一 复利计算(第三次实验实验总结)
    软件工程实验一 复利计算
    实验0 了解和熟悉操作系统
    团队工作总结及自评 & 补上来的用户调研
    四则运算安卓版ver.mk2
    安卓版四则运算
    每日更新
  • 原文地址:https://www.cnblogs.com/shijt/p/11725627.html
Copyright © 2020-2023  润新知