SpringBoot问题集锦

  • 1、Bean类无法扫描到或者指定注解的类无法启动后扫描到
  • 2、properties 文件读取中文乱码问题
  • 3、Controller返回字符串中文乱码问题
  • 4、继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效,或者静态接口无法访问失效
  • 5、Spring Data Redis - Could not safely identify store assignment for repositor
  • 6、Getters of lazy classes cannot be final Kotlin Spring Boot
  • 7、Using org.hibernate.id.UUIDHexGenerator which does not generate IETF
  • 8、spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
  • 9、required a single bean, but 2 were found
  • 10、Loading class `com.mysql.jdbc.Driver'. This is deprecated.
  • 11、logback.xml 不生效解决
  • 12、如何关闭 DEBUG org.apache.http.**日志
  • 13、事务不生效的几种 Case


1、Bean类无法扫描到或者指定注解的类无法启动后扫描到

响应注解所在的类或者相应的Bean没有在启动Application所在的相同包目录下

2、properties 文件读取中文乱码问题

val props = Properties()
 var inputStream: InputStream? = null
 try {
     inputStream = this::class.java.classLoader.getResourceAsStream(propertiesName)
     if (inputStream != null) {
         val isr = InputStreamReader(inputStream, "UTF-8")
         val br = BufferedReader(isr)
         props.load(br)
         props.load(isr)
     } else {
         getLogUtil().logE(this::class.java, "${propertiesName}配置文件加载异常")
    }
 } catch (e: Exception) {
    getLogUtil().logE(this::class.java, "${propertiesName}配置文件加载异常")
 } finally {
     try {
         inputStream?.close()
     } catch (e: IOException) {
         getLogUtil().logE(this::class.java, "${propertiesName}文件流关闭出现异常")
     }
 }

3、Controller返回字符串中文乱码问题

@Configuration
class MyConfiguration : WebMvcConfigurationSupport() {
    override fun extendMessageConverters(converters: List<HttpMessageConverter<*>?>) {
        // 解决controller返回字符串中文乱码问题
        for (converter in converters) {
            if (converter is StringHttpMessageConverter) {
                converter.defaultCharset = StandardCharsets.UTF_8
            }
        }
    }
}

4、继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效,或者静态接口无法访问失效

@Configuration
class MyConfiguration : WebMvcConfigurationSupport() {
    /**
     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     *
     * @param registry
     */
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/")
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/")
        super.addResourceHandlers(registry)
    }

    /**
     * 配置servlet处理
     */
    override fun configureDefaultServletHandling(configurer: DefaultServletHandlerConfigurer) {
        configurer.enable()
    }
}

5、Spring Data Redis - Could not safely identify store assignment for repositor

原因:

  • 使用了Spring data jpa 作为持久层框架
  • 使用了Spring Redis 做缓存

解决办法:

  • properties文件配置JPA处:
    spring.data.redis.repositories.enabled=false
  • yaml文件:
spring:
   data:
     redis:
       repositories:
         enabled: false

6、Getters of lazy classes cannot be final Kotlin Spring Boot

entry表中字段定义加上open

open var name: Int? = null

7、Using org.hibernate.id.UUIDHexGenerator which does not generate IETF

Hibernate 3.6开始,如果有model的主键有uuid生成,就会报这个错误,采用最新的生成策略,改成下面的就会正常

@GenericGenerator(name = "system-uuid", strategy = "uuid.hex")  
@Id  
@GeneratedValue(generator = "system-uuid")  
@Column(name = "user_id")  
-----------------------------------------改成下面-----------------------------------------
@Id  
@Column(length = 32, nullable = false)  
@GeneratedValue(generator = "uuid2" )   //指定生成器名称  
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator" )  //生成器名称,uuid生成类  


<id name="id" type="string">  
     <column name="ID" length="32" />  
     <generator class="uuid" />  
</id>
-----------------------------------------改成下面-----------------------------------------
<id name="id" type="string">  
     <column name="ID" length="36" />  
     <generator class="uuid2" />  
</id>

8、spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

问题原因:
配置JpaBaseConfiguration.java:234- spring.jpa.打开-默认情况下,“视图中”处于启用状态。
因此,可以在视图呈现期间执行数据库查询。显式配置spring.jpa.打开-以禁用此警告。

解决方案:
在配置文件中加入下面这个:
spring.jpa.open-in-view=false

9、required a single bean, but 2 were found

项目的bean中出现多个相同类的bean,但名字不一定一样,例如:No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate

解决方案:

①
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;
②
applicationContext.getBean<RedisTemplate<String, Any>>("redisTemplate")

10、Loading class `com.mysql.jdbc.Driver’. This is deprecated.

数据库驱动com.mysql.jdbc.Driver已经被弃用了、应当使用新的驱动com.mysql.cj.jdbc.Driver
所以,按照提示更改jdbc.properties配置 com.mysql.jdbc.Driver 改为 com.mysql.cj.jdbc.Driver

11、logback.xml 不生效解决

logback配置文件没有放在src-resourse目录。

12、如何关闭 DEBUG org.apache.http.**日志

springBoot项目中在resources目录下创建一个文件:logback.xml 复制以下内容:

<configuration>
    <logger name="org.apache" level="WARN" />
    <logger name="httpclient" level="WARN" /> 
</configuration>

13、事务不生效的几种 Case

  • 类内部访问:A 类的 a1 方法没有标注 @Transactional,a2 方法标注 @Transactional,在 a1 里面调用 a2;
  • 私有方法:将 @Transactional 注解标注在非 public 方法上;
  • 异常不匹配:@Transactional 未设置 rollbackFor 属性,方法返回 Exception 等异常;
  • 多线程:主线程和子线程的调用,线程抛出异常。