zoukankan      html  css  js  c++  java
  • SpringBoot入门

    1、Background

    最初没有大量的配置,各自为战的”小作坊“式的开发。慢慢的,我们意识到这种方式的弊端。”小作坊“彼此不交流,难以使得资源合理利用,而且水平也不高。

    之后,随着ioc、aop的引入,开发的流程变得标准化、规范化,资源的利用率提升很多。但它也有不足之处,为了规范和整理彼此之间的关系,我们也付出了不小的代价,那就是需要做大量的配置工作。

    这时候,人们开始追求更方便、更灵活的开发模式。

    2、What

    Spring Boot是Spring社区发布的一款开源项目,使用它,你可以更加轻松、快捷的构建项目。

    2.1 与Jfinal

    单从这一点来看,小编最初了解到的是Jfinal。它的核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级。因此,二者的设计理念还是蛮像的。但其中的原理就差很多了,而且jfinal的java config也不必配置文件方便很多。

    2.2 约定大于配置

      Convention Over Configuration 也叫按约定设计,这个宗旨使得软件设计、开发更加的方便。在ORM框架、Maven等中都有很好的体现。
    

    当然,在SpringBoot中,更是利用了这一点。既然大家都不想写这么繁杂的配置信息,那么我就提供一些默认的约定,如果你想遵循其他的约定,就再重新配置。

    2.3 特性

    • 创建独立的Spring应用程序
    • 嵌入Web容器(Tomcat、Jetty等)
    • 简化Maven配置(starter pom)
    • 自动配置Spring
    • 不需要xml配置

    3、How

    这个例子很简单,主要是熟悉一下,后面就介绍注解背后做的工作。

    Step 1:
    pom文件配置

    <!-- 继承Springboot默认配置 -->
       <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.2.RELEASE</version>
        </parent>
    
     <dependencies>
        <!-- 添加Web应用的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <!-- 构建打包 -->
          <build>  
            <plugins>  
                <plugin>  
                    <groupId>org.springframework.boot</groupId>  
                    <artifactId>spring-boot-maven-plugin</artifactId>  
                </plugin>  
            </plugins>  
        </build> 

    代码实现

    1、启动类
    运行项目时,执行运行该类中的main方法即可。

    package com.test.web;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class Configration {
    
        public static void main(String[] args) {
            SpringApplication.run(Configration.class, args);
        }
    }

    2、Controller类

    这里给了两种方式,一种是简单字符串返回的测试,另一种是注入service。

    package com.test.web;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @EnableAutoConfiguration
    public class BaseController {
    
        //注入service
        @Autowired
        BaseService baseService;
    
        @RequestMapping("/hello")
        String home(){
            //return "hello world!";
            return baseService.hello();
        }
    
    }
    

    运行启动类,我们可以看到控制台的输出信息。

    这里写图片描述

    在浏览器中输入 http://localhost:8080/hello

    这里写图片描述

  • 相关阅读:
    判断ImageIcon创建成功
    Node中的explorer views的双击事件
    Oracle数据类型
    Sql三种行转列
    数据库迁移
    并发采集同一站点被封的解决方案
    .net获取版本号的三种方法
    List转DataSet
    Orcale自增长主键
    python学习笔记数字和表达式
  • 原文地址:https://www.cnblogs.com/saixing/p/6730194.html
Copyright © 2011-2022 走看看