ganhuan 10 лет назад
Родитель
Сommit
0fd3c3f5eb
3 измененных файлов: 3 добавлений и 323 удалений
  1. 0
    16
      LICENSE.code.txt
  2. 0
    1
      LICENSE.writing.txt
  3. 3
    306
      README.adoc

+ 0
- 16
LICENSE.code.txt Просмотреть файл

@@ -1,16 +0,0 @@
1
-   All code in this repository is:
2
-   =======================================================================
3
-   Copyright (c) 2013 GoPivotal, Inc. All Rights Reserved
4
-
5
-   Licensed under the Apache License, Version 2.0 (the "License");
6
-   you may not use this file except in compliance with the License.
7
-   You may obtain a copy of the License at
8
-
9
-       http://www.apache.org/licenses/LICENSE-2.0
10
-
11
-   Unless required by applicable law or agreed to in writing, software
12
-   distributed under the License is distributed on an "AS IS" BASIS,
13
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
-   See the License for the specific language governing permissions and
15
-   limitations under the License.
16
-

+ 0
- 1
LICENSE.writing.txt Просмотреть файл

@@ -1 +0,0 @@
1
-Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by-nd/3.0/

+ 3
- 306
README.adoc Просмотреть файл

@@ -1,306 +1,3 @@
1
----
2
-tags: [spring-boot, groovy]
3
-projects: [spring-boot]
4
----
5
-:spring_boot_version: 1.3.5.RELEASE
6
-:spring-boot: https://github.com/spring-projects/spring-boot
7
-:toc:
8
-:icons: font
9
-:source-highlighter: prettify
10
-:project_id: gs-spring-boot
11
-This guide provides a sampling of how {spring-boot}[Spring Boot] helps you accelerate and facilitate application development. As you read more Spring Getting Started guides, you will see more use cases for Spring Boot.
12
-It is meant to give you a quick taste of Spring Boot. If you want to create your own Spring Boot-based project, visit 
13
-http://start.spring.io/[Spring Initializr], fill in your project details, pick your options, and you can download either
14
-a Maven build file, or a bundled up project as a zip file.
15
-
16
-== What you'll build
17
-You'll build a simple web application with Spring Boot and add some useful services to it.
18
-
19
-== What you'll need
20
-
21
-:java_version: 1.8
22
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
23
-
24
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
25
-
26
-
27
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-gradle.adoc[]
28
-
29
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-maven.adoc[]
30
-
31
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[]
32
-
33
-== Learn what you can do with Spring Boot
34
-
35
-Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you're missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
36
-
37
-For example:
38
-
39
-- Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.
40
-- Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.
41
-- Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.
42
-
43
-These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn't get in your way. For example, if Thymeleaf is on your path, Spring Boot adds a `SpringTemplateEngine` to your application context automatically. But if you define your own `SpringTemplateEngine` with your own settings, then Spring Boot won't add one. This leaves you in control with little effort on your part.
44
-
45
-NOTE: Spring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.
46
-
47
-== Create a simple web application
48
-Now you can create a web controller for a simple web application.
49
-
50
-`src/main/java/hello/HelloController.java`
51
-[source,java]
52
-----
53
-include::initial/src/main/java/hello/HelloController.java[]
54
-----
55
-    
56
-The class is flagged as a `@RestController`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text. That's because `@RestController` combines `@Controller` and `@ResponseBody`, two annotations that results in web requests returning data rather than a view.
57
-
58
-== Create an Application class
59
-Here you create an `Application` class with the components:
60
-
61
-`src/main/java/hello/Application.java`
62
-[source,java]
63
-----
64
-include::initial/src/main/java/hello/Application.java[]
65
-----
66
-
67
-`@SpringBootApplication` is a convenience annotation that adds all of the following:
68
-    
69
-- `@Configuration` tags the class as a source of bean definitions for the application context.
70
-- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
71
-- Normally you would add `@EnableWebMvc` for a Spring MVC app, but Spring Boot adds it automatically when it sees **spring-webmvc** on the classpath. This flags the application as a web application and activates key behaviors such as setting up a `DispatcherServlet`.
72
-- `@ComponentScan` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
73
-
74
-The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. Did you notice that there wasn't a single line of XML? No **web.xml** file either. This web application is 100% pure Java and you didn't have to deal with configuring any plumbing or infrastructure.
75
-
76
-The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
77
-
78
-== Run the application
79
-To run the application, execute:
80
-
81
-[subs="attributes"]
82
-----
83
-./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
84
-----
85
-
86
-If you are using Maven, execute:
87
-
88
-[subs="attributes"]
89
-----
90
-mvn package && java -jar target/{project_id}-0.1.0.jar
91
-----
92
-
93
-You should see some output like this:
94
-
95
-....
96
-Let's inspect the beans provided by Spring Boot:
97
-application
98
-beanNameHandlerMapping
99
-defaultServletHandlerMapping
100
-dispatcherServlet
101
-embeddedServletContainerCustomizerBeanPostProcessor
102
-handlerExceptionResolver
103
-helloController
104
-httpRequestHandlerAdapter
105
-messageSource
106
-mvcContentNegotiationManager
107
-mvcConversionService
108
-mvcValidator
109
-org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
110
-org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
111
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
112
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration
113
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
114
-org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
115
-org.springframework.boot.context.embedded.properties.ServerProperties
116
-org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
117
-org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
118
-org.springframework.context.annotation.internalAutowiredAnnotationProcessor
119
-org.springframework.context.annotation.internalCommonAnnotationProcessor
120
-org.springframework.context.annotation.internalConfigurationAnnotationProcessor
121
-org.springframework.context.annotation.internalRequiredAnnotationProcessor
122
-org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
123
-propertySourcesBinder
124
-propertySourcesPlaceholderConfigurer
125
-requestMappingHandlerAdapter
126
-requestMappingHandlerMapping
127
-resourceHandlerMapping
128
-simpleControllerHandlerAdapter
129
-tomcatEmbeddedServletContainerFactory
130
-viewControllerHandlerMapping
131
-....
132
-
133
-You can clearly see **org.springframework.boot.autoconfigure** beans. There is also a `tomcatEmbeddedServletContainerFactory`.
134
-
135
-Check out the service.
136
-
137
-....
138
-$ curl localhost:8080
139
-Greetings from Spring Boot!
140
-....
141
-
142
-== Add Unit Tests
143
-
144
-You will want to add a test for the endpoint you added, and Spring Test already provides some machinery for that, and it's easy to include in your project.
145
-
146
-Add this to your build file's list of dependencies:
147
-
148
-[source,groovy]
149
-----
150
-include::complete/build.gradle[tag=tests]
151
-----
152
-
153
-If you are using Maven, add this to your list of dependencies:
154
-
155
-[source,xml]
156
-----
157
-include::complete/pom.xml[tag=tests]
158
-----
159
-
160
-Now write a simple unit test that mocks the servlet request and response through your endpoint:
161
-
162
-`src/test/java/hello/HelloControllerTest.java`
163
-[source,java]
164
-----
165
-include::complete/src/test/java/hello/HelloControllerTest.java[]
166
-----
167
-
168
-Note the use of the `MockServletContext` to set up an empty `WebApplicationContext` so the `HelloController` can be created in the `@Before` and passed to `MockMvcBuilders.standaloneSetup()`. An alternative would be to create the full application context using the `Application` class and `@Autowired` the `HelloController` into the test. The `MockMvc` comes from Spring Test and allows you, via a set of convenient builder classes, to send HTTP requests into the `DispatcherServlet` and make assertions about the result.
169
-
170
-As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this:
171
-
172
-`src/test/java/hello/HelloControllerIT.java`
173
-[source,java]
174
-----
175
-include::complete/src/test/java/hello/HelloControllerIT.java[]
176
-----
177
-
178
-The embedded server is started up on a random port by virtue of the `@IntegrationTest("${server.port=0}")` and the actual port is discovered at runtime with the `@Value("${local.server.port}")`.
179
-
180
-== Add production-grade services
181
-If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready[actuator module], such as health, audits, beans, and more.
182
-
183
-Add this to your build file's list of dependencies:
184
-
185
-[source,groovy]
186
-----
187
-include::complete/build.gradle[tag=actuator]
188
-----
189
-
190
-If you are using Maven, add this to your list of dependencies:
191
-
192
-[source,xml]
193
-----
194
-include::complete/pom.xml[tag=actuator]
195
-----
196
-
197
-Then restart the app:
198
-
199
-[subs="attributes"]
200
-----
201
-./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
202
-----
203
-
204
-If you are using Maven, execute:
205
-
206
-[subs="attributes"]
207
-----
208
-mvn package && java -jar target/{project_id}-0.1.0.jar
209
-----
210
-
211
-You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
212
-
213
-....
214
-2014-06-03 13:23:28.119  ... : Mapped "{[/error],methods=[],params=[],headers=[],consumes...
215
-2014-06-03 13:23:28.119  ... : Mapped "{[/error],methods=[],params=[],headers=[],consumes...
216
-2014-06-03 13:23:28.136  ... : Mapped URL path [/**] onto handler of type [class org.spri...
217
-2014-06-03 13:23:28.136  ... : Mapped URL path [/webjars/**] onto handler of type [class ...
218
-2014-06-03 13:23:28.440  ... : Mapped "{[/info],methods=[GET],params=[],headers=[],consum...
219
-2014-06-03 13:23:28.441  ... : Mapped "{[/autoconfig],methods=[GET],params=[],headers=[],...
220
-2014-06-03 13:23:28.441  ... : Mapped "{[/mappings],methods=[GET],params=[],headers=[],co...
221
-2014-06-03 13:23:28.442  ... : Mapped "{[/trace],methods=[GET],params=[],headers=[],consu...
222
-2014-06-03 13:23:28.442  ... : Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=...
223
-2014-06-03 13:23:28.442  ... : Mapped "{[/env],methods=[GET],params=[],headers=[],consume...
224
-2014-06-03 13:23:28.443  ... : Mapped "{[/configprops],methods=[GET],params=[],headers=[]...
225
-2014-06-03 13:23:28.443  ... : Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],head...
226
-2014-06-03 13:23:28.443  ... : Mapped "{[/metrics],methods=[GET],params=[],headers=[],con...
227
-2014-06-03 13:23:28.444  ... : Mapped "{[/health],methods=[GET],params=[],headers=[],cons...
228
-2014-06-03 13:23:28.444  ... : Mapped "{[/dump],methods=[GET],params=[],headers=[],consum...
229
-2014-06-03 13:23:28.445  ... : Mapped "{[/beans],methods=[GET],params=[],headers=[],consu...
230
-....
231
-
232
-They include: errors, http://localhost:8080/env[environment], http://localhost:8080/health[health], http://localhost:8080/beans[beans], http://localhost:8080/info[info], http://localhost:8080/metrics[metrics], http://localhost:8080/trace[trace], http://localhost:8080/configprops[configprops], and http://localhost:8080/dump[dump].
233
-
234
-NOTE: There is also a `/shutdown` endpoint, but it's only visible by default via JMX. To http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-customizing-endpoints[enable it as an HTTP endpoint], add 
235
-`endpoints.shutdown.enabled=true` to your `application.properties` file.
236
-
237
-It's easy to check the health of the app.
238
-
239
-----
240
-$ curl localhost:8080/health
241
-{"status":"UP"}
242
-----
243
-
244
-You can try to invoke shutdown through curl.
245
-
246
-----
247
-$ curl -X POST localhost:8080/shutdown
248
-{"timestamp":1401820343710,"error":"Method Not Allowed","status":405,"message":"Request method 'POST' not supported"}
249
-----
250
-
251
-Because we didn't enable it, the request is blocked by the virtue of not existing.
252
-
253
-For more details about each of these REST points and how you can tune their settings with an `application.properties` file, you can read detailed http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-endpoints[docs about the endpoints].
254
-
255
-== View Spring Boot's starters
256
-You have seen some of http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#using-boot-starter-poms[Spring Boot's "starters"]. You can see them all https://github.com/spring-projects/spring-boot/tree/master/spring-boot-starters[here in source code].
257
-
258
-== JAR support and Groovy support
259
-The last example showed how Spring Boot makes it easy to wire beans you may not be aware that you need. And it showed how to turn on convenient management services.
260
-
261
-But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin` and `spring-boot-maven-plugin`.
262
-
263
-On top of that, Spring Boot also has Groovy support, allowing you to build Spring MVC web apps with as little as a single file.
264
-
265
-Create a new file called **app.groovy** and put the following code in it:
266
-
267
-[source,groovy]
268
-----
269
-@RestController
270
-class ThisWillActuallyRun {
271
-
272
-    @RequestMapping("/")
273
-    String home() {
274
-        return "Hello World!"
275
-    }
276
-
277
-}
278
-----
279
-
280
-NOTE: It doesn't matter where the file is. You can even fit an application that small inside a https://twitter.com/rob_winch/status/364871658483351552[single tweet]!
281
-
282
-Next, http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#getting-started-installing-the-cli[install Spring Boot's CLI].
283
-
284
-Run it as follows:
285
-
286
-----
287
-$ spring run app.groovy
288
-----
289
-
290
-NOTE: This assumes you shut down the previous application, to avoid a port collision.
291
-
292
-From a different terminal window:
293
-----
294
-$ curl localhost:8080
295
-Hello World!
296
-----
297
-
298
-Spring Boot does this by dynamically adding key annotations to your code and leveraging http://groovy.codehaus.org/Grape[Groovy Grape] to pull down needed libraries to make the app run.
299
-
300
-== Summary
301
-Congratulations! You built a simple web application with Spring Boot and learned how it can ramp up your development pace. You also turned on some handy production services.
302
-This is only a small sampling of what Spring Boot can do. Checkout http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle[Spring Boot's online docs]
303
-if you want to dig deeper.
304
-
305
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]
306
-
1
+yii-framework
2
+----------------------
3
+1.使用Spring Boot框架