使用springBoot進行快速開發


springBoot項目是spring的一個子項目,使用約定由於配置的思想省去了以往在開發過程中許多的配置工作(其實使用springBoot並不是零配置,只是使用了注解完全省去了XML文件的配置),達到了開箱即用的目的使我們專注於業務邏輯的快速開發

一下的demo是我總結網上的一些配置自己實踐過得來的:

我們使用maven來作為依賴管理的工具,首先,我們使用maven-archtype-quickstart模板建立一個普通java項目 以下是pom.xml文件的內容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jiaoyiping</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 根據自己的需要決定是打jar包還是war包-->
<packaging>war</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>

以rest方式的接口開發為例,如果我們不需要用打war包的方式進行部署(pom中打包方式要改成jar) 可以通過main()方法來運行:

package com.jiaoyiping.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiaoyiping.springboot.service.MailService;

/**
 * Hello world!
 *
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RequestMapping(value = "/hello")
public class App{
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {

        return "hello world";

    }public static void main(String[] args) {

        SpringApplication.run(App.class, args);

    }
}

啟動項目、訪問http://localhost:8080/hello/hello 就可以訪問我們定義的接口

 

如果需要通過打包的方式在web容器中進行部署,則需要繼承 SpringBootServletInitializer 覆蓋configure(SpringApplicationBuilder)方法

代碼如下:

package com.jiaoyiping.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiaoyiping.springboot.service.MailService;

/**
 * Hello world!
 *
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RequestMapping(value = "/hello")
public class App extends SpringBootServletInitializer {
    @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { return application.sources(App.class); }

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {

        return "hello world";

    }
}

 

需要注意一下幾點:

1.jar包中的打包方式根據自己的需要進行修改

2.若打包成war包,則需要繼承 org.springframework.boot.context.web.SpringBootServletInitializer類,覆蓋其config(SpringApplicationBuilder)方法

3.打包成war的話,如果打包之后的文件中沒有web.xml文件的話自己可以加進去一個最簡單的web.xml(只有根節點的定義,而沒有子元素),防止因缺乏web.xml文件而部署失敗

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM