springboot中的日志:

    在默认的spring-boot-starter中,会引入spring-boot-starter-logging,

 

 

 而springboot-starte-longing中引入了:slf4j规范和logback默认实现。

 

 

springboot对jdbc的支持:

 引入依赖:

   

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

点进去实际上就是依赖:

 <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>3.2.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.7.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

一个连接池,一个spring-jdbc的包。这个的好处就是,通过条件注解来决定是否把jdbc-template装载到ioc容器中:

@Configuration
@ConditionalOnClass({ DataSource.class, JdbcTemplate.class })
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcTemplateAutoConfiguration {

    @Configuration
    static class JdbcTemplateConfiguration {

        private final DataSource dataSource;

        private final JdbcProperties properties;

        JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) {
            this.dataSource = dataSource;
            this.properties = properties;
        }

        @Bean
        @Primary
        @ConditionalOnMissingBean(JdbcOperations.class)
        public JdbcTemplate jdbcTemplate() {
            JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
            JdbcProperties.Template template = this.properties.getTemplate();
            jdbcTemplate.setFetchSize(template.getFetchSize());
            jdbcTemplate.setMaxRows(template.getMaxRows());
            if (template.getQueryTimeout() != null) {
                jdbcTemplate
                        .setQueryTimeout((int) template.getQueryTimeout().getSeconds());
            }
            return jdbcTemplate;
        }

    }

    @Configuration
    @Import(JdbcTemplateConfiguration.class)
    static class NamedParameterJdbcTemplateConfiguration {

        @Bean
        @Primary
        @ConditionalOnSingleCandidate(JdbcTemplate.class)
        @ConditionalOnMissingBean(NamedParameterJdbcOperations.class)
        public NamedParameterJdbcTemplate namedParameterJdbcTemplate(
                JdbcTemplate jdbcTemplate) {
            return new NamedParameterJdbcTemplate(jdbcTemplate);
        }

    }

}

spring中多数据源问题:

简单的可以借助AbstractRoutingDataSource这个抽象类,结合aop来实现动态切换。

springboot的实现:

@Configuration
public class JdbcDataSourceConfig {


    /**
     * 配置连接属性1
     * 前缀后面的属性要与DataSourceProperties类中的属性对应
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "app.datasource.db1")
    public DataSourceProperties db1Properties(){
        return new DataSourceProperties();
    }



    /**
     * 配置连接属性2
     * 前缀后面的属性要与DataSourceProperties类中的属性对应
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "app.datasource.db2")
    public DataSourceProperties db2Properties(){
        return new DataSourceProperties();
    }


    /**
     * 使用属性配置信息构建一个DataSource
     * @return
     */
    @Bean
    public DataSource  db1DataSource(){
        return db1Properties().initializeDataSourceBuilder().build();
    }


    @Bean
    public DataSource  db2DataSource(){
        return db2Properties().initializeDataSourceBuilder().build();
    }


    /**
     * 不单独配置bean的名称了,使用方法名作为默认名称
     * @return
     */

    @Bean
    public JdbcTemplate templateOne(){
        return new JdbcTemplate(db1DataSource());
    }


    @Bean
    public JdbcTemplate templateTwo(){
        return new JdbcTemplate(db2DataSource());
    }




}

测试代码:

@SpringBootTest(classes= Application.class)
@RunWith(SpringRunner.class)
public class JdbcTest {


    @Autowired
    JdbcTemplate templateOne;


    @Test
    public void test() throws Exception{

        String sql="insert into user(name,age) values('aa',18)";
        templateOne.execute(sql);
    }


}

报错:

 

 

 

因为在将DataSourceProperties去注入到DataSource的时候发现存在多个DataSourceProperties的bean。因为去无论是db1Properties方法还是还是db2Properties都是返回一个DataSourceProperties,springboot无法根据上下文去推导,是需要哪个注入。jdbctemplate 通过bean的名字指定需要加载哪一个。

Actuator:springboot的监控服务:

引入:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

启动应用: 访问: http://localhost:8080/actuator

 JMX:

支持自定义发布一些监控信息。springboot支持简化发布一个bean信息到jmx。