Spring Boot 快速入门(一)无配置文件

来源:互联网 发布:淘宝在台湾 编辑:程序博客网 时间:2024/06/06 18:54
1. pom.xml

一下是pom文件的配置,配置的时候需要注意依赖的位置。

<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/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>myTest</groupId>  <artifactId>myTest</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>myTest Maven Webapp</name>  <url>http://maven.apache.org</url> <!-- spring-boot-starter-parent 是一个特殊的starter,它提供了有用的Maven默认设置。同时,它也提供了一个 dependency-management 节点,这样对于”blessed“依赖你可以省略version标记 -->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.3.RELEASE</version>    </parent>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <!-- Add typical dependencies for a web application 添加典型依赖 Web模块-->    <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!-- 测试模块 -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>  </dependencies>  <build>    <finalName>myTest</finalName>  </build></project>
2. 创建Example类
package com.test.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController//这被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。对于Spring,该类扮演了一个特殊角色。在本示例中,我们的类是一个web @Controller ,所以当处理进来的web请求时,Spring会询问它。@EnableAutoConfiguration//The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.public class Example {/* * @RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boot的特定部分)。 *  */    @RequestMapping("/")//这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。    public String home() {        return "Hellow Man!";    }    /*     * 这只是一个标准的方法,它遵循Java对于一个应用程序入口点的约定。我们的main方     法通过调用run,将业务委托给了Spring Boot的SpringApplication类。SpringApplication将引导我们 的应用,启动Spring,相     应地启动被自动配置的Tomcat web服务器。我们需要将 Example.class  作为参数传递给run方法来告诉SpringApplication谁是     主要的Spring组件。为了暴露任何的命令行参数,args数组也会被传递过去。     * */    public static void main(String[] args) throws Exception {        SpringApplication.run(Example.class, args);    }}
3. 访问
  • 在浏览器中打开 http://localhost:8080/ 即可访问
  • 这里的 localhost 不可写为127.0.0.1 下篇文章中加入配置文件之后 可以自己配置端口 以及访问路径
原创粉丝点击