[Springboot 2019-10-23] custom springboot autoconfiguration

1. Create springboot 3 key categories

1.1 Properties read class: for reading the configuration information from the configuration file

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 Core Event Class

Package com.shijt.springbootdemo.jdbc; 

Import the java.sql *. ; 

public  class JDBCUtils {
     Private String Driver;
     Private String URL;
     Private String username;
     Private String password; 

    public  int testJdbc () { 

        Connection CON = null ;
         the try {
             / / load MySql driver class 
            Class.forName ( "com.mysql.jdbc.Driver" ); 
            CON = DriverManager.getConnection (url, username, password); 
       // do the only test, write directly sql dead SQL String
= "smbms_user` the INSERT the INTO` ( `id`, userCode``, `username`) the VALUES ( '001', '001', 'zhangsan')" ; the PreparedStatement pstmt = con.prepareStatement (SQL); int rows = pstmt.executeUpdate (); pstmt.close (); return rows; } the catch (a ClassNotFoundException E) { System.out.println ( "class not found driver, driver failed to load!" ); e.printStackTrace (); } the catch (SQLException SE) { System.out.println ( "database connection failed!" ); SE.printStackTrace (); } the 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 Integration class: the property to read class attributes assigned to the core event classes

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 to true )
 public  class JDBCAutoConfigration { 
    @Resource 
    Private JDBCRead jdbcRead; 

    @Bean 
    . @ConditionalOnMissingBean (JDBCUtils { class } )
     public JDBCUtils getJDBCUtils () {
         // determining method performed in time (starting at springboot, rather than when the call JDBCUtils) 
        System.out.println ( "STEP INTO JDBCAutoConfigration getJDBCUtils ----- -----" ); 
        jdbcUtils JDBCUtils = new new JDBCUtils ();
        jdbcUtils.setDriver(jdbcRead.getDriver());
        jdbcUtils.setUrl(jdbcRead.getUrl());
        jdbcUtils.setUsername(jdbcRead.getUsername());
        jdbcUtils.setPassword(jdbcRead.getPassword());
        return jdbcUtils;
    }
}

2. META-INF file in the resources directory folder, create a file spring.factories

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

The integrated type is added to the written Autoconfiguration

3. Write Controller class, call 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();
    }

}

Configuration database connection information in the 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. Start springboot, test

 

 

 

Guess you like

Origin www.cnblogs.com/shijt/p/11725627.html