1. บทนำ
ตั้งแต่เปิดตัว Spring Boot เป็นผู้มีบทบาทสำคัญในระบบนิเวศของ Spring โครงการนี้ทำให้ชีวิตของเราง่ายขึ้นมากด้วยความสามารถในการกำหนดค่าอัตโนมัติ
ในบทช่วยสอนนี้เราจะพูดถึงคำถามที่พบบ่อยที่สุดเกี่ยวกับ Spring Boot ที่อาจเกิดขึ้นระหว่างการสัมภาษณ์งาน
2. คำถาม
คำถามที่ 1 Spring Boot คืออะไรและมีคุณสมบัติหลักอะไรบ้าง?
Spring Boot เป็นกรอบการทำงานสำหรับการพัฒนาแอปพลิเคชันอย่างรวดเร็วที่สร้างขึ้นจาก Spring Framework ด้วยการกำหนดค่าอัตโนมัติและการสนับสนุนแอ็พพลิเคชันเซิร์ฟเวอร์แบบฝังรวมกับเอกสารประกอบที่กว้างขวางและการสนับสนุนจากชุมชนทำให้ Spring Boot เป็นหนึ่งในเทคโนโลยีที่ได้รับความนิยมมากที่สุดในระบบนิเวศของ Java ในปัจจุบัน
นี่คือคุณสมบัติเด่นบางประการ:
- Starters - ชุดของตัวบ่งชี้การอ้างอิงเพื่อรวมการอ้างอิงที่เกี่ยวข้องในขณะเดินทาง
- การกำหนดค่าอัตโนมัติ - วิธีกำหนดค่าแอปพลิเคชันโดยอัตโนมัติตามการอ้างอิงที่มีอยู่ใน classpath
- ตัวกระตุ้น - เพื่อรับคุณสมบัติที่พร้อมใช้งานจริงเช่นการตรวจสอบ
- ความปลอดภัย
- การบันทึก
คำถามที่ 2 อะไรคือความแตกต่างระหว่าง Spring และ Spring Boot?
Spring Framework มีคุณสมบัติมากมายที่ทำให้การพัฒนาเว็บแอปพลิเคชันง่ายขึ้น คุณลักษณะเหล่านี้รวมถึงการแทรกข้อมูลอ้างอิงการผูกข้อมูลการเขียนโปรแกรมเชิงลักษณะการเข้าถึงข้อมูลและอื่น ๆ อีกมากมาย
ในช่วงหลายปีที่ผ่านมา Spring มีความซับซ้อนมากขึ้นเรื่อย ๆ และจำนวนการกำหนดค่าแอปพลิเคชันดังกล่าวอาจเป็นเรื่องที่น่าวิตก นี่คือจุดที่ Spring Boot มีประโยชน์ - ทำให้การกำหนดค่าแอปพลิเคชัน Spring เป็นเรื่องง่าย
โดยพื้นฐานแล้วในขณะที่ Spring ยังไม่ได้เปิดใช้งานSpring Boot จะพิจารณามุมมองของแพลตฟอร์มและไลบรารีเพื่อให้เราเริ่มต้นได้อย่างรวดเร็ว
ประโยชน์ที่สำคัญที่สุดสองประการที่ Spring Boot นำมาให้:
- กำหนดค่าแอปพลิเคชันอัตโนมัติตามสิ่งประดิษฐ์ที่พบใน classpath
- ให้คุณสมบัติที่ไม่สามารถใช้งานได้ทั่วไปสำหรับแอปพลิเคชันในการผลิตเช่นความปลอดภัยหรือการตรวจสอบความสมบูรณ์
โปรดตรวจสอบบทช่วยสอนอื่น ๆ ของเราเพื่อดูการเปรียบเทียบโดยละเอียดระหว่าง vanilla Spring และ Spring Boot
ไตรมาสที่ 3 เราจะตั้งค่า Spring Boot Application ด้วย Maven ได้อย่างไร?
เราสามารถรวม Spring Boot ไว้ในโปรเจ็กต์ Maven เหมือนกับที่เราทำกับไลบรารีอื่น ๆ อย่างไรก็ตามวิธีที่ดีที่สุดคือการสืบทอดจากโปรเจ็กต์spring-boot-starter-parentและประกาศการอ้างอิงกับ Spring Boot starters การทำเช่นนี้ทำให้โครงการของเราสามารถใช้การตั้งค่าเริ่มต้นของ Spring Boot ได้
การสืบทอดโปรเจ็กต์spring-boot-starter-parentนั้นตรงไปตรงมา - เราจำเป็นต้องระบุองค์ประกอบหลักในpom.xml เท่านั้น :
org.springframework.boot spring-boot-starter-parent 2.3.0.RELEASE
เราสามารถค้นหารุ่นล่าสุดของspring-boot-starter-parent ได้ที่ Maven Central
การใช้โปรเจ็กต์หลักเริ่มต้นทำได้สะดวก แต่ไม่สามารถทำได้เสมอไป ตัวอย่างเช่นหาก บริษัท ของเรากำหนดให้โครงการทั้งหมดรับช่วงจาก POM มาตรฐานเรายังคงได้รับประโยชน์จากการจัดการการพึ่งพาของ Spring Boot โดยใช้พาเรนต์ที่กำหนดเอง
Q4. Spring Initializr คืออะไร?
Spring Initializr เป็นวิธีที่สะดวกในการสร้างโครงการ Spring Boot
เราสามารถไปที่ไซต์ Spring Initializr เลือกเครื่องมือจัดการการพึ่งพา (ไม่ว่าจะเป็น Maven หรือ Gradle) ภาษา (Java, Kotlin หรือ Groovy) รูปแบบบรรจุภัณฑ์ (Jar หรือ War) เวอร์ชันและการอ้างอิงและดาวน์โหลดโครงการ
สิ่งนี้สร้างโครงการโครงกระดูกสำหรับเราและประหยัดเวลาในการตั้งค่าเพื่อให้เราสามารถมุ่งเน้นไปที่การเพิ่มตรรกะทางธุรกิจได้
แม้ว่าเราจะใช้ตัวช่วยสร้างโปรเจ็กต์ใหม่ของ IDE (เช่น STS หรือ Eclipse with STS) เพื่อสร้างโปรเจ็กต์ Spring Boot แต่ก็ใช้ Spring Initializr ภายใต้ประทุน
คำถามที่ 5 Spring Boot Starters มีอะไรบ้าง?
ผู้เริ่มต้นแต่ละคนมีบทบาทเป็นร้านค้าครบวงจรสำหรับเทคโนโลยี Spring ทั้งหมดที่เราต้องการ จากนั้นการอ้างอิงที่จำเป็นอื่น ๆ จะถูกดึงเข้ามาและจัดการในลักษณะที่สอดคล้องกัน
เริ่มทั้งหมดอยู่ภายใต้org.springframework.bootกลุ่มและชื่อของพวกเขาเริ่มต้นด้วยฤดูใบไม้ผลิบูต starter- รูปแบบการตั้งชื่อนี้ช่วยให้ค้นหาตัวเริ่มต้นได้ง่ายโดยเฉพาะอย่างยิ่งเมื่อทำงานกับ IDE ที่รองรับการอ้างอิงการค้นหาตามชื่อ
ในขณะที่เขียนนี้มีผู้เริ่มใช้งานมากกว่า 50 รายการ ที่นิยมใช้ ได้แก่
- spring-boot-starter: core starter รวมถึงการรองรับการกำหนดค่าอัตโนมัติการบันทึกและ YAML
- spring-boot-starter-aop:สตาร์ทเตอร์สำหรับการเขียนโปรแกรมเชิงมุมมองด้วย Spring AOP และ AspectJ
- spring-boot-starter-data-jpa: ตัวเริ่มต้นสำหรับการใช้ Spring Data JPA กับ Hibernate
- spring-boot-starter-security:สตาร์ทเตอร์สำหรับการใช้ Spring Security
- spring-boot-starter-test:สตาร์ทเตอร์สำหรับทดสอบแอพพลิเคชั่น Spring Boot
- spring-boot-starter-web: ตัวเริ่มต้นสำหรับการสร้างเว็บรวมถึง RESTful แอปพลิเคชันที่ใช้ Spring MVC
สำหรับรายการเริ่มต้นทั้งหมดโปรดดูที่เก็บนี้
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับ Spring Boot starters โปรดดูที่ Intro to Spring Boot Starters
คำถาม 6 จะปิดการใช้งานการกำหนดค่าอัตโนมัติเฉพาะได้อย่างไร?
If we want to disable a specific auto-configuration, we can indicate it using the exclude attribute of the @EnableAutoConfiguration annotation. For instance, this code snippet neutralizes DataSourceAutoConfiguration:
// other annotations @EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) public class MyConfiguration { }
If we enabled auto-configuration with the @SpringBootApplication annotation — which has @EnableAutoConfiguration as a meta-annotation — we could disable auto-configuration with an attribute of the same name:
// other annotations @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class MyConfiguration { }
We can also disable an auto-configuration with the spring.autoconfigure.exclude environment property. This setting in the application.properties file does the same thing as before:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Q7. How to Register a Custom Auto-Configuration?
To register an auto-configuration class, we must have its fully-qualified name listed under the EnableAutoConfiguration key in the META-INF/spring.factories file:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration
If we build a project with Maven, that file should be placed in the resources/META-INF directory, which will end up in the mentioned location during the package phase.
Q8. How to Tell an Auto-Configuration to Back Away When a Bean Exists?
To instruct an auto-configuration class to back off when a bean is already existent, we can use the @ConditionalOnMissingBean annotation. The most noticeable attributes of this annotation are:
- value: The types of beans to be checked
- name: The names of beans to be checked
When placed on a method adorned with @Bean, the target type defaults to the method's return type:
@Configuration public class CustomConfiguration { @Bean @ConditionalOnMissingBean public CustomService service() { ... } }
Q9. How to Deploy Spring Boot Web Applications as Jar and War Files?
Traditionally, we package a web application as a WAR file, then deploy it into an external server. Doing this allows us to arrange multiple applications on the same server. During the time that CPU and memory were scarce, this was a great way to save resources.
However, things have changed. Computer hardware is fairly cheap now, and the attention has turned to server configuration. A small mistake in configuring the server during deployment may lead to catastrophic consequences.
Spring tackles this problem by providing a plugin, namely spring-boot-maven-plugin, to package a web application as an executable JAR. To include this plugin, just add a plugin element to pom.xml:
org.springframework.boot spring-boot-maven-plugin
With this plugin in place, we'll get a fat JAR after executing the package phase. This JAR contains all the necessary dependencies, including an embedded server. Thus, we no longer need to worry about configuring an external server.
We can then run the application just like we would an ordinary executable JAR.
Notice that the packaging element in the pom.xml file must be set to jar to build a JAR file:
jar
If we don't include this element, it also defaults to jar.
In case we want to build a WAR file, change the packaging element to war:
war
And leave the container dependency off the packaged file:
org.springframework.boot spring-boot-starter-tomcat provided
After executing the Maven package phase, we'll have a deployable WAR file.
Q10. How to Use Spring Boot for Command Line Applications?
Just like any other Java program, a Spring Boot command line application must have a main method. This method serves as an entry point, which invokes the SpringApplication#run method to bootstrap the application:
@SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class); // other statements } }
The SpringApplication class then fires up a Spring container and auto-configures beans.
Notice we must pass a configuration class to the run method to work as the primary configuration source. By convention, this argument is the entry class itself.
After calling the run method, we can execute other statements as in a regular program.
Q11. What Are Possible Sources of External Configuration?
Spring Boot provides support for external configuration, allowing us to run the same application in various environments. We can use properties files, YAML files, environment variables, system properties, and command-line option arguments to specify configuration properties.
We can then gain access to those properties using the @Value annotation, a bound object via the @ConfigurationProperties annotation, or the Environment abstraction.
Q12. What Does it Mean that Spring Boot Supports Relaxed Binding?
Relaxed binding in Spring Boot is applicable to the type-safe binding of configuration properties.
With relaxed binding, the key of a property doesn't need to be an exact match of a property name. Such an environment property can be written in camelCase, kebab-case, snake_case, or in uppercase with words separated by underscores.
For example, if a property in a bean class with the @ConfigurationProperties annotation is named myProp, it can be bound to any of these environment properties: myProp, my-prop, my_prop, or MY_PROP.
Q13. What is Spring Boot Devtools Used For?
Spring Boot Developer Tools, or DevTools, is a set of tools making the development process easier. To include these development-time features, we just need to add a dependency to the pom.xml file:
org.springframework.boot spring-boot-devtools
The spring-boot-devtools module is automatically disabled if the application runs in production. The repackaging of archives also excludes this module by default. Hence, it won't bring any overhead to our final product.
By default, DevTools applies properties suitable to a development environment. These properties disable template caching, enable debug logging for the web group, and so on. As a result, we have this sensible development-time configuration without setting any properties.
Applications using DevTools restart whenever a file on the classpath changes. This is a very helpful feature in development, as it gives quick feedback for modifications.
By default, static resources, including view templates, don't set off a restart. Instead, a resource change triggers a browser refresh. Notice this can only happen if the LiveReload extension is installed in the browser to interact with the embedded LiveReload server that DevTools contains.
For further information on this topic, please see Overview of Spring Boot DevTools.
Q14. How to Write Integration Tests?
When running integration tests for a Spring application, we must have an ApplicationContext.
To make our life easier, Spring Boot provides a special annotation for testing – @SpringBootTest. This annotation creates an ApplicationContext from configuration classes indicated by its classes attribute.
In case the classes attribute isn't set, Spring Boot searches for the primary configuration class. The search starts from the package containing the test up until it finds a class annotated with @SpringBootApplication or @SpringBootConfiguration.
For detailed instructions, check out our tutorial on testing in Spring Boot.
Q15. What Is Spring Boot Actuator Used For?
Essentially, Actuator brings Spring Boot applications to life by enabling production-ready features. These features allow us to monitor and manage applications when they're running in production.
Integrating Spring Boot Actuator into a project is very simple. All we need to do is to include the spring-boot-starter-actuator starter in the pom.xml file:
org.springframework.boot spring-boot-starter-actuator
Spring Boot Actuator can expose operational information using either HTTP or JMX endpoints. Most applications go for HTTP, though, where the identity of an endpoint and the /actuator prefix form a URL path.
Here are some of the most common built-in endpoints Actuator provides:
- env: Exposes environment properties
- health: Shows application health information
- httptrace: Displays HTTP trace information
- info: Displays arbitrary application information
- metrics: Shows metrics information
- loggers: Shows and modifies the configuration of loggers in the application
- mappings: Displays a list of all @RequestMapping paths
Please refer to our Spring Boot Actuator tutorial for a detailed rundown.
Q16. Which Is a Better Way to Configure a Spring Boot Project – Using Properties or YAML?
YAML offers many advantages over properties files, such as:
- More clarity and better readability
- Perfect for hierarchical configuration data, which is also represented in a better, more readable format
- Support for maps, lists, and scalar types
- Can include several profiles in the same file
However, writing it can be a little difficult and error-prone due to its indentation rules.
For details and working samples, please refer to our Spring YAML vs Properties tutorial.
Q17. What Are the Basic Annotations that Spring Boot Offers?
The primary annotations that Spring Boot offers reside in its org.springframework.boot.autoconfigure and its sub-packages. Here are a couple of basic ones:
- @EnableAutoConfiguration – to make Spring Boot look for auto-configuration beans on its classpath and automatically apply them.
- @SpringBootApplication – used to denote the main class of a Boot Application. This annotation combines @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.
Spring Boot Annotations offers more insight into the subject.
Q18. How Can You Change the Default Port in Spring Boot?
We can change the default port of a server embedded in Spring Boot using one of these ways:
- using a properties file – we can define this in an application.properties (or application.yml) file using the property server.port
- programmatically – in our main @SpringBootApplication class, we can set the server.port on the SpringApplication instance
- using the command line – when running the application as a jar file, we can set the server.port as a java command argument:
java -jar -Dserver.port=8081 myspringproject.jar
Q19. Which Embedded Servers does Spring Boot Support, and How to Change the Default?
As of date, Spring MVC supports Tomcat, Jetty, and Undertow. Tomcat is the default application server supported by Spring Boot's web starter.
Spring WebFlux supports Reactor Netty, Tomcat, Jetty, and Undertow with Reactor Netty as default.
In Spring MVC, to change the default, let's say to Jetty, we need to exclude Tomcat and include Jetty in the dependencies:
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty
Similarly, to change the default in WebFlux to UnderTow, we need to exclude Reactor Netty and include UnderTow in the dependencies.
“Comparing embedded servlet contains in Spring Boot” contains more details on the different embedded servers we can use with Spring MVC.
Q20. Why Do We Need Spring Profiles?
When developing applications for the enterprise, we typically deal with multiple environments such as Dev, QA, and Prod. The configuration properties for these environments are different.
For example, we might be using an embedded H2 database for Dev, but Prod could have the proprietary Oracle or DB2. Even if the DBMS is the same across environments, the URLs would definitely be different.
นี้เพื่อให้ง่ายและสะอาดฤดูใบไม้ผลิมีบทบัญญัติของโปรไฟล์เพื่อช่วยให้แยกการกำหนดค่าสำหรับแต่ละสภาพแวดล้อม ดังนั้นแทนที่จะรักษาสิ่งนี้โดยใช้โปรแกรมคุณสมบัติสามารถเก็บไว้ในไฟล์แยกต่างหากเช่นapplication-dev คุณสมบัติและแอปพลิเคชัน prod คุณสมบัติ . application.propertieเริ่มต้นชี้ไปที่โปรไฟล์ที่ใช้งานอยู่ในปัจจุบันโดยใช้สปริง โปรไฟล์ แอ็คทีฟเพื่อให้เลือกการกำหนดค่าที่ถูกต้อง
Spring Profiles ให้มุมมองที่ครอบคลุมเกี่ยวกับหัวข้อนี้
3. สรุป
บทช่วยสอนนี้กล่าวถึงคำถามที่สำคัญที่สุดเกี่ยวกับ Spring Boot ที่คุณอาจพบในระหว่างการสัมภาษณ์ทางเทคนิค เราหวังว่าพวกเขาจะช่วยให้คุณได้งานในฝัน