[读书笔记] 一、Spring boot项目搭建与配置文件

读书笔记:[JavaEE开发的颠覆者 Spring Boot实战] 作者:汪云飞

从今天开始坚持读书,并记录下此读书笔记。

一,初接触

Spring boot 项目Hello world搭建

1.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.springboot</groupId>
    <artifactId>springboot_test1_1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_test1_1</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
pom.xml

2.main 入口类如下:

package com.springboot.springboot_test1_1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// @SpringBootApplication 是Spring boot 项目的核心注解
// 它是一个组合注解,主要组合了@Configuration,@EnableAutoConfiguration,@ComponentScan
// 若不使用@SpringBootApplication,直接使用@Configuration,@EnableAutoConfiguration,@ComponentScan也可

// tips1:
// @EnableAutoConfiguration : 让Spring boot根据类路径中的jar包依赖为当前项目进行自动配置
// tips2:
// Spring boot会自动扫描@SpringBootApplication注解所在类的同级与下级包里的Bean,故一般让@SpringBootApplication注解所在类放到顶级目录
@SpringBootApplication
public class App {
    // main 方法作为启动入口
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @RequestMapping("/")
    public String index() {
        return "Hello Spring Boot!";
    }
}
App.java

3.运行main class,浏览器访问:http://localhost:8080/

输出:

Hello Spring Boot!

成功了。

二,properties配置文件

1.使用application.properties配置文件

Spring Boot 可使用application.properties配置文件,例如使用application.properties修改tomcat的端口号与访问路径:

application.properties:

server.port=9090
server.context-path=/helloboot

浏览器访问:http://localhost:9090/helloboot/

成功。

2.使用application.properties配置文件中的属性

application.properties:

app.name=spring boot test app

App.java:

package com.springboot.springboot_test1_1;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// @SpringBootApplication 是Spring boot 项目的核心注解
// 它是一个组合注解,主要组合了@Configuration,@EnableAutoConfiguration,@ComponentScan
// 若不使用@SpringBootApplication,直接使用@Configuration,@EnableAutoConfiguration,@ComponentScan也可
// tips1:
// @EnableAutoConfiguration : 让Spring boot根据类路径中的jar包依赖为当前项目进行自动配置
// tips2:
// Spring
// boot会自动扫描@SpringBootApplication注解所在类的同级与下级包里的Bean,故一般让@SpringBootApplication注解所在类放到顶级目录
@SpringBootApplication
public class App {
    // main 方法作为启动入口
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Value("${app.name}")
    private String appName;

    @RequestMapping("/")
    public String index() {
        return "Hello Spring Boot! appName:" + appName;
    }
}
App.java

3.类型安全的配置(基于properties)

application.properties:

author.name=cainiao87
author.age=30

App.java:

package com.springboot.springboot_test1_1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// @SpringBootApplication 是Spring boot 项目的核心注解
// 它是一个组合注解,主要组合了@Configuration,@EnableAutoConfiguration,@ComponentScan
// 若不使用@SpringBootApplication,直接使用@Configuration,@EnableAutoConfiguration,@ComponentScan也可
// tips1:
// @EnableAutoConfiguration : 让Spring boot根据类路径中的jar包依赖为当前项目进行自动配置
// tips2:
// Spring
// boot会自动扫描@SpringBootApplication注解所在类的同级与下级包里的Bean,故一般让@SpringBootApplication注解所在类放到顶级目录
@SpringBootApplication
public class App {
    // main 方法作为启动入口
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Value("${app.name}")
    private String appName;

    @Autowired
    private AuthorSettings setting;

    @RequestMapping("/")
    public String index() {
        return "Hello Spring Boot! appName:" + appName;
    }

    @RequestMapping("/author")
    public String author() {
        return "i'm :" + setting.getName() + ", age :" + setting.getAge();
    }
}
App.java

三,xml配置文件

Spring Boot 不推荐使用xml配置文件,若有些情况必须要求使用:

使用注解:@ImportResource("classpath:xxx-config.xml")

 

目录结构:

 

 四,profile配置

修改application.properties文件,去掉server.port属性,新增属性:

spring.profiles.active=dev

新增2个.properties配置文件:

application-dev.properties:

server.port=8080

application-prod.properties:

server.port=80

这样,通过设置spring.profiles.active=dev或者prod就可切换测试、生产环境配置文件了。

 

posted on 2017-05-05 16:31  懂技术爱生活  阅读(456)  评论(0编辑  收藏  举报

导航