首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot系列之环境搭建

Spring Boot系列之环境搭建

作者头像
猿天地
发布2018-07-25 17:42:41
4950
发布2018-07-25 17:42:41
举报
文章被收录于专栏:猿天地猿天地

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"}]

推荐相关阅读:

  • 《面试题-实现多线程的方式》
  • 《注解面试题-请了解下》
  • 《反射面试题-请了解下》

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 猿天地 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档