springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:

1.创建yml-springboot

springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第1张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第2张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第3张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第4张图片

2.application.properties配置端口

springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第5张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第6张图片

3.yml配置文件值注入

springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第7张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第8张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第9张图片

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
      - lisi
      - zhaoliu
    dog:
      name: 小狗
      age: 12

4.需要安装配置文件处理器

springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第10张图片

"1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   4.0.0</modelVersion>
   
      org.springframework.boot</groupId>
      spring-boot-starter-parent</artifactId>
      2.3.1.RELEASE</version>
      /> <!-- lookup parent from repository -->
   </parent>
   com.cevent</groupId>
   springboot-02-config</artifactId>
   0.0.1-SNAPSHOT</version>
   springboot-02-config</name>
   Demo project for Spring Boot</description>

   
      .version>1.8</java.version>
   </properties>

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

      
         org.springframework.boot</groupId>
         spring-boot-starter-test</artifactId>
         test</scope>
         
            
               org.junit.vintage</groupId>
               junit-vintage-engine</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <!--导入yml配置文件提示:配置文件处理器-->
      
         org.springframework.boot</groupId>
         spring-boot-configuration-processor</artifactId>
         true</optional>
      </dependency>
      <!--引入junit-->
<!--      
         junit</groupId>
         junit</artifactId>
         4.12</version>
         test</scope>
      </dependency>-->
   </dependencies>

   
      
         
            org.springframework.boot</groupId>
            spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>


5.自动生成的main class

package com.cevent.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot02ConfigApplication {

   public static void main(String[] args) {
      SpringApplication.run(Springboot02ConfigApplication.class, args);
   }

}


6.person类

package com.cevent.springboot.bean;/**
 * Created by Cevent on 2020/7/6.
 */

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cevent
 * @description
 * @date 2020/7/6 11:27
 */
//将application.yml配置的属性映射到Person,需要加入prefix="配置文件内的属性名"
    //只有这个@ConfigurationProperties组件是容器中的组件,才能提供功能@Component
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private boolean boss;
    private Date birth;

    private Map,Object> maps;
    private List lists;

    private Dog dog;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public boolean isBoss() {
        return boss;
    }

    public void setBoss(boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map, Object> maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}


 
  

7.dog类

package com.cevent.springboot.bean;/**
 * Created by Cevent on 2020/7/6.
 */

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author cevent
 * @description
 * @date 2020/7/6 11:44
 */

public class Dog {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


8.yml配置

server:
  port: 8082

person:
  lastName: cevent
  age: 28
  boss: false
  birth: 1992/11/22
  maps: {k1: cevent1,k2: echo1}
  lists:
    - kakaxi
    - mingren
  dog:
    name: 鲁班
    age: 2


9.启动失败

C:\JAVA\JDK\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\DevTools\IntelliJ IDEA 
….. com.intellij.rt.execution.junit.JUnitStarter 
-ideVersion5 -junit5 com.cevent.springboot.Springboot02ConfigApplicationTests,contextLoads
Exception in thread "main" 
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
	at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
	at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
	at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

更换为本地maven仓库,重新导包-不管用,但做错误记录】

"1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   4.0.0</modelVersion>
   
      org.springframework.boot</groupId>
      spring-boot-starter-parent</artifactId>
      2.3.1.RELEASE</version>
      /> <!-- lookup parent from repository -->
   </parent>
   com.cevent</groupId>
   springboot-02-config</artifactId>
   0.0.1-SNAPSHOT</version>
   springboot-02-config</name>
   Demo project for Spring Boot</description>

   
      .version>1.8</java.version>
   </properties>

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

      
         org.springframework.boot</groupId>
         spring-boot-starter-test</artifactId>
         test</scope>
         
            <!--
               org.junit.vintage</groupId>
               junit-vintage-engine</artifactId>
            </exclusion>-->
         </exclusions>
      </dependency>
      <!--导入yml配置文件提示:配置文件处理器-->
      
         org.springframework.boot</groupId>
         spring-boot-configuration-processor</artifactId>
         true</optional>
      </dependency>
      <!--引入junit-->
      
         junit</groupId>
         junit</artifactId>
         4.12</version>
         test</scope>
      </dependency>
   </dependencies>

   
      
         
            org.springframework.boot</groupId>
            spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>


springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第11张图片

10.只能升级idea,这里之前选用2017,现在使用2020版,安装如下

(1)官网下载
https://www.jetbrains.com/idea/download/other.html
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第12张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第13张图片
(2)安装
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第14张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第15张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第16张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第17张图片
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第18张图片
(3)默认进入只能register,我们退出后进入原来的old版本,我之前安装的是2017
然后退出,再进入2020版
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第19张图片
(4)弹出restart选项
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第20张图片
(5)因为不是默认的C盘位置,jar包不管用,退出project,进入configure配置
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第21张图片
(6)修改agent
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第22张图片
(7)重启idea,进入register
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第23张图片
(9)执行项目

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-07-06 23:16:57.428  INFO 40172 --- [           main] c.c.s.Springboot02ConfigApplicationTests : Starting Springboot02ConfigApplicationTests on LAPTOP-CQRDCFKL with PID 40172 (started by asus in D:\DEV_CODE\Intelligy_idead_code\spring\springboot\springboot-02-config)
2020-07-06 23:16:57.429  INFO 40172 --- [           main] c.c.s.Springboot02ConfigApplicationTests : No active profile set, falling back to default profiles: default
2020-07-06 23:16:58.244  INFO 40172 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-06 23:16:58.450  INFO 40172 --- [           main] c.c.s.Springboot02ConfigApplicationTests : Started Springboot02ConfigApplicationTests in 1.317 seconds (JVM running for 2.278)

Person{lastName='cevent', age=28, boss=false, birth=Sun Nov 22 00:00:00 CST 1992, maps={k1=cevent1, k2=echo1}, lists=[kakaxi, mingren], dog=Dog{name='鲁班', age=2}}

2020-07-06 23:16:58.608  INFO 40172 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

(9)配置properties

# server.port=8081
# idea默认支持utf-8 properties默认支持ASCii编码
# 设置personֵ
person.last-name=cevent
person.age=28
person.birth=1992/07/06
person.boss=false
person.maps.k1=key1
person.maps.k2=haha
person.lists=kakaxi,luban,xiaofengxian
person.dog.name=dogger
person.dog.age=188


(10)乱码解决
idea
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第24张图片
(11)修改配置:重启生效

-Xms128m
-Xmx2048m
-XX:ReservedCodeCacheSize=240m
-XX:+UseConcMarkSweepGC
-XX:SoftRefLRUPolicyMSPerMB=50
-ea
-XX:CICompilerCount=2
-Dsun.io.useCanonPrefixCache=false
-Djdk.http.auth.tunneling.disabledSchemes=""
-XX:+HeapDumpOnOutOfMemoryError
-XX:-OmitStackTraceInFastThrow
-Djdk.attach.allowAttachSelf=true
-Dkotlinx.coroutines.debug=off
-Djdk.module.illegalAccess.silent=true
#-javaagent:C:\Users\Public\.jetbrains\jetbrains-agent-v3.2.0.de72.619
-javaagent:D:\DEV\IntelligyIdea2020\jetbrains-agent.jar
-Dfile.encoding=UTF-8


springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第25张图片
【原因:application.properties文件问题,yml不会报错】
springboot:配置ymlDidea.test.cyclic.buffer.size=1048576 “-javaagent:_第26张图片

你可能感兴趣的:(springboot,maven,yml)