What is spring boot
Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses an ad hoc approach to configuration, eliminating the need for developers to define boilerplate configurations. To understand it in my words, spring boot is actually not a new framework. It configures the use of many frameworks by default, just like maven integrates all jar packages, and spring boot integrates all frameworks (I don’t know if this metaphor is appropriate. ).
What are the benefits of using spring boot
In fact, it is simple, fast and convenient! What should we do if we need to build a spring web project?
1) Configure web.xml, load spring and spring mvc
2) Configure database connection, configure spring transactions
3) Configure reading of loading configuration files, enable annotations
4) Configure log files
.. .
After the configuration is completed, deploy tomcat for debugging
...
Microservices are very popular now. If my project just needs to send an email, if my project just needs to produce a point; I need to do this all over again!
But what if you use spring boot?
It’s very simple. I only need a few configurations to quickly and easily set up a web project or build a microservice!
Quick Start
Having said so much, my hands are itchy, let’s give it a try right away!
maven build project
1. Visit http://start.spring.io/
2 , select the build tool Maven Project, Spring Boot version 1.3.6 and some basic project information, click "Switch to the full version." Select 1.7 for the java version, as shown in the figure below:
3. Click Generate Project Download the project compressed package
4. After unzipping, use eclipse, Import -> Existing Maven Projects -> Next -> Select the unzipped folder -> Finsh, OK done!
Project structure introduction
As shown in the picture above, the basic structure of Spring Boot has three files:
l src/main/java program development and main program entry
l src/main/resources configuration file
l src/test/ java test program
In addition, the directory results recommended by spingboot are as follows:
root package structure: com.example.myproject
com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- controller | +- CustomerController.java |
1. Application.java is recommended to be placed under the following directory, mainly used for some framework configuration
2. The domain directory is mainly used for the entity (Entity) and data access layer (Repository)
3. The service layer is mainly business code
4. The controller is responsible for page access control
Using the default configuration can save a lot Configuration, of course, you can also change it according to your own preferences
Finally, start the Application main method, and now a java project is set up!
Introducing the web module
1. Add web-supporting modules to pom. , logs and YAML;
spring-boot-starter-test: test modules, including JUnit, Hamcrest, Mockito.
2. Write controller content
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
@RestController means that all methods in the controller are output in json format, and there is no need to write any jackjson configuration!
3. Start the main program, open the browser and visit http://localhost:8080/hello, and you can see the effect. It’s very simple!
How to do unit testing
打開(kāi)的src/test/下的測(cè)試入口,編寫簡(jiǎn)單的http請(qǐng)求來(lái)測(cè)試;使用mockmvc進(jìn)行,利用MockMvcResultHandlers.print()打印出執(zhí)行結(jié)果。
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MockServletContext.class) @WebAppConfiguration public class HelloWorldControlerTests { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
開(kāi)發(fā)環(huán)境的調(diào)試
熱啟動(dòng)在正常開(kāi)發(fā)項(xiàng)目中已經(jīng)很常見(jiàn)了吧,雖然平時(shí)開(kāi)發(fā)web項(xiàng)目過(guò)程中,改動(dòng)項(xiàng)目啟重啟總是報(bào)錯(cuò);但springBoot對(duì)調(diào)試支持很好,修改之后可以實(shí)時(shí)生效,需要添加以下的配置:
????
????????
????????
????????
? ?
????
????????
????????????
????????????
????????????
????????????????
????????????
????????
? ?
該模塊在完整的打包環(huán)境下運(yùn)行的時(shí)候會(huì)被禁用。如果你使用java -jar啟動(dòng)應(yīng)用或者用一個(gè)特定的classloader啟動(dòng),它會(huì)認(rèn)為這是一個(gè)“生產(chǎn)環(huán)境”。
?
總結(jié)
使用spring boot可以非常方便、快速搭建項(xiàng)目,使我們不用關(guān)心框架之間的兼容性,適用版本等各種問(wèn)題,我們想使用任何東西,僅僅添加一個(gè)配置就可以,所以使用sping boot非常適合構(gòu)建微服務(wù)。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

With the advent of the big data era, more and more companies are beginning to understand and recognize the value of big data and apply it to business. The problem that comes with it is how to handle this large flow of data. In this case, big data processing applications have become something that every enterprise must consider. For developers, how to use SpringBoot to build an efficient big data processing application is also a very important issue. SpringBoot is a very popular Java framework that allows

As technology continues to evolve, we can now use different technologies to build desktop applications. SpringBoot and JavaFX are one of the more popular choices now. This article will focus on how to use these two frameworks to build a feature-rich desktop application. 1. Introduction to SpringBoot and JavaFXSpringBoot is a rapid development framework based on the Spring framework. It helps developers quickly build web applications while providing a set of

In actual projects, we try to avoid distributed transactions. However, sometimes it is really necessary to do some service splitting, which will lead to distributed transaction problems. At the same time, distributed transactions are also asked in the market during interviews. You can practice with this case, and you can talk about 123 in the interview.

With the rise of digital currencies such as Bitcoin, blockchain technology has gradually become a hot topic. Smart contracts can be regarded as an important part of blockchain technology. SpringBoot, as a popular Java back-end development framework, can also be used to build blockchain applications and smart contracts. This article will introduce how to use SpringBoot to build applications and smart contracts based on blockchain technology. 1. SpringBoot and blockchain First, we need to understand some basic concepts related to blockchain. Blockchain

In modern web application development, WebSocket is a common technology for instant communication and real-time data transfer. The SpringBoot framework provides support for integrated WebSocket, making it very convenient for developers to implement push and notification functions. This article will introduce how to use WebSocket to implement push and notification functions in SpringBoot, and demonstrate the implementation of a simple real-time online chat room. Create a SpringBoot project First, we need to create a

As modern businesses rely more and more on a variety of disparate applications and systems, enterprise integration becomes even more important. Enterprise Service Bus (ESB) is an integration architecture model that connects different systems and applications together to provide common data exchange and message routing services to achieve enterprise-level application integration. Using SpringBoot and ApacheServiceMix, we can easily build an ESB system. This article will introduce how to implement it. SpringBoot and A

SpringBoot is a very popular Java development framework. It not only has the advantage of rapid development, but also has many built-in practical functions. Among them, task scheduling and scheduled tasks are one of its commonly used functions. This article will explore SpringBoot's task scheduling and timing task implementation methods. 1. Introduction to SpringBoot task scheduling SpringBoot task scheduling (TaskScheduling) refers to executing some special tasks at a specific point in time or under certain conditions.

With the development of globalization, more and more websites and applications need to provide multi-language support and internationalization functions. For developers, implementing these functions is not an easy task because it requires consideration of many aspects, such as language translation, date, time and currency formats, etc. However, using the SpringBoot framework, we can easily implement multi-language support and international applications. First, let us understand the LocaleResolver interface provided by SpringBoot. Loc
