SpringBoot问题集锦(添加中)

5 篇文章 0 订阅
4 篇文章 0 订阅

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 等异常;
  • 多线程:主线程和子线程的调用,线程抛出异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值