欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring-Boot框架初步搭建

程序员文章站 2024-03-05 11:06:12
一、简介 springmvc是非常伟大的框架,开源,发展迅速。优秀的设计必然会划分、解耦。所以,spring有很多子项目,比如core、context、bean、mvc等...

一、简介

springmvc是非常伟大的框架,开源,发展迅速。优秀的设计必然会划分、解耦。所以,spring有很多子项目,比如core、context、bean、mvc等。这对知根底的人来说很简单明了,然而springmvc就是为了傻瓜式的操作而发明的。对于初学springmvc的人来说,想要入手就开发需要拷贝一连串的dependency而不知道这个是干嘛,不知道是不是少了依赖。像我刚接触springmvc的时候到处百度教程而发现各有不同,于是复制了一个又一个代码却不能自己设置,根本原因是不了解各个依赖的包。

spring-boot 正是为了解决繁复的代码配置而产生的。spring-boot 也是基于java-base 开发的代码,及不用xml文件配置,所有代码都由java来完成。还可以加入groovy的动态语言执行。

 二、搭建一个基本的web-mvc 项目

2.1 configure environment

  1. java 1.8+
  2. maven 3.3+
  3. spring-boot 1.3.5
  4. idea 15
  5. thymeleaf 3

2.2 start

在idea中,选择new-》maven创建一个空的maven项目,比如名字springboot-test。

2.2.1pom.xml

设定java版本:

<properties>

    <java.version>1.8</java.version>

</properties> 

添加依赖版本管理dependencymanagement

<dependencymanagement>

    <dependencies>

      <dependency>

        <!-- import dependency management from spring boot -->

        <groupid>org.springframework.boot</groupid>

        <artifactid>spring-boot-dependencies</artifactid>

        <version>1.3.5.release</version>

        <type>pom</type>

        <scope>import</scope>

      </dependency>

    </dependencies>

</dependencymanagement> 

添加spring-web项目依赖

<dependencies>

    <dependency>

      <groupid>org.springframework.boot</groupid>

      <artifactid>spring-boot-starter-web</artifactid>

    </dependency>

  <dependency>

    <groupid>org.springframework.boot</groupid>

    <artifactid>spring-boot-devtools</artifactid>

    <optional>true</optional>

  </dependency>

</dependencies> 

添加build-plugin

<build>

    <plugins>

      <plugin>

        <groupid>org.springframework.boot</groupid>

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

        <configuration>

          <fork>true</fork>

        </configuration>

      </plugin>

    </plugins>

 

</build> 

如此,一个简单的restful的webservice的项目就搭建好了。如果想要支持视图渲染,即jsp、freemark、velocity等,添加对应的依赖即可。比如,我使用thymeleaf模板:

<dependency>

      <groupid>org.springframework.boot</groupid>

      <artifactid>spring-boot-starter-thymeleaf</artifactid>

</dependency> 

2.2.2 创建java代码

如果新建项目的名字是:springboot-test. 则创建包springboot-test/src/main/java/com/test.

com
 +- example
   +- myproject
     +- application.java
     |
     +- domain
     |  +- customer.java
     |  +- customerrepository.java
     |
     +- service
     |  +- customerservice.java
     |
     +- web
       +- customercontroller.java

com.test是我们的基本包名。下面创建配置类com.test.appconfig。

package com.test;

import org.springframework.boot.springapplication;

import org.springframework.boot.autoconfigure.springbootapplication;

 

/**

 * created by miaorf on 2016/6/19.

 */

@springbootapplication

public class appconfig {

  public static void main(string[] args) {

    springapplication.run(appconfig.class);

  }

} 

@springbootapplication 标注启动配置入口,可以发现通过一个main方法启动。使用这个注解的类必须放置于最外层包中,因为默认扫描这个类以下的包。否则需要自己配置@componentscan。 

这样,配置基本完成了。下面开发控制层controller:

创建com.test.controller.hellocontroller。

package com.test.controller; 

import org.springframework.stereotype.controller;

import org.springframework.ui.model;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.responsebody;

 

import java.util.hashmap;

import java.util.map;

 

/**

 * created by miaorf on 2016/6/19.

 */

@controller

public class hellocontroller {

 

  @requestmapping("/index")

  public string index(model model){

 

    model.addattribute("name","ryan");

 

    return "index";

  } 

  @requestmapping("/json")

  @responsebody

  public map<string,object> json(){

    map<string,object> map = new hashmap<string,object>();

    map.put("name","ryan");

    map.put("age","18");

    map.put("sex","man");

    return map;

  }

} 

创建视图代码:

视图默认放在springboot-test\src\main\resources\templates**.

所以创建springboot-test\src\main\resources\templates\index.html

<!doctype html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

  <title>getting started: serving web content</title>

  <meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>

<body>

<p th:text="'hello, ' + ${name} + '!'" />

</body>

</html> 

d:\workspace\springboot\springboot-test\src\main\webapp\hello.html

<!doctype html>

<html>

<head>

  <title>getting started: serving web content</title>

  <meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>

<body>

hello, this is static page. not resolve by server, just the html.

</body>

</html> 

2.2.3 run

启动方式多种,可以启动main方法,也可以通过命令行启动:

d:\temp\springboot-test>mvn spring-boot:run
[info] scanning for projects...
[warning]
[warning] some problems were encountered while building the effective model for com.test:springboot-test:jar:1.0-snapshot
[warning] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 49, column 21
[warning]
[warning] it is highly recommended to fix these problems because they threaten the stability of your build.
[warning]
[warning] for this reason, future maven versions might no longer support building such malformed projects.
[warning]
[info]
[info] ------------------------------------------------------------------------
[info] building springboot-test 1.0-snapshot
[info] ------------------------------------------------------------------------
[info]
[info] >>> spring-boot-maven-plugin:1.3.5.release:run (default-cli) > test-compile @ springboot-test >>>
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot-test ---
[warning] using platform encoding (gbk actually) to copy filtered resources, i.e. build is platform dependent!
[info] copying 2 resources
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot-test ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ springboot-test ---
[warning] using platform encoding (gbk actually) to copy filtered resources, i.e. build is platform dependent!
[info] skip non existing resourcedirectory d:\temp\springboot-test\src\test\resources
[info]
[info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ springboot-test ---
[info] no sources to compile
[info]
[info] <<< spring-boot-maven-plugin:1.3.5.release:run (default-cli) < test-compile @ springboot-test <<<
[info]
[info] --- spring-boot-maven-plugin:1.3.5.release:run (default-cli) @ springboot-test ---
[info] attaching agents: []

 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::    (v1.3.5.release)

浏览器访问:localhost:8080/index

Spring-Boot框架初步搭建

Spring-Boot框架初步搭建

Spring-Boot框架初步搭建

demo下载路径:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。