{"id":2028,"date":"2023-04-01T20:06:20","date_gmt":"2023-04-01T12:06:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2028"},"modified":"2023-04-07T06:14:03","modified_gmt":"2023-04-06T22:14:03","slug":"spring-boot-quick-start","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/spring-boot-quick-start\/","title":{"rendered":"Spring Boot\u5feb\u901f\u5165\u95e8"},"content":{"rendered":"<h2>Spring Boot \u6982\u8ff0<\/h2>\n<blockquote>\n<p><strong>Build Anything with Spring Boot<\/strong>\uff1aSpring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.<\/p>\n<\/blockquote>\n<p>\u5f15\u81ea\u5b98\u7f51\u7684\u4e00\u6bb5\u8bdd\uff0c\u5927\u6982\u662f\u8bf4\uff1aSpring Boot\u662f\u6240\u6709\u57fa\u4e8eSpring\u5f00\u53d1\u7684\u9879\u76ee\u7684\u8d77\u70b9\u3002Spring Boot\u7684\u8bbe\u8ba1\u662f\u4e3a\u4e86\u8ba9\u4f60\u5c3d\u53ef\u80fd\u5feb\u7684\u8dd1\u8d77\u6765 Spring \u5e94\u7528\u7a0b\u5e8f\u5e76\u4e14\u5c3d\u53ef\u80fd\u51cf\u5c11\u4f60\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4f18\u52bf\uff1a\u7b80\u5355\u3001\u5feb\u901f\u3001\u65b9\u4fbf\u5730\u642d\u5efa\u9879\u76ee\uff1b\u5bf9\u4e3b\u6d41\u5f00\u53d1\u6846\u67b6\u7684\u65e0\u914d\u7f6e\u96c6\u6210\uff1b\u6781\u5927\u63d0\u9ad8\u4e86\u5f00\u53d1\u3001\u90e8\u7f72\u6548\u7387\u3002<\/p>\n<h2>Spring Boot \u5feb\u901f\u642d\u5efa<\/h2>\n<h3>\u65b0\u5efa\u9879\u76ee<\/h3>\n<p>New -&gt; Project -&gt; Spring Initializr -&gt; \u9009\u62e9\u9ed8\u8ba4\u7684url -&gt; \u914d\u7f6e\u9879\u76ee\u4fe1\u606fGroup\u548cArtifact -&gt; Dependencies\u9009\u62e9Web\uff0c\u5e76\u52fe\u9009\u4e0aWeb\u6a21\u677f -&gt; \u9009\u62e9\u597d\u9879\u76ee\u4f4d\u7f6e<\/p>\n<p>\u5efa\u8bae\u52fe\u9009\u7684\u4f9d\u8d56\uff1a<\/p>\n<ul>\n<li>Web -&gt; Web<\/li>\n<li>Template -&gt; Freemaker<\/li>\n<li>SQL -&gt; MySQL<\/li>\n<li>SQL -&gt;MyBatis<\/li>\n<\/ul>\n<p>\u5982\u679c\u662f\u7b2c\u4e00\u6b21\u914d\u7f6eSpring Boot\u53ef\u80fd\u9700\u8981\u7b49\u5f85\u4e00\u4f1aIDEA\u4e0b\u8f7d\u76f8\u5e94\u7684\u4f9d\u8d56\u5305\uff0c\u9ed8\u8ba4\u521b\u5efa\u597d\u7684\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><code>SpringbootApplication<\/code>\uff1a\u4e00\u4e2a\u5e26\u6709 main()\u65b9\u6cd5\u7684\u7c7b\uff0c\u7528\u4e8e\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/li>\n<li><code>SpringbootApplicationTests<\/code>\uff1a\u4e00\u4e2a\u7a7a\u7684Junit\u6d4b\u8bd5\u7c7b\uff0c\u5b83\u52a0\u8f7d\u4e86\u4e00\u4e2a\u4f7f\u7528Spring Boot\u5b57\u5178\u914d\u7f6e\u529f\u80fd\u7684Spring\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587<\/li>\n<li><code>application.properties<\/code>\uff1a\u4e00\u4e2a\u7a7a\u7684properties\u6587\u4ef6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u914d\u7f6e\u5c5e\u6027<\/li>\n<li><code>pom.xml<\/code>\uff1aMaven\u6784\u5efa\u8bf4\u660e\u6587\u4ef6<\/li>\n<\/ul>\n<h3>\u76ee\u5f55\u7ed3\u679c<\/h3>\n<p>Spring Boot\u5efa\u8bae\u7684\u76ee\u5f55\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>com\n  +- example\n    +- myproject\n      +- Application.java\n      |\n      +- model\n      |  +- Customer.java\n      |  +- CustomerRepository.java\n      |\n      +- service\n      |  +- CustomerService.java\n      |\n      +- controller\n      |  +- CustomerController.java\n      |<\/code><\/pre>\n<ol>\n<li><code>Application.java<\/code> \u5efa\u8bae\u653e\u5230\u6839\u76ee\u5f55\u4e0b\u9762,\u4e3b\u8981\u7528\u4e8e\u505a\u4e00\u4e9b\u6846\u67b6\u914d\u7f6e<\/li>\n<li><code>model<\/code> \u76ee\u5f55\u4e3b\u8981\u7528\u4e8e\u5b9e\u4f53\u4e0e\u6570\u636e\u8bbf\u95ee\u5c42\uff08Repository\uff09<\/li>\n<li><code>service<\/code> \u5c42\u4e3b\u8981\u662f\u4e1a\u52a1\u7c7b\u4ee3\u7801<\/li>\n<li><code>controller<\/code> \u8d1f\u8d23\u9875\u9762\u8bbf\u95ee\u63a7\u5236<\/li>\n<\/ol>\n<h3>\u5f15\u5165 web \u6a21\u5757<\/h3>\n<p>pom.xml\u4e2d\u6dfb\u52a0\u652f\u6301web\u7684\u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>pom.xml \u6587\u4ef6\u4e2d\u9ed8\u8ba4\u6709\u4e24\u4e2a\u6a21\u5757\uff1a<\/p>\n<ul>\n<li><code>spring-boot-starter<\/code>\uff1a\u6838\u5fc3\u6a21\u5757\uff0c\u5305\u62ec\u81ea\u52a8\u914d\u7f6e\u652f\u6301\u3001\u65e5\u5fd7\u548c YAML\uff0c\u5982\u679c\u5f15\u5165\u4e86<code>spring-boot-starter-web<\/code>\uff0cweb \u6a21\u5757\u53ef\u4ee5\u53bb\u6389\u6b64\u914d\u7f6e\uff0c\u56e0\u4e3a<code>spring-boot-starter-web<\/code>\u81ea\u52a8\u4f9d\u8d56\u4e86<code>spring-boot-starter<\/code><\/li>\n<li><code>spring-boot-starter-test<\/code>\uff1a\u6d4b\u8bd5\u6a21\u5757\uff0c\u5305\u62ec JUnit\u3001Hamcrest\u3001Mockito<\/li>\n<\/ul>\n<h3>HelloController<\/h3>\n<pre><code class=\"language-java\">package cn.appblog.springboot;\n\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class HelloController {\n\n    @RequestMapping(&quot;\/hello&quot;)\n    public String hello() {\n        return &quot;Hello Spring Boot!&quot;;\n    }\n\n}<\/code><\/pre>\n<blockquote>\n<p>@RestController \u6ce8\u89e3\uff1a\u8be5\u6ce8\u89e3\u662f @Controller \u548c @ResponseBody \u6ce8\u89e3\u7684\u5408\u4f53\u7248<\/p>\n<\/blockquote>\n<h3>\u5229\u7528 IDEA \u542f\u52a8 Spring Boot<\/h3>\n<p>\u56de\u5230SpringbootApplication\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u7136\u540e\u53f3\u952e\u70b9\u51fb<strong>Run &#8216;SpringbootApplication&#8217;<\/strong>\u8fd0\u884c\uff1a<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u4e4b\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u9879\u76ee\u4e2d\u6ca1\u6709\u624b\u52a8\u7684\u53bb\u914d\u7f6eTomcat\u670d\u52a1\u5668\uff0c\u662f\u56e0\u4e3aSpring Boot\u5185\u7f6e\u4e86Tomcat<\/p>\n<\/blockquote>\n<p>\u7b49\u5f85\u4e00\u4f1a\u513f\u5c31\u4f1a\u770b\u5230\u4e0b\u65b9\u7684\u6210\u529f\u8fd0\u884c\u7684\u63d0\u793a\u4fe1\u606f\uff1a<\/p>\n<pre><code>  .   ____          _            __ _ _\n \/\\\\ \/ ___&#039;_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\n( ( )\\___ | &#039;_ | &#039;_| | &#039;_ \\\/ _` | \\ \\ \\ \\\n \\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\n  &#039;  |____| .__|_| |_|_| |_\\__, | \/ \/ \/ \/\n =========|_|==============|___\/=\/_\/_\/_\/\n :: Spring Boot ::        (v2.1.3.RELEASE)\n\n...\nTomcat started on port(s): 8080 (http) with context path &#039;&#039;\nStarted SpringbootApplication in 2.711 seconds (JVM running for 4.294)\nInitializing Spring DispatcherServlet &#039;dispatcherServlet&#039;\nInitializing Servlet &#039;dispatcherServlet&#039;\nCompleted initialization in 10 ms<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230Tomcat\u8fd0\u884c\u57288080\u7aef\u53e3\uff0c\u8bbf\u95ee<code>\/hello<\/code>\u5730\u5740\u5373\u53ef\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8080\/hello\">http:\/\/localhost:8080\/hello<\/a><\/p>\n<h3>\u5355\u5143\u6d4b\u8bd5<\/h3>\n<p>\u6253\u5f00\u7684src\/test\/\u4e0b\u7684\u6d4b\u8bd5\u5165\u53e3\uff0c\u7f16\u5199\u7b80\u5355\u7684 http \u8bf7\u6c42\u6765\u6d4b\u8bd5\uff1b\u4f7f\u7528 mockmvc \u8fdb\u884c\uff0c\u5229\u7528MockMvcResultHandlers.print()\u6253\u5370\u51fa\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-java\">import org.junit.Before;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.http.MediaType;\nimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;\nimport org.springframework.test.web.servlet.MockMvc;\nimport org.springframework.test.web.servlet.request.MockMvcRequestBuilders;\nimport org.springframework.test.web.servlet.setup.MockMvcBuilders;\nimport static org.hamcrest.Matchers.equalTo;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;\n\n@SpringBootTest\n@RunWith(SpringRunner.class)\npublic class HelloTest {\n    private MockMvc mvc;\n\n    @Before\n    public void setUp() throws Exception {\n        mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();\n    }\n\n    @Test\n    public void getHello() throws Exception {\n        mvc.perform(MockMvcRequestBuilders.get(&quot;\/hello&quot;).accept(MediaType.APPLICATION_JSON))\n                .andExpect(status().isOk())\n                .andExpect(content().string(equalTo(&quot;Hello World&quot;)));\n    }\n}<\/code><\/pre>\n<h2>\u89e3\u6790Spring Boot\u9879\u76ee<\/h2>\n<h3>\u89e3\u6790 pom.xml \u6587\u4ef6<\/h3>\n<p>\u9ed8\u8ba4\u751f\u6210\u7684pom.xml\u6587\u4ef6\u4e2d\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u6bd4\u8f83\u964c\u751f\u7684\u6807\u7b7e<code>&lt;parent&gt;<\/code>\uff0c\u8fd9\u4e2a\u6807\u7b7e\u662f\u5728\u914d\u7f6eSpring Boot\u7684\u7236\u7ea7\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;parent&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n    &lt;version&gt;2.1.3.RELEASE&lt;\/version&gt;\n    &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;\n&lt;\/parent&gt;<\/code><\/pre>\n<p>\u6709\u4e86\u8fd9\u4e2a\u914d\u7f6e\uff0c\u5f53\u524d\u7684\u9879\u76ee\u624d\u662fSpring Boot\u9879\u76ee\uff0c<code>spring-boot-starter-parent<\/code>\u662f\u4e00\u4e2a\u7279\u6b8a\u7684starter\uff0c\u5b83\u7528\u6765\u63d0\u4f9b\u76f8\u5173\u7684Maven\u9ed8\u8ba4\u4f9d\u8d56\uff0c<strong>\u4f7f\u7528\u5b83\u4e4b\u540e\uff0c\u5e38\u7528\u7684\u5305\u4f9d\u8d56\u5c31\u53ef\u4ee5\u7701\u53bbversion\u6807\u7b7e<\/strong>\u3002<\/p>\n<p>\u5173\u4e8e\u5177\u4f53Spring Boot\u63d0\u4f9b\u4e86\u54ea\u4e9bjar\u5305\u7684\u4f9d\u8d56\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u672c\u5730Maven\u4ed3\u5e93\u4e0b\uff1a<code>\\repository\\org\\springframework\\boot\\spring-boot-dependencies\\2.1.3.RELEASE\\spring-boot-dependencies-2.1.3.RELEASE.pom<\/code>\u6587\u4ef6\u6765\u67e5\u770b<\/p>\n<h3>\u5e94\u7528\u5165\u53e3\u7c7b<\/h3>\n<p>Spring Boot\u9879\u76ee\u901a\u5e38\u6709\u4e00\u4e2a\u540d\u4e3a *Application \u7684\u5165\u53e3\u7c7b\uff0c\u5165\u53e3\u7c7b\u91cc\u6709\u4e00\u4e2amain\u65b9\u6cd5\uff0c \u8fd9\u4e2amain\u65b9\u6cd5\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u6807\u51c6\u7684Java\u5e94\u7528\u7684\u5165\u53e3\u65b9\u6cd5\u3002<\/p>\n<p><code>@SpringBootApplication<\/code>\u662fSpring Boot\u7684\u6838\u5fc3\u6ce8\u89e3\uff0c\u5b83\u662f\u4e00\u4e2a\u7ec4\u5408\u6ce8\u89e3\uff0c\u8be5\u6ce8\u89e3\u7ec4\u5408\u4e86\uff1a<code>@Configuration<\/code>\u3001<code>@EnableAutoConfiguration<\/code>\u3001<code>@ComponentScan<\/code>\uff1b\u82e5\u4e0d\u662f\u7528 <code>@SpringBootApplication<\/code>\u6ce8\u89e3\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e09\u4e2a\u6ce8\u89e3\u4ee3\u66ff\u3002<\/p>\n<p>\u5176\u4e2d\uff0c<code>@EnableAutoConfiguration<\/code>\u8ba9Spring Boot\u6839\u636e\u7c7b\u8def\u5f84\u4e2d\u7684jar\u5305\u4f9d\u8d56\u4e3a\u5f53\u524d\u9879\u76ee\u8fdb\u884c\u81ea\u52a8\u914d\u7f6e\uff0c\u4f8b\u5982\uff0c\u6dfb\u52a0\u4e86<code>spring-boot-starter-web<\/code>\u4f9d\u8d56\uff0c\u4f1a\u81ea\u52a8\u6dfb\u52a0Tomcat\u548cSpring MVC\u7684\u4f9d\u8d56\uff0c\u90a3\u4e48Spring Boot\u4f1a\u5bf9Tomcat\u548cSpring MVC\u8fdb\u884c\u81ea\u52a8\u914d\u7f6e\u3002<br \/>\nSpring Boot\u8fd8\u4f1a\u81ea\u52a8\u626b\u63cf<code>@SpringBootApplication<\/code>\u6240\u5728\u7c7b\u7684\u540c\u7ea7\u5305\u4ee5\u53ca\u4e0b\u7ea7\u5305\u91cc\u7684Bean\uff0c\u6240\u4ee5\u5165\u53e3\u7c7b\u5efa\u8bae\u5c31\u914d\u7f6e\u5728<code>grounpID + arctifactID<\/code>\u7ec4\u5408\u7684\u5305\u540d\u4e0b\uff08\u8fd9\u91cc\u4e3a cn.appblog.springboot \u5305\uff09<\/p>\n<h3>Spring Boot\u7684\u914d\u7f6e\u6587\u4ef6<\/h3>\n<h4><code>application.properties<\/code>\u4e0e<code>application.yml<\/code><\/h4>\n<p>Spring Boot\u4f7f\u7528\u4e00\u4e2a\u5168\u5c40\u7684\u914d\u7f6e\u6587\u4ef6<code>application.properties<\/code>\u6216<code>application.yml<\/code>\uff0c\u653e\u7f6e\u5728<code>src\/main\/resources<\/code>\u76ee\u5f55\u6216\u8005\u7c7b\u8def\u5f84\u7684<code>\/config<\/code>\u4e0b\u3002<\/p>\n<p>Spring Boot\u4e0d\u4ec5\u652f\u6301\u5e38\u89c4\u7684<code>properties<\/code>\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd8\u652f\u6301<code>yaml<\/code>\u8bed\u8a00\u7684\u914d\u7f6e\u6587\u4ef6\u3002<code>yaml<\/code>\u662f\u4ee5\u6570\u636e\u4e3a\u4e2d\u5fc3\u7684\u8bed\u8a00\uff0c\u5728\u914d\u7f6e\u6570\u636e\u7684\u65f6\u5019\u5177\u6709\u9762\u5411\u5bf9\u8c61\u7684\u7279\u5f81\u3002<\/p>\n<p><strong>Spring Boot\u7684\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u7684\u4f5c\u7528\u662f\u5bf9\u4e00\u4e9b\u9ed8\u8ba4\u914d\u7f6e\u7684\u914d\u7f6e\u503c\u8fdb\u884c\u4fee\u6539<\/strong><\/p>\n<p>\u4f8b\u5982\u540c\u6837\u5c06Tomcat\u9ed8\u8ba4\u7aef\u53e3\u8bbe\u7f6e\u4e3a8080\uff0c\u5e76\u5c06\u9ed8\u8ba4\u7684\u8bbf\u95ee\u8def\u5f84\u4ece<code>\/<\/code>\u4fee\u6539\u4e3a<code>\/hello<\/code>\u65f6\uff0c\u4f7f\u7528<code>properties<\/code>\u6587\u4ef6\u548c<code>yml<\/code>\u6587\u4ef6\u7684\u533a\u522b\uff1a<\/p>\n<p>application.properties<\/p>\n<pre><code class=\"language-properties\">server.port=8080\nserver.servlet.context-path=\/appblog<\/code><\/pre>\n<p>application.yml<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8080\n  servlet:\n    context-path: \/appblog<\/code><\/pre>\n<p>\u91cd\u542fSpring Boot\uff0c\u8f93\u5165URL\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8080\/appblog\/hello\">http:\/\/localhost:8080\/appblog\/hello<\/a><\/p>\n<p>\u6ce8\u610f\uff1a<code>yml<\/code>\u9700\u8981\u5728<code>:<\/code>\u540e\u52a0\u4e00\u4e2a\u7a7a\u683c\uff0c\u5e78\u597dIDEA\u5f88\u597d\u5730\u652f\u6301\u4e86<code>yml<\/code>\u6587\u4ef6\u7684\u683c\u5f0f\u6709\u826f\u597d\u7684\u4ee3\u7801\u63d0\u793a<\/p>\n<h4>\u914d\u7f6e\u591a\u4e2a\u5c5e\u6027<\/h4>\n<p>\u6211\u4eec\u76f4\u63a5\u628a<code>.properties<\/code>\u540e\u7f00\u7684\u6587\u4ef6\u5220\u6389\uff0c\u4f7f\u7528<code>.yml<\/code>\u6587\u4ef6\u6765\u8fdb\u884c\u7b80\u5355\u7684\u914d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528<code>@Value<\/code>\u6765\u83b7\u53d6\u914d\u7f6e\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8080\n  servlet:\n    context-path: \/\nname: Joe.Ye\nage: 28<\/code><\/pre>\n<pre><code class=\"language-java\">@RestController\npublic class HelloController {\n\n    @Value(&quot;${name}&quot;)\n    private String name;\n\n    @Value(&quot;${age}&quot;)\n    private Integer age;\n\n    @RequestMapping(&quot;\/hello&quot;)\n    public String hello() {\n        return &quot;name:&quot; + name + &quot;, &quot; + &quot;age:&quot; + age;\n    }\n}<\/code><\/pre>\n<p>\u91cd\u542fSpring Boot\uff0c\u8f93\u5165URL\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8080\/hello\">http:\/\/localhost:8080\/hello<\/a><\/p>\n<p>\u4e5f\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528\u5f53\u524d\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8080\n  servlet:\n    context-path: \/\nname: Joe.Ye\nage: 28\ncontent: &quot;name:${name}, age:${age}&quot;<\/code><\/pre>\n<pre><code class=\"language-java\">@RestController\npublic class HelloController {\n\n    @Value(&quot;${content}&quot;)\n    private String content;\n\n    @RequestMapping(&quot;\/hello&quot;)\n    public String hello() {\n        return content;\n    }\n}<\/code><\/pre>\n<h4>\u5c01\u88c5\u914d\u7f6e\u4fe1\u606f<\/h4>\n<p>\u95ee\u9898\uff1a\u5982\u4e0a\u5199\u914d\u7f6e\u6587\u4ef6\u7e41\u7410\u800c\u4e14\u53ef\u80fd\u4f1a\u9020\u6210\u7c7b\u7684\u81c3\u80bf\uff0c\u56e0\u4e3a\u6709\u8bb8\u8bb8\u591a\u591a\u7684<code>@Value<\/code>\u6ce8\u89e3\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u628a\u914d\u7f6e\u4fe1\u606f\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b\uff0c\u9996\u5148\u5728\u6211\u4eec\u7684<code>name<\/code>\u548c<code>age<\/code>\u524d\u52a0\u4e00\u4e2a<code>person<\/code>\u524d\u7f00\uff0c\u7136\u540e\u65b0\u5efa\u4e00\u4e2a<code>Person<\/code>\u7c7b\u7528\u6765\u5c01\u88c5\u8fd9\u4e9b\u4fe1\u606f\uff0c\u5e76\u7528\u4e0a\u4e24\u4e2a\u6ce8\u89e3\uff1a<\/p>\n<ul>\n<li><code>@Component<\/code>\uff1a\u8868\u660e\u5f53\u524d\u7c7b\u662f\u4e00\u4e2aJava Bean<\/li>\n<li><code>@ConfigurationProperties(prefix = &quot;person&quot;)<\/code>\uff1a\u8868\u793a\u83b7\u53d6\u524d\u7f00\u4e3a<code>person<\/code>\u7684\u914d\u7f6e\u4fe1\u606f<\/li>\n<\/ul>\n<pre><code class=\"language-yml\">server:\n  port: 8080\n  servlet:\n    context-path: \/\nperson:\n  name: Joe.Ye\n  age: 28<\/code><\/pre>\n<pre><code class=\"language-java\">@Component\n@ConfigurationProperties(prefix = &quot;person&quot;)\npublic class Person {\n    private String name;\n    private Integer age;\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public Integer getAge() {\n        return age;\n    }\n\n    public void setAge(Integer age) {\n        this.age = age;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@RestController\npublic class HelloController {\n\n    @Autowired\n    private Person person;\n\n    @RequestMapping(&quot;\/hello&quot;)\n    public String hello() {\n        return &quot;name:&quot; + person.getName() + &quot;, age:&quot; + person.getAge();\n    }\n}<\/code><\/pre>\n<h3>Spring Boot\u70ed\u90e8\u7f72<\/h3>\n<h4>IDEA\u914d\u7f6e\u70ed\u90e8\u7f72<\/h4>\n<p>\uff081\uff09<code>File<\/code> -&gt; <code>Settings<\/code> -&gt; <code>Compiler<\/code> -&gt; \u52fe\u9009<code>Build project automatically<\/code><br \/>\n\uff082\uff09\u5feb\u6377\u952e<code>Ctrl+Shift+Alt+\/<\/code> -&gt; \u9009\u4e2dRegistry -&gt; \u52fe\u9009<code>compiler.automake.allow.when.app.running<\/code><br \/>\n\uff083\uff09\u91cd\u542fIDEA<br \/>\n\uff084\uff09Chrome\u6d4f\u89c8\u5668\u4e2d\u6309F12 -&gt; Disable cache<\/p>\n<h4>Spring Boot\u914d\u7f6e\u70ed\u90e8\u7f72<\/h4>\n<p>\u5728\u76ee\u524d\u7684Spring Boot\u9879\u76ee\u4e2d\uff0c\u5f53\u53d1\u751f\u4efb\u4f55\u4fee\u6539\u4e4b\u540e\u6211\u4eec\u90fd\u9700\u8981\u91cd\u65b0\u542f\u52a8\u624d\u80fd\u591f\u67e5\u770b\u6548\u679c\uff0c\u8fd9\u6837\u4f1a\u7565\u663e\u9ebb\u70e6\uff0cSpring Boot\u63d0\u4f9b\u4e86\u70ed\u90e8\u7f72\u7684\u65b9\u5f0f\uff0c\u5f53\u53d1\u73b0\u4efb\u4f55\u7c7b\u53d1\u751f\u6539\u53d8\uff0c\u5c31\u4f1a\u901a\u8fc7JVM\u7c7b\u52a0\u8f7d\u7684\u65b9\u5f0f\uff0c\u52a0\u8f7d\u6700\u65b0\u7684\u7c7b\u5230\u865a\u62df\u673a\u4e2d\uff0c\u4e0d\u9700\u8981\u91cd\u542f\u4e5f\u80fd\u770b\u5230\u4fee\u6539\u540e\u7684\u6548\u679c\u3002<\/p>\n<p>\u5728<code>pom.xml<\/code>\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u4f9d\u8d56\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-devtools&lt;\/artifactId&gt;\n    &lt;optional&gt;true&lt;\/optional&gt; &lt;!-- \u9700\u8981\u4e3atrue\u70ed\u90e8\u7f72\u624d\u6709\u6548 --&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u91cd\u65b0\u542f\u52a8Spring Boot\uff0c\u7136\u540e\u4fee\u6539\u4efb\u610f\u4ee3\u7801\uff0c\u5c31\u80fd\u89c2\u5bdf\u5230\u63a7\u5236\u53f0\u7684\u81ea\u52a8\u91cd\u542f\u73b0\u8c61\u3002<\/p>\n<h2>Spring Boot\u4f7f\u7528<\/h2>\n<h3>Spring Boot\u652f\u6301JSP<\/h3>\n<p>Spring Boot\u7684\u9ed8\u8ba4\u89c6\u56fe\u652f\u6301\u662f<code>Thymeleaf<\/code>\u6a21\u677f\u5f15\u64ce\uff0c\u6211\u4eec\u8fd8\u662f\u60f3\u8981\u4f7f\u7528JSP\u600e\u4e48\u529e\u5462\uff1f<\/p>\n<p>\uff081\uff09\u4fee\u6539<code>pom.xml<\/code>\u589e\u52a0\u5bf9JSP\u6587\u4ef6\u7684\u652f\u6301<\/p>\n<pre><code class=\"language-xml\">&lt;!-- servlet\u4f9d\u8d56. --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\n    &lt;artifactId&gt;javax.servlet-api&lt;\/artifactId&gt;\n    &lt;scope&gt;provided&lt;\/scope&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\n    &lt;artifactId&gt;jstl&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- tomcat\u7684\u652f\u6301.--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-tomcat&lt;\/artifactId&gt;\n    &lt;!--&lt;scope&gt;provided&lt;\/scope&gt;--&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.tomcat.embed&lt;\/groupId&gt;\n    &lt;artifactId&gt;tomcat-embed-jasper&lt;\/artifactId&gt;\n    &lt;scope&gt;provided&lt;\/scope&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\uff082\uff09\u914d\u7f6e\u8bd5\u56fe\u91cd\u5b9a\u5411JSP\u6587\u4ef6\u7684\u4f4d\u7f6e<\/p>\n<p>\u4fee\u6539<code>application.yml<\/code>\u6587\u4ef6\uff0c\u5c06JSP\u6587\u4ef6\u91cd\u5b9a\u5411\u5230<code>\/WEB-INF\/views\/<\/code>\u76ee\u5f55\u4e0b\uff1a<\/p>\n<pre><code class=\"language-yml\">spring:\n  mvc:\n    view:\n      prefix: \/WEB-INF\/views\/\n      suffix: .jsp<\/code><\/pre>\n<p>\uff083\uff09\u4fee\u6539 HelloController<\/p>\n<p>\u4fee\u6539<code>@RestController<\/code>\u6ce8\u89e3\u4e3a<code>@Controller<\/code>\uff0c\u7136\u540e\u5c06<code>hello<\/code>\u65b9\u6cd5\u4fee\u6539\u4e3a\uff1a<\/p>\n<p>\uff084\uff09\u65b0\u5efa hello.jsp \u6587\u4ef6<\/p>\n<p>\u5728<code>src\/main<\/code>\u76ee\u5f55\u4e0b\u4f9d\u6b21\u521b\u5efa<code>webapp\/WEB-INF\/views<\/code>\u76ee\u5f55\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a<code>hello.jsp<\/code>\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-jsp\">&lt;%@ page language=&quot;java&quot; contentType=&quot;text\/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot; %&gt;\nHello, Joe.Ye. \u73b0\u5728\u65f6\u95f4\u662f${now}<\/code><\/pre>\n<p>\uff085\uff09\u70ed\u90e8\u7f72\u5b8c\u6bd5\u5237\u65b0\u7f51\u9875<\/p>\n<p>\u82e5\u62a5\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre><code>Whitelabel Error Page\nThis application has no explicit mapping for \/error, so you are seeing this as a fallback.\n\nFri Mar 08 17:13:32 CST 2019\nThere was an unexpected error (type=Not Found, status=404).\nNo message available<\/code><\/pre>\n<p>\u5219\u505c\u6b62\u8fd0\u884c\u5e94\u7528\uff0c\u518d\u4f7f\u7528Maven\u63d2\u4ef6<code>spring-boot:run<\/code>(<code>Plugins<\/code> -&gt; <code>spring-boot<\/code> -&gt; <code>spring-boot:run<\/code>)\u8fd0\u884c\u9879\u76ee\u53ef\u4ee5\u89e3\u51b3<\/p>\n<h3>\u4fee\u6539Spring Boot\u5185\u5d4cTomcat\u8fd0\u884c\u73af\u5883<\/h3>\n<pre><code class=\"language-java\">@Bean\npublic WebServerFactoryCustomizer webServerFactoryCustomizer(){\n    return new WebServerFactoryCustomizer&lt;ConfigurableServletWebServerFactory&gt;() {\n        @Override\n        public void customize(ConfigurableServletWebServerFactory factory) {\n            factory.setPort(8090);  \/\/\u9ed8\u8ba48080\n            factory.setDocumentRoot(new File(&quot;src\/main\/appblog&quot;));  \/\/\u9ed8\u8ba4src\/main\/webapp\n        }\n    };\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u6982\u8ff0 Build Anything with Spring Boot\uff1aSpring B [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-2028","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2028"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2028\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}