从.Net到Java学习第二篇——IDEA and start spring boot

  从.Net 到 Java 学习第一篇——开篇

  所谓工欲善其事,必先利其器,做 java 开发也一样,在比较了目前最流行的几个 java IDE(eclipse,myeclipse、IDEA)之后,我果断选择 IDEA。要知道,写代码的水平可以是一坨屎,但是开发工具一定要用最好的,就像一个人武功很差,又不拿一把像样的兵器在手的话,根本就没法闯荡江湖,分分钟被人砍死了。之前用 eclipse,myeclipse 的时候,由于和过去用 VS 的落差太大,弄得我一度想放弃写 java 代码,直到 IDEA 的出现,犹如黑暗中的一盏明灯。我是一个效率至上的人,原本可以 2s 钟撸完的代码,你偏偏要花 10s,我就受不了,这不是浪费生命吗?打个比方,同样水平的人搏斗,一个赤手空拳,一个拿把砍刀,谁的胜算大?除非你内功足够强大,强大到不滞于物,草木竹石皆可伤人。所以那种拿记事本撸代码的人,要么就是足够牛,要么就是无形装逼被致命。善假于物也,老鸟和菜鸟很大一个区别就是,老鸟会善用各种好用的辅助开发工具。用这些工具是干嘛的?提升开发效率!产出 = 时间 * 效率。时间大家都是 8h,你再怎么加班最多 24h,而效率往往是可以超越 4 倍的,也就是说可能你 24h 也干不过别人 8h,因为效率不一样。

  srping boot 的出现或许可以将 java 开发人员从 996 中的魔咒中解脱出来,从而少加班。采用 Spring Boot 开发实质上也是一个常规的 Spring 项目开发,只是利用了 Spring Boot 启动程序和自动配置简化开发过程,提高开发效率。Spring Boot 项目开发代码的实现依然是使用 Spring mvc+spring+mybatis 等,当然能集成几乎所有的开源项目。

java 环境配置

下载 java JDK,建议 1.8 以上版本。

我这里下载的是:jdk_8.0.1310.11_64.exe

然后进行安装,我这里的安装目录是 C:\Program Files\Java\jdk1.8.0_131,后面会用的。

配置环境变量

新建系统变量 JAVA_HOME,值:C:\Program Files\Java\jdk1.8.0_131

编辑环境变量 Path,在变量值的最前面添加 %JAVA_HOME%\bin;,注意最后要有一个; 分隔。

IDEA 安装及破解

去官网下载最新版本的 IDEA,需要注意的是,官网提供了两个版本一个是社区免费版,一个是旗舰版,我们要下载旗舰版的,因为免费版少了许多功能。

IDEA 官网:https://www.jetbrains.com/idea/download/#section=windows

现在最新版是 IntelliJ IDEA 2018.2 了,我之前下载的是 IntelliJ IDEA 2018.1.4 x64,因为我的是 win10 64bit 系统。

下载完成之后,点击下一步下一步就可以了,但是在最后那一步一定要注意,否则可能破解失败,那就是在最后出现完成界面的时候,一定要把那个是否马上运行前面的勾去掉。

(1)编辑电脑上 C:\Windows\System32\drivers\etc 下的 hosts 文件

加入下面一行代码:

0.0.0.0 account.jetbrains.com

保存并关闭。

(2)打开http://idea.lanyus.com/,获取注册码

打开 IDEA,

 IDEA 配置

IDEA 安装后之后,我们要来对其进行个性化配置,我觉得最重要的有两点,一个是快捷键的配置,一个是代码显示风格的配置,像我习惯了 VS 的代码显示风格,那么我们可以对其进行自定义修改。

我制作了一个配置文件,下载地址:https://files.cnblogs.com/files/jiekzou/settings.zip

下载完成之后,解压之后是一个 settings.jar 文件,然后在 IDEA 中把配置导入进来即可。

最后的显示效果如下:

关于 IDEA 的代码格式化快捷键,默认是:Ctrl+Alt+L,这和许多软件的锁屏快捷键冲突,所以我建议将其进行修改,我这里已经修改为和 VS 代码格式化快捷键一样:Ctrl+K,Ctrl+F。这样两个组合快捷键。

File——Settings

在 VS 中代码提示大小写是不敏感的,而 IDEA 默认情况下是敏感的,由于个人习惯,那么我需要将 IDEA 的代码智能提示也修改为大小写不敏感。

在 IDEA 中写的代码或者复制进去的代码,其命名空间也就是包会自动引入的,如果出现多个包,那么就需要自己手动引入,引入方式也很简单,用鼠标点到代码位置处,然后 Atl+Enter,选择指定的包就可以了。虽然 java 中没有属性这个传说,C#中属性其实就是像 java 中一样是通过一个 get 方法和一个 set 方法实现的,只是 C# 进行了封装,而 java 需要自己写,不过 IDEA 中有快捷键可以自动生成字段的属性:Alt+Insert。

不管使用什么样的 IDE,熟记各种快捷键都是必须的,为啥?效率!效率!还是效率!

第一个 spring boot web 程序

 what's spring boot?

我的理解是:spring boot=spring+spring mvc+ 一堆 java 技术栈的相关框架 + 约定大于配置的思想。

spring boot 并不是一项新的技术,就像 ajax 一样,而是各种已有技术的一个组合。它的出现,将极大地提高 java 应用的开发效率。同时,它的出现,彻底颠覆了以往我对 java 的认识(除了配置,TMD 还是配置)。轻量级、可插拔、微服务。

File——New——Project

关于 https://start.spring.io/,我们打开看下这是个啥

这不正是生成项目 jar 包的界面吗,直接在这个界面生成 jar 包,然后用 eclipse 打开也是一样的呀。

注意:通过这种方式来创建 spring boot 项目,会联网,从网站下载一些 jar 包。如果你想要通过离线的方式创建项目,可以选择 Maven。

由于是 web 项目,那么我们这里选择 web,关于 spring boot 版本,最新版本是 2.1.0,我这里用它的上一个稳定版 1.5.14。我们看到有些版本后面有 SNAPSHOT 字样,表明那是抢鲜版。

一直点击下一步,最终项目结果目录如下:

我们先来看下pom.xml,它是 maven 来管理各种 jar 包依赖的一个配置文件,maven 相当于.net 中的 nuget,是一个包管理工具。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.yujie<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>firstdemo<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>0.0.1-SNAPSHOT<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">packaging</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>jar<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">packaging</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>firstdemo<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">description</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Demo project for Spring Boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">description</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-parent<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.5.14.RELEASE<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">relativePath</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> lookup parent from repository </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">project.build.sourceEncoding</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>UTF-8<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">project.build.sourceEncoding</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">project.reporting.outputEncoding</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>UTF-8<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">project.reporting.outputEncoding</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.8<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-maven-plugin<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</project>

我们看到<parent>节点中申明了版本号,而且它是作为一个父节点,那么也就意味着它的子节点可以继承这个父节点的版本依赖。哪些是它的子节点,就是<dependencies>下面groupId<parent>groupId一致的依赖。这个 pom.xml 中的文件,正是我们之前可视化配置之后的产物,里面的一些信息就是我们之前填写的。那么后续,我们需要扩展的时候,只要直接修改这个配置文件就可以了。

关于pom.xml 详解,请参考:pom.xml 详解

如果你在 pom.xml 中添加代码的时候,没有智能提示,那么你需要更新一下。

 接下来,我们看下FirstdemoApplication这个类

package com.yujie;

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

@SpringBootApplication
public class FirstdemoApplication {

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    SpringApplication.run(FirstdemoApplication.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args);
}

}

我们看到有一个@SpringBootApplication 注解,我们查看一下它的源码如下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type
= FilterType.CUSTOM,
classes
= {TypeExcludeFilter.class}
), @Filter(
type
= FilterType.CUSTOM,
classes
= {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation
= EnableAutoConfiguration.class,
attribute
= "exclude"
)
Class
<?>[] exclude() default {};

@AliasFor(
    annotation </span>= EnableAutoConfiguration.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">,
    attribute </span>= "excludeName"<span style="color: rgba(0, 0, 0, 1)">
)
String[] excludeName() </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {};

@AliasFor(
    annotation </span>= ComponentScan.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">,
    attribute </span>= "basePackages"<span style="color: rgba(0, 0, 0, 1)">
)
String[] scanBasePackages() </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {};

@AliasFor(
    annotation </span>= ComponentScan.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">,
    attribute </span>= "basePackageClasses"<span style="color: rgba(0, 0, 0, 1)">
)
Class</span>&lt;?&gt;[] scanBasePackageClasses() <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {};

}

View Code

从源码中可以看出,其实@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。

我们依次再分别来看下这三个注解是什么意思。

@Configuration 是一个类级注释,指示对象是一个 bean 定义的源。@Configuration 类通过 @bean 注解的公共方法声明 bean。 @Configuration 的注解类标识这个类可以使用 Spring。

@Bean 注释是用来表示一个方法实例化,配置和初始化是由 Spring IoC 容器管理的一个新的对象。

@Configuration 一般与 @Bean 注解配合使用,用 @Configuration 注解类等价与 XML 中配置 beans,用 @Bean 注解方法等价于 XML 中配置 bean。

@EnableAutoConfiguration 注解的类所在的包有特定的意义,并且作为默认配置使用。

@ComponentScan 注解会自动扫描指定包下的全部标有 @Component 注解 的类,并注册成 bean,当然包括 @Component 下的子注解 @Service,@Repository,@Controller。

application.properties

这是应用的配置文件,跟.net 项目中的 web.config 差不多。但是我个人更习惯于使用 application.yml 这个文件来替代 application.properties

修改 application.properties,命名为 application.properties-bak,其实就是让这个配置文件失效,但是我又不想直接删除它。

然后在添加一个文件application.yml,注意要和application.properties在同一级目录下面。

添加配置项:

server:
  port: 8082

我这里修改 tomcat 服务器的启动端口为 8082,由于 IDEA srping boot 项目中自带了 tomcat,所以我们不需要配置外部的 tomcat。

新建一个控制器 TestController,它这个控制器的命名规范和.net mvc 中规范一致,都是 Controller 结尾。代码如下:

package com.yujie;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
// @RequestMapping("/hello")
// @RequestMapping(value = "/hello")
@RequestMapping(value = "/hello",method =RequestMethod.GET)
// @GetMapping("/hello")
public String hello(){
return "hello spring boot!";
}
}

@RequestMapping("/hello") 和 @RequestMapping(value = "/hello" 是等价的。

@RequestMapping(value = "/hello",method =RequestMethod.GET) @GetMapping("/hello")是等价的。
@RestController 注解相当于 @ResponseBody + @Controller。

如果需要返回到指定页面,则需要用 @Controller 配合视图解析器 InternalResourceViewResolver 才行。
如果需要返回 JSON,XML 或自定义 mediaType 内容到页面,则需要在对应的方法上加上 @ResponseBody 注解。

注意:@RestController 注解,相当于 @Controller+@ResponseBody 两个注解的结合,返回 json 数据不需要在方法前面加 @ResponseBody 注解了,但使用 @RestController 这个注解,就不能返回 jsp,html 页面,视图解析器无法解析 jsp,html 页面

运行程序:

热部署

在这之前,如果我们每次修改了代码,都必须重启一下服务器,并重新运行代码,那么有了热部署之后,修改了代码,我们只需要在 IDEA 中点击一下 Build,就可以直接看到效果了,不需要重启服务器。

pom.xml 文件中添加如下依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> </dependency>

添加依赖之后,在 IDEA 的右下角会弹出如下提示框

点击“Import Changes" 将会自动下载 j 所依赖的 jar 包。

修改 pom.xml,

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

如果不想每次修改后都要手动去 Build,可以在 IDEA 中配置自动 Build

然后按组合键:Shift+ALT+Ctrl+/ ,选择“Registry”,回车,找到“complier.automake.allow.when.app.running”,点击勾选即可。

优点:简单,支持 Spring-boot 项目,支持成员级别的修改热部署。

缺点:只支持 spring-boot 项目。

读取配置文件

修改配置文件 application.yml,添加如下代码:

server:
  port: 8082
website:
  name: 网站名称
  domain: www.yujie.com
  msg: 我这是一个博客网站

新建配置文件类WebSiteConfig,字段名称和配置项中的名称保持一致。

package com.yujie.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "website")
public class WebSiteConfig {
private String name;
private String domain;
private String msg;

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getDomain() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> domain;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setDomain(String domain) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.domain =<span style="color: rgba(0, 0, 0, 1)"> domain;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getMsg() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> msg;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setMsg(String msg) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.msg =<span style="color: rgba(0, 0, 0, 1)"> msg;
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "WebSiteConfig{" +
            "name='" + name + '\'' +
            ", domain='" + domain + '\'' +
            ", msg='" + msg + '\'' +
            '}'<span style="color: rgba(0, 0, 0, 1)">;
}

}

在控制器类TestController中修改代码:

@RestController
public class TestController {
    @Autowired
    private WebSiteConfig webSiteConfig;
//    @RequestMapping("/hello")
//    @RequestMapping(value = "/hello")
    @RequestMapping(value = "/hello",method =RequestMethod.GET)
   // @GetMapping("/hello")
    public String hello(){
        return "hello spring boot!1";
    }
    @GetMapping("/config")
    public String getConfig(){
        return  webSiteConfig.toString();}
}

是不是感觉像使用 vs 开发 asp.net mvc 项目一样那么简单?甚至比.net 操作配置文件还方便吧

参考:

Intellij IDEA 4 种配置热部署的方法