spring Boot学习(一)

spring Boot简介

spring boot是一款轻量级的spring框架。该框架避免了以往框架中众多的xml配置文件,采用注解方式进行配置,使得项目简洁易于理解,减少了上手难度。

spring Boot特点

1. 创建独立的Spring应用程序

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

3. 简化Maven配置

4. 自动配置Spring

5. 提供生产就绪型功能,如指标,健康检查和外部配置

6. 绝对没有代码生成和对XML没有要求配置

项目搭建

maven项目结构

project

-----src

-----------main

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

@SpringBootApplication
@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
public class HelloController {

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

运行结果:

PS:在application.properties中添加配置项server.port可以调整项目的端口号

server.port=8080
#server.context-path=/api

属性配置

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();
    }

}

运行结果为:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值