常见问题汇总(十五)——关于Spring boot

1、spring-boot:run -Dserver.port=# 启动时端口依旧是默认8080

        热编译依赖spring-boot-devtools会使注入无效 

2、debug启动模块时,无法访问JSP文件        

       注意选择正确的工作目录

常见问题汇总(十五)——关于Spring boot_第1张图片

3、 ip + port + /health 访问actuator报404错误

       spring cloud 2.0 以后 actuator访问路径改为 ip + port + /actuator/health

4、修改后的html无法立即生效

        a:添加devtools依赖   


        org.springframework.boot
        spring-boot-devtools
        true

        b:设置idea

   1) “File” -> “Settings” -> “Build,Execution,Deplyment” -> “Compiler”,选中打勾 “Build project automatically” 。

 2) 组合键:“Shift+Ctrl+Alt+/” ,选择 “Registry” ,选中打勾 “compiler.automake.allow.when.app.running” 

5、环境动态切换失效,${name}未替换

     1)spring boot依赖管理

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                2.0.1.RELEASE
                pom
                import
            
        
    

    2)环境配置


        
            local
            
                true
            
            
                local
            
        
        
            test
            
                test
            
        
        
            prepare
            
                prepare
            
        
        
            prod
            
                prod
            
        
    

    3)配置过滤

 
            
                src/main/resources
                true
            
        
        
            src/main/environment/${profile.active}/jdbc.properties
            src/main/environment/${profile.active}/server.properties
        

    4)maven编译插件

   
                org.springframework.boot
                spring-boot-maven-plugin
            

6、yml与properties注入变量的区别

    1)yml文件

server:
  port: 8080
spring:
  profiles:
    active: @profileActive@   #注入maven变量 用@变量名@
  application:
    name: @pom.artifactId@
  boot:
    admin:
      username: ${security.user.name:admin}
      password: ${security.user.password:admin} 

#注入配置变量 用${变量名}

    2)properties

# DATABASE (MySQL)
spring.datasource.url=${jdbc.url}
spring.datasource.username=${jdbc.username}
spring.datasource.password=${jdbc.password}
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.filters=stat,wall,log4j
spring.datasource.useGlobalDataSourceStat=true
#server
spring.profiles.active= ${profile.active} 

#maven变量和配置变量都用${}

7、Spring Boot默认配置失效

在某个配置类中继承了WebMvcConfigurationSupport

8、Spring Boot从1.x升级为2.x时项目出错

1)升级对应的cloud:



   
            org.springframework.cloud
            spring-cloud-starter-eureka-client
        

    
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        



    
		org.springframework.cloud
		spring-cloud-starter-feign
	

     
	    org.springframework.cloud
		spring-cloud-starter-openfeign
	




        
            io.zipkin.java
            zipkin-autoconfigure-ui
        
        
            io.zipkin.java
            zipkin-server
        

        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
    

2)修改配置文件



spring:
  application:
    name: pig-eureka-server
  cloud:
    config:
      uri: http://localhost:10000
security:
  basic:
    enabled: true # 启用身份认证
  user:
    name: pig # 定义用户名
    password: gip6666 # 定义密码


spring:
  application:
    name: pig-eureka-server
  cloud:
    config:
      uri: http://localhost:10000
  security:
    basic:
      enabled: true # 启用身份认证
    user:
      name: pig # 定义用户名
      password: gip6666 # 定义密码



log.level: root

log.level.{targetPackage}: root

3)关闭csrf检测(2.x默认开启)

/**
 * @author sunyiran
 * @date 2018-10-31
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

4)升级过时的依赖

5)修改过时代码

 

 

 

 

你可能感兴趣的:(常见开发问题)