zoukankan      html  css  js  c++  java
  • [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,测试

     

  • 相关阅读:
    (二)、一步一步学GTK+之窗口
    phpcms v9 评论的bug.
    为discuz x2.5添加播放附件(mp4)的方法
    code::blocks + C + lua 编译环境
    C语言从声卡录音的一个demo
    泛型集合(.NET 2.0)
    VS2008对ASP.NET引用的外部JS文件不能调试
    for循环和foreach
    CSS之DIV上下左右居中
    GridView控件相关(来自互联网)
  • 原文地址:https://www.cnblogs.com/shijt/p/11725627.html
Copyright © 2011-2022 走看看