Spring family bucket of learning (1)

Preface:

September last year, my colleagues and I at the time geeks bought Ding Xuefeng 's Spring family bucket learning, curriculum is divided into 16 chapters in total, I have learned the most part, are recorded in a notebook before, today I want to record on the Blog, Share more knowledge to those in need of help, I will be the number of chapters as the content of the article, the original course speak in more detail, I am here to do a summary on some specific points.

1.Spring family bucket contains what?

Born in 2002 of a Rod JohnSon, Spring has now developed to version 5.x

SpringFrameWork

Lightweight solution for building enterprise applications

idea:

  • 1. embody the spirit of be tolerant to diversity
  • 2. maintain backward compatibility
  • 3. Focus API design
  • 4. pursue stringent code quality

SpringBoot

The concept: to quickly build applications based on Spring

Features:

  • 1. Fast 
  • 2. out of the box.
  • 3. To provide a variety of non-functional properties
  • 4. do not generate code XML configuration
  • 5. Built Tomcat container, starts very convenient.

 II. Understand the development trend of Spring

Changes point:

  • 1.Java 8+ Kotlin 
  • 2.WebFlux (the rise of asynchronous programming model)
  • 3. Remove a lot of support (Portlet obsolete, Velocity does not maintain a JasperReport out of fashion)

III. Creating a Spring Program

Omission

IV. How to configure a single data source

SpringBoot do what configuration

  • DataSourceAutoConfiguration  (配置DataSource)
  • DataSourceTransactionManagerAutoConfiguation(配置DataSourceTransactionManager)
  • JdbcTemplateAutoConfiguration (Configuration JdbcTemplate)

 

Mysql connection configuration information

spring.datasource.url=jdbc:mysql://localhost:3306/prod?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 pom dependent file introduced

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>


Configuring embedded database h2

(1) Create a new project SpringBoot

(2) In creating this project SpringBoot when I introduced hz and jdbc 

Pom.xml file contents are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
       <!--h2内嵌数据库-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(3) h2 connection configuration information in the configuration file

server.port=8007

management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=ALWAYS

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

(4) to the default database to add data h2

(5) to start the service this Spring

Print out data when the connection is h2 database.

 

V. How to configure multiple data sources

Configuration Notes multiple data sources:

  • 1. The different data sources to be arranged separately
  • 2. attention every time you use a data source
  •    a. plurality DataSource on how the system should be determined
  •    b. the corresponding facilities (affairs, ORM, etc.) how to choose DataSource

Multi data source configuration has two ways:

The first, fully rely on manual configuration, excluded SpringBoot related dependencies, this is entirely possible.

Second, if you want to collaborate with SpringBoot (choose one).

A way Configuring @Primary type of Bean

Second way. Excluded automatically configured out of SpringBoot

            (DataSourceAutoConfiguation  DataSourceTransactionManagerAutoConfiguation    JdbcTemplateAutoConfiguation )

 

Today, tell us about the use of the second approach:

step 1):

Configure the connection information in the configuration file:

server.port=8007

management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=ALWAYS

foo.datasource.url=jdbc:h2:mem:foo
foo.datasource.username=sa
foo.datasource.password=

bar.datasource.url=jdbc:h2:mem:bar
bar.datasource.username=sa
bar.datasource.password=

Step (2):

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        JdbcTemplateAutoConfiguration.class})
public class SpringDemoApplication {


    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class, args);
    }


}

Step (3):

package  com.example.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
public class MultiDataSourceConfig {
    private Logger log = LoggerFactory.getLogger(MultiDataSourceConfig.class);

    /**
     * Config Foo DataSource
     * @return
     */
    @Bean
    @ConfigurationProperties("foo.datasource")
    public DataSourceProperties fooDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource fooDataSource() {
        DataSourceProperties dataSourceProperties = fooDataSourceProperties();
        log.info("foo datasource: {}", dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager fooTxManager(DataSource fooDataSource) {
        return new DataSourceTransactionManager(fooDataSource);
    }

    /**
     *  Config Bar DataSource
     * @return
     */

    @Bean
    @ConfigurationProperties("bar.datasource")
    public DataSourceProperties barDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource barDataSource() {
        DataSourceProperties dataSourceProperties = barDataSourceProperties();
        log.info("bar datasource: {}", dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager barTxManager(DataSource barDataSource) {
        return new DataSourceTransactionManager(barDataSource);
    }
}

This article first concluded that, on the poor readability of the article content too much, I am going to come out in a few of the articles

 

 

 

 

Published 66 original articles · won praise 49 · views 20000 +

Guess you like

Origin blog.csdn.net/tangthh123/article/details/104945631