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

Springboot搭建web项目

时间:2017-06-26 15:35:49      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:自动发现   nts   打包   main方法   bean   服务器端   stc   ide   enable   

姓名:黄于霞     班级:软件151

1、pom配置

首先,建立一个maven项目,修改pom.xml文件,添加parent依赖。

<parent>

   <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

   <version>1.4.2.RELEASE</version>

</parent>

spring-boot-starter-parent会自动为我们引入spring相关的依赖。

再看dependencies节点:

技术分享

  • 我们需要引入starter-web,这是开发web项目必须的依赖,springboot默认集成了tomcat服务器,在这里排除了tomcat,引入了NIO服务器undertow。

  • springboot默认服务器端口8080,可以自行修改,后面会介绍。

  •  视图引擎选择velocity,引入starter-velocity即可,具体配置后面介绍。

  •  引入maven插件:

        <plugin>

             <groupId>org.springframework.boot</groupId>

             <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

 

2、程序入口

    在一级包路径下,比如com.xxx,新建一个Application.java。

技术分享

解释一下注解:

  • @Configuration:指出该类是 Bean 配置的信息源,相当于XML中的<beans></beans>,一般加在主类上。

  • @EnableAutoConfiguration:让 SpringBoot 根据应用所声明的依赖来对 Spring 框架进行自动配置,由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置

  • @ ComponentScan:表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件(@Component , @Service , @Repository , @Controller 等),包括@Configuration类。

  • @SpringBootApplication: @EnableAutoConfiguration、@ComponentScan和@Configuration的合集。

  • @ EnableTransactionManagement:启用注解式事务。

 

3、配置

    在项目resources目录下新建application.properties文件,springboot默认会读取该配置文件,当然你也可以创建一个名为application.yml文件。

4、控制器

    控制器依然使用@Controller注解,或者@RestController(返回json,Controller和ResponseBody合体),我们在templates下新建一个index.vm视图文件,输出hello,world!

 

5、打包,启动

    使用mvn clean package将应用打成一个jar包,比如test.jar。

    在命令行执行命令:java -jar test.jar(也可以在IDE中直接执行main方法)

技术分享

 

在浏览器输入localhost:8081/test/看一下效果:

技术分享

 

6、总结:

  • 优点:简化配置,快速构建应用。

  • 缺点:坑很多啊,踩过才知道,对spring平台不了解,所以还是老老实实的自己配置。

 

 

 

Springboot搭建web项目

标签:自动发现   nts   打包   main方法   bean   服务器端   stc   ide   enable   

原文地址:http://www.cnblogs.com/hyxz/p/7080753.html

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