SpringBoot hello

Spring Boot 的神奇的不是借助于代码生成来实现的,而是通过调节注解来实现的,这是Spring4.x提供的新特性,Spring4.x提倡使用Java配置和注解配置组合,而Spring Boot不需要任何xml配置即可实现Spring的所有配置。
一下一些优点:
①快速构建项目;
②对主流开发框架的无配置集成;
③项目可独立运行,无需外部依赖Servlet容器;
④提供运行时的应用监控;
⑤极大地提高了开发、部署效率;
⑥与云计算的天然集成。
使用开发工具IDEA
创建一个SpringBoot项目

这里写图片描述


这里写图片描述
③选择相关依赖
这里写图片描述
等项目创建完成后,在自己包下创建一个HelloSpringBoot类。

@EnableAutoConfiguration
@RestController
@RequestMapping("/first_step")
public class HelloSpringBoot {
    @GetMapping("/hello") //get请求与 @RequestMapping的结合
    public String helloBoot() {
        return "hello spring boot";
    }

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

这样,一个简单的SpringBoot 之hello程序就完成了,是不是超简单,再也没有了那些繁琐的配置,因为SpringBoot已经默默地替我们完成了。
SpringBoot支持多种的运行方式,下面一一演示。
接下来运行看看
1.直接运行main函数运行

@SpringBootApplication(scanBasePackages = {"priv.hgs"})
public class BootstudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootstudyApplication.class, args);
    }
}

2.在当前项目路径下使用命令行

mvn spring-boot:run

运行成功后再控制台中将会看到如下打印
这里写图片描述

我们是做用浏览器访问下:http://127.0.0.1:8080/first_step/hello

这里写图片描述

ok 这就是我们的第一个SpringBoot程序,还记得我们当初学习SpringMVC,进行第一个Hello的艰辛吗?你一定会想那些配置都去哪了,其实这些都由SpringBoot替我们完成。以前的那么多的依赖关系,现在只需两个即可,剩下的都交给SpringBoot
pom中的依赖:

<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>

Spring 提供了一系列的start Pom 来简化Maven的依赖,上述两个start的依赖分别用于web和测试,将会引入所需的所有依赖包,你无需再一个一个的添加依赖。

3.使用jar进行运行
我们在创建Springboot程序时,你有没注意到有个Packaging选项,我选择了jar。你一定会问,怎么使jar,我们以前的项目部署中都是用war包,没关系jar依然可以运行
我们使用maven进行打包下 在项目目录下:

mvn package

之后我们可以在项目目录的G:\IDEAProject\bootstudy\target (以自己具体的项目目录为准)下看到打包好的jar包。bootstudy-0.0.1-SNAPSHOT.jar
运行看看,直接双击运行或者在命令

java -jar xxxx\bootstudy-0.0.1-SNAPSHOT.jar

启动成功后
这里写图片描述

此时我们再次访问,依然可以得到上面的结果。

4.打包成war文件
可是根据项目的规定,我一定要用war包怎么办,很简单 只需要将maven的pom.xml中的jar改成war,再进行打包即可。

<packaging>war</packaging>

依然在G:\IDEAProject\bootstudy\target目录下的到bootstudy-0.0.1-SNAPSHOT.war包。
当然你也可以在idea的maven快捷入口中进行打包,一样的效果。
这里写图片描述

有没有瞬间爱上SpringBoot,我们再也不用操心那些繁琐的配置了

有趣小贴士

修改banner,我们在启动Spring Boot时会在控制台中看到以下LOGO打印
这里写图片描述

如何修改它呢???
在src/main/resources下新建一个banner.txt ,把你想要输出的内容,放在这即可。
如以下内容

_____________________________________________________
|           * * * * *                               |
|         *                                         |
|       *                                           |
|          *                                        |
|             * * *                                 |
|                     *                             |
|                       *                           |
|                     *                             |
|          * *  *  *        p r i n g   Boot        |
|                                                   |
_____________________________________________________

运行时即可打印出此logo,你可以试试。

SpringBoot 1.5.7版提供的starter有:

包名starter版本
org.springframework.bootspring-boot-starter1.5.7.RELEASE
org.springframework.bootspring-boot-starter-activemq1.5.7.RELEASE
org.springframework.bootspring-boot-starter-actuator1.5.7.RELEASE
org.springframework.bootspring-boot-starter-amqp1.5.7.RELEASE
org.springframework.bootspring-boot-starter-aop1.5.7.RELEASE
org.springframework.bootspring-boot-starter-artemis1.5.7.RELEASE
org.springframework.bootspring-boot-starter-batch1.5.7.RELEASE
org.springframework.bootspring-boot-starter-cache1.5.7.RELEASE
org.springframework.bootspring-boot-starter-cloud-connectors1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-cassandra1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-couchbase1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-elasticsearch1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-gemfire1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-jpa1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-ldap1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-mongodb1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-neo4j1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-redis1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-rest1.5.7.RELEASE
org.springframework.bootspring-boot-starter-data-solr1.5.7.RELEASE
org.springframework.bootspring-boot-starter-freemarker1.5.7.RELEASE
org.springframework.bootspring-boot-starter-groovy-templates1.5.7.RELEASE
org.springframework.bootspring-boot-starter-hateoas1.5.7.RELEASE
org.springframework.bootspring-boot-starter-integration1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jdbc1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jersey1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jetty1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jooq1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jta-atomikos1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jta-bitronix1.5.7.RELEASE
org.springframework.bootspring-boot-starter-jta-narayana1.5.7.RELEASE
org.springframework.bootspring-boot-starter-log4j21.5.7.RELEASE
org.springframework.bootspring-boot-starter-logging1.5.7.RELEASE
org.springframework.bootspring-boot-starter-mail1.5.7.RELEASE
org.springframework.bootspring-boot-starter-mobile1.5.7.RELEASE
org.springframework.bootspring-boot-starter-mustache1.5.7.RELEASE
org.springframework.bootspring-boot-starter-remote-shell1.5.7.RELEASE
org.springframework.bootspring-boot-starter-security1.5.7.RELEASE
org.springframework.bootspring-boot-starter-social-facebook1.5.7.RELEASE
org.springframework.bootspring-boot-starter-social-linkedin1.5.7.RELEASE
org.springframework.bootspring-boot-starter-social-twitter1.5.7.RELEASE
org.springframework.bootspring-boot-starter-test1.5.7.RELEASE
org.springframework.bootspring-boot-starter-thymeleaf1.5.7.RELEASE
org.springframework.bootspring-boot-starter-tomcat1.5.7.RELEASE
org.springframework.bootspring-boot-starter-undertow1.5.7.RELEASE
org.springframework.bootspring-boot-starter-validation1.5.7.RELEASE
org.springframework.bootspring-boot-starter-web1.5.7.RELEASE
org.springframework.bootspring-boot-starter-web-services1.5.7.RELEASE
org.springframework.bootspring-boot-starter-websocket1.5.7.RELEASE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值