码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot系列之环境搭建

时间:2020-08-03 23:16:58      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:request   package   row   tap   red   oca   mamicode   exe   rest   

技术图片

16年开始就在写Spring Boot相关的文章了,之前一直是在自己猿天地的博客上发布,今年开始维护公众号,大部分时间都在写新的文章。

一周能保持一篇原创的文章就已经很不错了,毕竟精力有限,在没有出新文章的时候就想着把之前写的文章分享出来,给正在入门学习Spring Boot的朋友。

Spring Boot作为微服务框架,从最根本上来讲,Spring Boot就是一些库的集合,集成了各种Spring的子项目,它能够被任意项目的构建系统所使用。

解决了平时开发中搭建项目框架的难度,非常方便。

搭建的步骤也是很简单的哈,首先创建个maven工程,然后配置pom文件

文件中配置Spring Boot版本信息以及成jar包的插件。

properties中的java.version是指定jdk的版本。

关于Spring Boot的版本大家可以用最新的,目前最新的版本已经到了2.0了。


<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<properties>
    <java.version>1.8</java.version>
</properties>

还需要别的什么配置吗,以前都要配置一些什么junit啊,各种spring的包啊,commons包啊之类的。

现在完全不需要了,是不是很简单,很方便。

贴一个官网给的列子:

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

我们可以看到所有的配置都被注解代替了,启动程序也可以直接用main方法启动了,框架里有内置的web容器,我们不在需要把程序丢到tomcat里面去部署了,不然怎么体现为服务框架的魅力。

接下来我们运行这个main方法可以看到输出了一大片日志信息。最有趣的还要属这个图形了。

  .   ____          _            __ _ _
 /\\ / ___‘_ __ _ _(_)_ __  __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  ‘  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.1.RELEASE)

我们在浏览器中访问http://localhost:8080就能看到输出的Hello World!
下面讲下配置信息

配置的话默认的配置信息都在application.properties中,自己建个application.properties文件放在classpath中

比如这边默认启动的端口是8080, 我们改成80的配置如下:

#tomcat port
server.port=80

还有就是项目的context path默认是/, 也是可以改的

#the context path, defaults to ‘/‘
server.context-path=/spring-boot/

改成上面的配置信息后我们在访问刚刚的地址就变成了http://localhost/spring-boot

上面官方给的列子我们可以看到只是启动Example这一个类而已,往往开发中我们肯定加载的是多个类

下面给出一个简单的列子,以学生来说明

我们先按照这个层次建下包

com
 +- example
     +- spring-boot
         +- Application.java
         |
         +- domain
         |   +- Student.java
         |
         +- service
         |   +- StudentService.java
         |   +- StudentServiceImpl.java
         |
         +- web
             +- StudentController.java

我们的Application类是程序的入口,负责加载所有信息

通过ComponentScan扫描类,最简单的是只加@SpringBootApplication注解就可以了,@SpringBootApplication中已经包含了下面的注解。

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

业务类,这边先不涉及数据库,就在业务层模拟了一条数据返回。

public interface StudentService {

    List<Student> queryStudents();

}

@Service
public class StudentServiceImpl implements StudentService {

    @Override
    public List<Student> queryStudents() {
        return Arrays.asList(new Student("1001", "yinjihuan"));
    }

}

实体类

public class Student {

    private String id;

    private String name;

    public Student() {
        super();
    }

    public Student(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

控制类

@RestController
@EnableAutoConfiguration
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/students")
    Object queryStudents() {
        return studentService.queryStudents();
    }

}

我们在Application中执行main方法启动程序,然后访问我们的students资源

http://localhost/spring-boot/students

可以看到输出的结果

[{"id":"1001","name":"yinjihuan"}]

推荐相关阅读:

  • 《面试题-实现多线程的方式》
  • 《注解面试题-请了解下》
  • 《反射面试题-请了解下》
    更多技术分享请加我微信,我拉你进群进行交流:

Spring Boot系列之环境搭建

标签:request   package   row   tap   red   oca   mamicode   exe   rest   

原文地址:https://blog.51cto.com/14888386/2516388

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!