The so-called work wants to do well , You must sharpen your tools first , do java The same goes for development , In the comparison of the most popular java IDE（eclipse,myeclipse、IDEA） after , I made a decision to IDEA. Need to know , The level of code writing can be a piece of shit , But development tools must use the best , It's like a person with poor martial arts , If you don't have a decent weapon in your hand , There's no way to get around , I was chopped to death in minutes . I used to use eclipse,myeclipse When , Because it's used in the past VS The gap is too big , It made me want to give up writing for a time java Code , until IDEA Appearance , Like a light in the dark . I'm an efficient person , Could have been 2s The code of the end of the bell , You have to spend 10s, I can't stand it , Isn't it a waste of life ？ For example , People of the same level fight , A bare hand , One with a machete , Who has a good chance of winning ？ Unless you're strong enough , Strong enough not to be stagnant , Plants, trees, bamboo and stone can hurt people . So the kind of people who use Notepad to write code , Or enough cattle , Or it's invisible and forced to die . Good and false in things , There is a big difference between the old and the new , The old bird will make good use of all kinds of useful auxiliary development tools . What do you do with these tools ？ Improve development efficiency ！ Produce = Time * efficiency . Time is all about 8h, No matter how much you work overtime 24h, And efficiency is often beyond 4 Times , That means maybe you 24h You can't do anything else 8h, Because the efficiency is different .
srping boot The emergence of the may be able to java Developers from 996 Free from the curse in , So you can work less overtime . use Spring Boot Development is essentially a routine Spring Project development , It just takes advantage of Spring Boot Startup programs and automatic configuration simplify the development process , Improve development efficiency .Spring Boot Project development code implementation is still using Spring mvc+spring+mybatis etc. , Of course, it can integrate almost all open source projects .
PS： If you encounter any problems during the configuration process , You can leave a message below or check my personal announcement , Answer online anytime , And some of the things I've been working on these days Java Learning manual , Interview questions , development tool ,PDF Document book tutorial , If you need it, you can share it for free . Get down to business ：
java Environment configuration
download java JDK, Suggest 1.8 Above version .
Here's what I downloaded ：jdk_8.0.1310.11_64.exe
Then install , My installation directory here is C:\Program Files\Java\jdk1.8.0_131, It will be used later .
Configure environment variables
New system variable JAVA_HOME, value ：C:\Program Files\Java\jdk1.8.0_131
Edit environment variables Path, Add... To the front of the variable value %JAVA_HOME%\bin;, Pay attention to the last one ; Separate .
IDEA Install and crack
Now the latest version is IntelliJ IDEA 2018.2 了 , What I downloaded before is IntelliJ IDEA 2018.1.4 x64, Because mine is win10 64bit System .
Once the download is complete , Click next. Next is OK , But at the last step, be sure to pay attention to , Otherwise, the attack may fail , That's when the finish screen finally appears , Be sure to remove the check in front of whether to run immediately .
（1） Edit on the computer C:\Windows\System32\drivers\etc Under the hosts file
Add the following line of code ：
Save and close .
（2） open http://idea.lanyus.com/, Get the registration code
IDEA To configure
IDEA After installation , We're going to personalize it , I think the most important two points are , One is the configuration of shortcut keys , One is the configuration of code display style , Like I'm used to VS Code display style of , Then we can customize it .
I made a configuration file , Download address ：https://files.cnblogs.com/files/jiekzou/settings.zip
Once the download is complete , After decompressing it is a settings.jar file , And then in IDEA Import the configuration in .
The final display is as follows ：
About IDEA Code format shortcut key for , The default is ：Ctrl+Alt+L, This conflicts with many software lock screen shortcuts , So I suggest that it be modified , I've changed it here to and VS Code formatting shortcuts are the same ：Ctrl+K,Ctrl+F. This is a combination of two shortcut keys .
stay VS The case is insensitive to code hints in , and IDEA Sensitive by default , Because of personal habits , So I need to put IDEA The code smart prompt is also changed to case insensitive .
stay IDEA Code written in or copied into , Its namespace is what packages automatically introduce , If more than one bag appears , Then you need to import it manually , It's also very simple to introduce , Click to the code location with the mouse , then Atl+Enter, Select the specified package . although java There is no attribute in the legend ,C# In fact, the medium attribute is like java In the same way through a get Methods and a set Method , It's just C# It was packaged , and java Need to write by yourself , however IDEA There are shortcut keys to automatically generate field properties in ：Alt+Insert.
No matter what kind of IDE, It is necessary to remember all kinds of shortcut keys , Why? ？ efficiency ！ efficiency ！ Or efficiency ！
first spring boot web Program
what's spring boot？
My understanding is that ：spring boot=spring+spring mvc+ a pile java Technology stack related framework + The idea that convention is greater than configuration .
spring boot It's not a new technology , It's like ajax equally , It's a combination of existing technologies . The appearance of it , Will greatly improve java Application development efficiency . meanwhile , The appearance of it , Completely overturn the past. I'm very interested in java The understanding of （ In addition to the configuration ,TMD Or configuration ）. Lightweight 、 Pluggable 、 Microservices .
About https://start.spring.io/, Let's open it up and see what this is
This is not exactly a build project jar Package interface , Generate directly in this interface jar package , And then use eclipse Opening is the same .
Be careful ： Create... In this way spring boot project , It's connected to the Internet , Download some from the website jar package . If you want to create a project offline , You can choose Maven.
Because it is web project , So we choose web, About spring boot edition , The latest version is 2.1.0, I use its last stable version here 1.5.14. We see some versions with SNAPSHOT word , It shows that it's a flash version .
Keep clicking next , The final project results are listed below ：
So let's see pom.xml, It is maven To manage all kinds of jar A configuration file that the package depends on ,maven amount to .net Medium nuget, It's a package management tool .
We see <parent> The version number is declared in the node , And it's as a parent node , This means that its child node can inherit the version dependency of the parent node . Which are its child nodes , Namely <dependencies> below groupId and <parent> in groupId Consistent dependence . This pom.xml Documents in , It's the product of our previous visual configuration , Some of the information in it is what we filled in before . Then follow up , When we need to expand , Just modify the configuration file directly .
About pom.xml Detailed explanation , Please refer to ：pom.xml Detailed explanation
If you are in the pom.xml When you add code to , There's no smart tip , So you need to update .
Next , Let's take a look at FirstdemoApplication This class
We see a @SpringBootApplication annotation , Let's look at its source code as follows ：
You can see it in the source code , Actually @SpringBootApplication = ( The default attribute )@Configuration + @EnableAutoConfiguration + @ComponentScan.
Let's take a look at what these three annotations mean in turn .
@Configuration Is a class level comment , Indicates that the object is a bean Source of definition .@Configuration Class passing @bean Public method statements for annotations bean. @Configuration The annotation class of indicates that this class can be used Spring.
@Bean Annotations are used to represent a method instantiation , Configuration and initialization are done by Spring IoC A new object for container management .
@Configuration Generally speaking, it is related to @Bean Use with annotations , use @Configuration Annotation class is equivalent to XML Middle configuration beans, use @Bean Annotation method is equivalent to XML Middle configuration bean.
@EnableAutoConfiguration The package in which the annotated class is located has a specific meaning , And use it as the default configuration .
@ComponentScan The annotation will automatically scan all marked with @Component annotation Class , And register into bean, Including, of course @Component Next sub note @Service,@Repository,@Controller.
This is the configuration file for the application , Follow .net In the project web.config almost . But I'm more used to using application.yml This file replaces application.properties
modify application.properties, Name it application.properties-bak, In fact, it is to disable the configuration file , But I don't want to delete it directly .
Then add a file application.yml, Pay attention to and application.properties Under the same level of directory .
Add configuration item ：
I'm going to change it here tomcat The boot port of the server is 8082, because IDEA srping boot The project comes with tomcat, So we don't need to configure external tomcat.
Create a new controller TestController, It's the naming convention for this controller and .net mvc The standards in China are consistent , All are Controller ending . The code is as follows ：
@RequestMapping("/hello") and @RequestMapping(value = "/hello" It is equivalent. .
@RequestMapping(value = "/hello",method =RequestMethod.GET) and @GetMapping("/hello") It is equivalent. .
@RestController Annotation equivalent @ResponseBody ＋ @Controller.
If necessary, return to the specified page , You need to use @Controller Coordinates with the view parser InternalResourceViewResolver Talent .
If you need to go back JSON,XML Or custom mediaType Content to page , You need to add to the corresponding method @ResponseBody annotation .
Be careful ：@RestController annotation , amount to @Controller+@ResponseBody The combination of two notes , return json Data does not need to be preceded by a method @ResponseBody Note the , But use @RestController This annotation , Can't go back to jsp,html page , The view parser cannot parse jsp,html page
Run the program ：
before this , If we change the code every time , You have to restart the server , And rerun the code , So with the hot deployment , Changed the code , All we need to do is IDEA Click on Build, You can see the effect directly , No need to restart the server .
pom.xml Add the following dependencies to the file ：
After adding dependencies , stay IDEA The following prompt box will pop up in the lower right corner of
Click on “Import Changes" Will download automatically j The dependent jar package .
If you don't want to go manually after each modification Build, Can be in IDEA Configure automatic Build
Then press the key combination ：Shift+ALT+Ctrl+/ , choice “Registry”, enter , find “complier.automake.allow.when.app.running”, Click the tick to .
advantage ： Simple , Support Spring-boot project , Support member level modification and hot deployment .
shortcoming ： Only support spring-boot project .
Read configuration file
Modify the configuration file application.yml, Add the following code ：
New profile class WebSiteConfig, The field name is consistent with the name in the configuration item .
In the controller class TestController Modify the code in ：
Does it feel like using vs Development asp.net mvc The project is as simple as that ？ Even better than .net It's convenient to operate the configuration file .