1. 程式人生 > >Springboot 注意事項 慢慢累積

Springboot 注意事項 慢慢累積

1.當使用這種方式配置資料來源,預設url名稱使用的是jdbc-url
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
2.使用這種方式配置資料來源,使用的是url
    @ConfigurationProperties(prefix = "local.datasource")
    public DataSourceProperties localDataSourceProperties() {
        return new DataSourceProperties();
    }
    @Bean(name = "localDateSource")
    @ConfigurationProperties(prefix = "local.datasource")
    public DataSource firstDataSource() {
        return localDataSourceProperties().initializeDataSourceBuilder().build();
    }
3.在使用springboot時使用freemarker作為模板是,預設情況下輸出的數字是千分位格式化的如:0,000,000。導致使用一些js框架時讀取數字失敗。
可以使用number_format: 0.##  設定格式。

1、spring boot maven Unable to find main class

在pom中加入 二者選其一  

<properties>
    <start-class>com.EurekaApplication</start-class>main方法類路徑
</properties>

 

<build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  			<configuration>
        		<mainClass>com.EurekaApplication</mainClass>main方法類路徑
    		</configuration>
  		</plugin>
  	</plugins>
  </build>

2、今天做專案,給方法名稱起了一個不正規的名字,居然出現了錯誤提示,好生奇怪,看提示資訊顯示在chinesedrug類中沒有test這個屬性,才發現原來springboot還有這種檢查。

將實現的介面repository註釋掉後,錯誤提示消失。

3、springboot專案啟動時,如果沒有配置資料庫配置,啟動時會丟擲如下異常。

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. 
If you have database settings to be loaded from a particular profile you may 
need to active it (no profiles are currently active).

直接原因是ClassPath中含有DataSource,所以必須在appliaction.yml/appliaction.properties中配置資料庫相關資訊。

 

簡單點說就是在maven中你匯入了資料庫配置。例如:

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

網路中大多解決辦法都是在啟動類註解中加入一下內容,意識是排除資料自動配置。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

個人認為這是個辦法,但不是個好辦法(你會說出於測試其他功能,我的回答是隻要你在程式碼中實現了repository就必須配置資料庫連線資訊,並且匯入資料庫驅動,不然即便配置中排除了資料庫配置那麼一樣會出錯誤「親測過哦」)。

因為如果你不使用資料庫何必匯入它,

如果使用資料庫那就應該在appliaction.yml/appliaction.properties中配置相關內容。

例如:

spring:
  datasource: 
      username: root
      password: 123
      url: jdbc:mysql://192.168.1.6:3306/engine
      driver-class-name: com.mysql.jdbc.Driver