澳门新葡亰官网Spring boot入门篇

澳门新葡亰官网Spring boot入门篇

  1. Spring
    Boot是由Pivotal团队供的崭新框架,其设计目的是为此来简化新Spring应用的始搭建与支出进程。该框架下了特定的措施来开展配置,从而使开发人员不再要定义样板化的配置。通过这种方式,Boot致力为以蓬勃发展的敏捷利用开发领域(rapid
       application  development)成为官员。
     可以 Maven | Gradle | Ant |
    Starters构建项目,参考:http://start.spring.io/ 可以选Maven或Gradle生成Demo,Spring
    boot微服务架构重组Docker容器运行。
      软件版本运行要求要参见官网: Spring boot官网  
    本实例软件版本:JDK1.7 + Spring boot 1.3.5 + Spring 4.2.6 

  常用之starter以及用可列举如下:

(1)spring-boot-starter: 这是中心Spring Boot
starter,提供了大部分基础力量,其他starter都依赖让它,因此无必要显式定义其。

(2)spring-boot-starter-actuator:主要提供监控、管理暨审查应用程序的功用。

(3)spring-boot-starter-jdbc:该starter提供针对性JDBC操作的支撑,包括连接数据库、操作数据库,以及管理数据库连接等等。

(4)spring-boot-starter-data-jpa:JPA starter提供利用Java Persistence
API(例如Hibernate等)的依赖库。

(5)spring-boot-starter-data-*:提供对MongoDB、Data-Rest或者Solr的支持。

(6)spring-boot-starter-security:提供有Spring-security的依赖库。

(7)spring-boot-starter-test:这个starter包括了spring-test依赖以及任何测试框架,例如JUnit和Mockito等等。

(8)spring-boot-starter-web:该starter包括web应用程序的依赖库。

2.Maven构建项目pom代码

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4 
  5     <groupId>ygnet</groupId>
  6     <artifactId>boot</artifactId>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <packaging>jar</packaging>
  9 
 10     <name>Springboot</name>
 11     <url>http://maven.apache.org</url>
 12     
 13     <properties>
 14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 15         <java.version>1.7</java.version>
 16     </properties>
 17     
 18     <!-- Spring Boot 启动父依赖 -->
 19     <parent>
 20         <groupId>org.springframework.boot</groupId>
 21         <artifactId>spring-boot-starter-parent</artifactId>
 22         <version>1.3.5.RELEASE</version>
 23         <relativePath />
 24     </parent>
 25     <dependencies>
 26         <dependency>
 27             <groupId>junit</groupId>
 28             <artifactId>junit</artifactId>
 29             <version>4.12</version>
 30             <scope>test</scope>
 31         </dependency>
 32         <dependency>
 33           <groupId>org.springframework</groupId>
 34           <artifactId>spring-test</artifactId>
 35           <version>4.2.6.RELEASE</version>
 36         </dependency>
 37             
 38         <!-- Spring Boot web依赖 -->
 39         <dependency>
 40             <groupId>org.springframework.boot</groupId>
 41             <artifactId>spring-boot-starter-web</artifactId>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.springframework.boot</groupId>
 45             <artifactId>spring-boot-starter</artifactId>
 46         </dependency>    
 47         <!--dependency>
 48             <groupId>org.springframework.boot</groupId>
 49             <artifactId>spring-boot-starter-test</artifactId>
 50             <scope>test</scope>
 51         </dependency-->
 52         <dependency>
 53             <groupId>org.springframework.boot</groupId>
 54             <artifactId>spring-boot-starter-jdbc</artifactId>
 55         </dependency>
 56         <dependency>
 57             <groupId>org.postgresql</groupId>
 58             <artifactId>postgresql</artifactId><scope>runtime</scope>
 59         </dependency>
 60         <dependency>
 61             <groupId>org.springframework.boot</groupId>
 62             <artifactId>spring-boot-starter-actuator</artifactId>
 63         </dependency>
 64     </dependencies>
 65     <build>
 66         <pluginManagement>  
 67             <plugins>  
 68               <plugin>  
 69                 <groupId>org.eclipse.m2e</groupId>  
 70                 <artifactId>lifecycle-mapping</artifactId>  
 71                 <version>1.0.0</version>  
 72                 <configuration>  
 73                   <lifecycleMappingMetadata>  
 74                     <pluginExecutions>  
 75                       <pluginExecution>  
 76                         <pluginExecutionFilter>  
 77                           <groupId>org.apache.maven.plugins</groupId>  
 78                           <artifactId>maven-dependency-plugin</artifactId>  
 79                           <versionRange>[2.0,)</versionRange>  
 80                           <goals>  
 81                             <goal>copy-dependencies</goal>  
 82                           </goals>  
 83                         </pluginExecutionFilter>  
 84                         <action>  
 85                           <ignore />  
 86                         </action>  
 87                       </pluginExecution>  
 88                     </pluginExecutions>  
 89                   </lifecycleMappingMetadata>  
 90                 </configuration>  
 91               </plugin>  
 92             </plugins>  
 93         </pluginManagement>
 94         <plugins>
 95             <!-- 打Jar包(META-INF) -->
 96             <plugin>
 97                 <groupId>org.apache.maven.plugins</groupId>  
 98                 <artifactId>maven-jar-plugin</artifactId>  
 99                 <configuration>  
100                     <archive>  
101                         <manifest>  
102                            <addClasspath>true</addClasspath>  
103                            <classpathPrefix>lib/</classpathPrefix>  
104                            <mainClass>yg.boot.App</mainClass>  
105                         </manifest>  
106                     </archive>  
107                 </configuration>  
108             </plugin>
109             <!-- 项目资源文件 -->
110             <plugin>  
111                 <groupId>org.apache.maven.plugins</groupId>  
112                 <artifactId>maven-resources-plugin</artifactId>  
113                 <version>2.5</version>  
114                 <executions>  
115                     <execution>  
116                         <phase>compile</phase>  
117                     </execution>  
118                 </executions>  
119                 <configuration>  
120                     <encoding>${project.build.sourceEncoding}</encoding>  
121                 </configuration>  
122             </plugin>
123             <!-- 是否启动测试 -->
124             <plugin>
125                 <groupId>org.apache.maven.plugins</groupId>  
126                 <artifactId>maven-surefire-plugin</artifactId>
127                 <version>2.17</version> 
128                 <configuration>
129                   <skipTests>true</skipTests>  
130                 </configuration>
131             </plugin>
132             <!-- 复制依赖包到项目lib文件夹下 -->
133             <plugin>  
134                 <groupId>org.apache.maven.plugins</groupId>  
135                 <artifactId>maven-dependency-plugin</artifactId>  
136                 <version>2.8</version>  
137                 <executions>  
138                     <execution>  
139                         <phase>package</phase>  
140                         <goals>  
141                             <goal>copy-dependencies</goal>  
142                         </goals>  
143                     </execution>  
144                 </executions>
145                 <configuration>
146                     <outputDirectory>${project.basedir}/lib</outputDirectory>
147                     <includeScope>compile</includeScope>  
148                 </configuration>  
149             </plugin>
150             <!-- Spring boot 打包 -->
151             <plugin>
152                 <groupId>org.springframework.boot</groupId>
153                 <artifactId>spring-boot-maven-plugin</artifactId>
154             </plugin>
155         </plugins>
156     </build>
157 </project>

3.Controller

Spring
Boot框架提供的建制好工程师实现规范的RESTful接口,编写Controller代码,首先我们若以pom文件中长对应的starter,即spring-boot-starter-web,对应的xml代码示例为:
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@RestController注解是@Controller和@ResponseBody的合集,表示这是单控制器bean,并且是用函数的回来值直接填入HTTP响应体中,是REST风格的控制器。
@RequestMapping(“/test”)表示该控制器处理所有“/test”的URL请求,具体由坏函数处理,要根据HTTP的章程来区别:GET表示查询、POST表示提交、PUT表示更新、DELETE代表去。
Restful设计指南请参见:RESTFul Controller的角色,大家可看到,我此拿广大事务代码混淆在Controller的代码中。实际上,根据程序员必知之前端演进史一文所述Controller层应该举行的从是:
处理要的参数 渲染和重定向 选择Model和Service
处理Session和Cookies,我差不多认同者看法,最多又增长OAuth验证(利用拦截器实现即可)。而真的业务逻辑应该单独分处一重合来拍卖,即大的service层;

 java代码:

 1 package yg.boot.action;
 2 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 @RestController
 6 @EnableAutoConfiguration
 7 @RequestMapping("/test")
 8 public class AppController {    
 9     @RequestMapping("/sayhello")
10     public String sayHello(){
11         return "Hello World!";
12     }
13 }

4.Spring Boot启动  

 
@SpringBootApplication是这个注解是欠应用程序入口的表明,然后出习的main函数,通过SpringApplication.run(xxxApplication.class,
args)来运作Spring Boot应用。打开SpringBootApplication注解  
可以窥见,它是由另外几独像样组合而成的:@Configuration

(等同于
spring中的xml配置文件,使用Java文件举行安排好检查类安全)、@EnableAutoConfiguration(自动配置)、@ComponentScan(组件扫描,大家老熟悉的,可以自动发现及配有Bean)

 
 pom文件里可以看来,org.postgresql这个库起作用的限定是runtime,也就是说,当应用程序启动时,如果Spring
Boot在classpath下检测到org.postgresql的存在,会活动配置postgresql数据库连接。

Application.properties代码:

 1 # DataSource settings
 2 spring.datasource.url=jdbc:postgresql://localhost:5432/jcbk
 3 spring.datasource.username=jcbk
 4 spring.datasource.password=123456
 5 spring.datasource.driver-class-name=org.postgresql.Driver
 6 
 7 # Tomcat Server settings (ServerProperties)
 8 server.port= 9080
 9 server.address= 127.0.0.1
10 server.sessionTimeout= 30
11 server.contextPath= /
12 
13 # Tomcat specifics
14 tomcat.accessLogEnabled= false
15 tomcat.protocolHeader= x-forwarded-proto
16 tomcat.remoteIpHeader= x-forwarded-for
17 tomcat.basedir=
18 tomcat.backgroundProcessorDelay=30 \# secs

java代码:

 1 package yg.boot;
 2 import org.springframework.boot.SpringApplication;
 3 import org.springframework.boot.autoconfigure.SpringBootApplication;
 4 /**
 5  * Hello world!
 6  */
 7 @SpringBootApplication
 8 public class App {    
 9     public static void main(String[] args ){
10         SpringApplication.run(App.class,args);
11     }
12 }

直运行App后,结果而下图所出示。启动后拜http://localhost:9080/test/sayhello,
输出 Hello World!,如下所示:

澳门新葡亰官网 1

5.项目打包

种打包使用maven-jar-plugin插件即可,生成boot-0.0.1-SNAPSHOT.jar。spring-boot-maven-plugin插件将boot-0.0.1-SNAPSHOT.jar重命名吧boot-0.0.1-SNAPSHOT.jar.original,然后生成新boot-0.0.1-SNAPSHOT.jar包澳门新葡亰官网,目录结构为:
+—yg
          boot
+—org
          springframework
              boot
                 loader
+—-lib
+—-META-INF
+—-application.properties

Meta-inf代码:

 1 Manifest-Version: 1.0
 2 Implementation-Vendor: Pivotal Software, Inc.
 3 Implementation-Title: Springboot
 4 Implementation-Version: 0.0.1-SNAPSHOT
 5 Implementation-Vendor-Id: ygnet
 6 Built-By: oy
 7 Build-Jdk: 1.7.0_45
 8 Class-Path: lib/spring-test-4.2.6.RELEASE.jar lib/spring-core-4.2.6.RE
 9  LEASE.jar lib/spring-boot-starter-web-1.3.5.RELEASE.jar lib/spring-bo
10  ot-starter-tomcat-1.3.5.RELEASE.jar lib/tomcat-embed-core-8.0.33.jar 
11  lib/tomcat-embed-el-8.0.33.jar lib/tomcat-embed-logging-juli-8.0.33.j
12  ar lib/tomcat-embed-websocket-8.0.33.jar lib/spring-boot-starter-vali
13  dation-1.3.5.RELEASE.jar lib/hibernate-validator-5.2.4.Final.jar lib/
14  validation-api-1.1.0.Final.jar lib/jboss-logging-3.3.0.Final.jar lib/
15  classmate-1.1.0.jar lib/jackson-databind-2.6.6.jar lib/jackson-annota
16  tions-2.6.6.jar lib/jackson-core-2.6.6.jar lib/spring-web-4.2.6.RELEA
17  SE.jar lib/spring-aop-4.2.6.RELEASE.jar lib/aopalliance-1.0.jar lib/s
18  pring-beans-4.2.6.RELEASE.jar lib/spring-context-4.2.6.RELEASE.jar li
19  b/spring-webmvc-4.2.6.RELEASE.jar lib/spring-expression-4.2.6.RELEASE
20  .jar lib/spring-boot-starter-1.3.5.RELEASE.jar lib/spring-boot-1.3.5.
21  RELEASE.jar lib/spring-boot-autoconfigure-1.3.5.RELEASE.jar lib/sprin
22  g-boot-starter-logging-1.3.5.RELEASE.jar lib/logback-classic-1.1.7.ja
23  r lib/logback-core-1.1.7.jar lib/slf4j-api-1.7.21.jar lib/jcl-over-sl
24  f4j-1.7.21.jar lib/jul-to-slf4j-1.7.21.jar lib/log4j-over-slf4j-1.7.2
25  1.jar lib/snakeyaml-1.16.jar lib/spring-boot-starter-jdbc-1.3.5.RELEA
26  SE.jar lib/tomcat-jdbc-8.0.33.jar lib/tomcat-juli-8.0.33.jar lib/spri
27  ng-jdbc-4.2.6.RELEASE.jar lib/spring-tx-4.2.6.RELEASE.jar lib/postgre
28  sql-9.4.1208.jre7.jar lib/spring-boot-starter-actuator-1.3.5.RELEASE.
29  jar lib/spring-boot-actuator-1.3.5.RELEASE.jar
30 Start-Class: yg.boot.App
31 Created-By: Apache Maven 3.0.4
32 Spring-Boot-Version: 1.3.5.RELEASE
33 Main-Class: org.springframework.boot.loader.JarLauncher
34 Archiver-Version: Plexus Archiver

 

 Start-Class为Spring boot启动类,Main-Class为main方法入口。

                                      END

 

admin

网站地图xml地图