ในบทเรียนก่อนหน้านี้เราได้สร้างโครงการ Springboot ผ่านเครื่องมือ IDEA โดยไม่มีการกำหนดค่าใด ๆ และได้รับการเปิดตัวสำเร็จ แต่เราทุกคนชัดเจนว่าสิ่งเหล่านี้ยังห่างไกลจากความต้องการของโครงการจริงของเรา ตัวอย่างเช่นเราต้องการแนะนำการกำหนดค่า Redis ของเราเองการกำหนดค่า MySQL ฯลฯ เราควรจัดการกับมันอย่างไร? ใน Spring MVC เราทุกคนกำหนดค่าผ่านไฟล์ที่เกี่ยวข้องกับ Spring.xml ใน Springboot สิ่งเหล่านี้ไม่มีอยู่อีกต่อไป เราควรกำหนดค่าอย่างไร? ไม่ต้องกังวลมาอธิบายคำตอบของทุกคนทันทีตามฉันมา!
no1. เราจะแยกแยะสภาพแวดล้อมมากมายเมื่อเราทำโครงการ? ตัวอย่างเช่นสภาพแวดล้อมการพัฒนาสภาพแวดล้อมการทดสอบสภาพแวดล้อมการผลิต ฯลฯ จากนั้นขั้นตอนแรกที่ฉันจะพาคุณไปกำหนดค่าแต่ละสภาพแวดล้อม
1. ก่อนเปิดไฟล์ pom.xml ของโครงการของเราและเพิ่มเนื้อหาต่อไปนี้:
<muild> <minalname> $ {project.artifactid}-$ {project.version} </finalname> <plugins> <plugin> <molin> <roupId> org.springframework.boot </groupid> </Execution> </Executions> </plugin> <plugin> <sloupId> org.apache.maven.plugins </groupId> <ArtIfactId> maven-compiler-plugin </artifactid> <Sencod> <SONCOD> <CODINGUNITING> </plugin> </plugins> <filters> <filter> src/main/resources/application-$ {ตัวกรอง-resource-name} .properties </filter> </filters> <resources> <resource> <EXCLUDE> ตัวกรอง/*</excrude> <excrude> Application-dev.properties </exclude> <cedsid> แอปพลิเคชันทดสอบ. <incond> <cluve> แอปพลิเคชัน-$ {ตัวกรอง-resource-name} .properties </include> </cold> </sorsors> </sorses> </build> </profiles> <id> <id> dev </id> <Active> </properties> </premerties> </profile> <profile> <id> ทดสอบ </id> <properties> <filter-resource-name> ทดสอบ </filter-resource-name> </premerties> </properties> <id> <id> alpha </id> <properties> <crofile> <id> prod </id> <properties> <filter-resource-name> prod </filter-resource-name> </premerties> </profile> </files>ฉันเชื่อว่าทุกคนคุ้นเคยกับย่อหน้านี้ดังนั้นฉันจะไม่อธิบายมาก (ถ้าคุณมีคำถามใด ๆ โปรดส่งข้อความส่วนตัวถึงฉัน)
2. จากนั้นเปิดไฟล์ Application.properties และเพิ่มเนื้อหาต่อไปนี้ลงไป:
# ระบุไฟล์กำหนดค่าที่เปิดใช้งาน (dev | prod)
Spring.profiles.active =@filter-resource-name@
โครงการทั้งหมดได้กลายเป็นโครงสร้างต่อไปนี้:
เมื่อมาถึงจุดนี้การกำหนดค่าสปริงโบทหลายสภาพแวดล้อมของเราเสร็จสมบูรณ์แล้ว
3. ตั้งค่าระดับบันทึก
#log levellogging.level.root = debug
4. ตั้งพอร์ตที่กำหนดเองและชื่ออินสแตนซ์
#port server.port = 8888#ชื่ออินสแตนซ์ Spring.application.name = demo-springboot
5. logback-spring.xml
<? xml version = "1.0" encoding = "utf-8"?> <การกำหนดค่า> <include resource = "org/springframework/boot/logging/logback/base.xml"/> <appender name = "demo"> <file> demo/demo.log </ไฟล์> <rollingpolicy> <! <filenamePattern> demo/demo.%d {yyyy-mm-dd} .log </filenamepattern> <!-ประวัติที่ใหญ่ที่สุดของบันทึก-> <maxhistory> 10 </axhistory> </rollingPolicy> </appender> <logger name = "com.example.demo" level = "info" เพิ่มเติม = "false"> <appender-ref ref = "demo"/> </logger> <logger name = "com.example.demo.dao" leveler = "debug"/> name = "druid.sql.statement" level = "debug" /> <logger name = "druid.sql.statement" level = "debug" /> <logger name = "com.example.demo.service" level = "info" /> name = "druid.sql.resultset" level = "debug" /> <logger name = "org.apache" level = "info" /> <logger name = "org.mybatis.spring" level = "ข้อผิดพลาด" /> <logger name = "org.springframework" <root level = "info"> <appender-ref ref = "demo"/> </root> </การกำหนดค่า ณ จุดนี้การกำหนดค่าสภาพแวดล้อมพื้นฐานของโครงการของเราได้ถูกสร้างขึ้น เลือก Dev | ทดสอบ | Prod เพื่อป้อนการกำหนดค่าที่คุณระบุโดย Maven Clean Install แล้วเรียกใช้แอปพลิเคชัน หากคุณสามารถเข้าถึง LocalHost: 8888 คุณสามารถเข้าถึงและระบุการกำหนดค่าของคุณ แต่นี่ยังห่างไกลพอ การพัฒนาโครงการของเราจะต้องดำเนินการฐานข้อมูล ฮ่าฮ่าใช่มาสู่โลกของ Springboot + MySQL + MyBatis!
สรุป
ด้านบนเป็นบทช่วยสอนการกำหนดค่าของ Springboot Multi-Invironment ที่แนะนำโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!