spring Boot學習(一)

spring Boot簡介

spring boot是一款輕量級的spring框架。該框架避免了以往框架中衆多的xml配置文件,採用註解方式進行配置,使得項目簡潔易於理解,減小了上手難度。java

spring Boot特色

1. 建立獨立的Spring應用程序web

2. 嵌入的Tomcat,無需部署WAR文件spring

3. 簡化Maven配置apache

4. 自動配置Springapp

5. 提供生產就緒型功能,如指標,健康檢查和外部配置框架

6. 絕對沒有代碼生成和對XML沒有要求配置maven

項目搭建

maven項目結構

projectspring-boot

-----src測試

-----------mainui

-------------------java.com.xxx...

-------------------resource

----------------------------application.properties

-----------test.java

-----------test.resource

-----pom.xml

例如:

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>
    <artifactId>spring-boot-test</artifactId>
    <groupId>com.rainman</groupId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-boot-test</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

簡單啓動程序

添加啓動程序SimpleRun.java,代碼以下:

package com.rainman;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

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

添加接口

添加啓動程序HelloController.java,代碼以下:

package com.rainman;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "呵呵呵";
    }

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

運行結果:

PS:在application.properties中添加配置項server.port能夠調整項目的端口號

屬性配置

1.添加啓動程序BootRun.java,代碼以下:

package com.rainman;

import com.rainman.conf.ApplicationConf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties({ApplicationConf.class})
public class BootRun {
    public static void main(String[] args){
        SpringApplication.run(BootRun.class, args);
    }
}

2. 添加配置文件ApplicationConf.java,代碼以下:

package com.rainman.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix="conf",locations = "classpath:application.properties")
public class ApplicationConf {
    private String user;
    private String pw;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }
}

3.在配置文件application.properties中添加配置信息,以下

conf.user=hello
conf.pw=hello

4.添加測試接口LoginController.java,代碼以下

package com.rainman.controller;

import com.rainman.conf.ApplicationConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @Autowired
    private ApplicationConf applicationConf;

    @RequestMapping("/login")
    @ResponseBody
    public String login(){
        StringBuffer loginInfo = new StringBuffer("恭喜你,登錄了!!<br>");
        loginInfo.append("userName:"+applicationConf.getUser()+"<br>");
        loginInfo.append("pw:"+applicationConf.getPw()+"<br>");
        return loginInfo.toString();
    }

}

運行結果爲:

相關文章
相關標籤/搜索