Spring Boot核心配置

Spring Boot核心配置

一、基本配置

1、入口类和@SpringBootApplication

Spring Boot核心配置_第1张图片

@SpringApplication是Spring Boot的核心注解,是一个组合注解。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "exclude"
    )
    Class[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "excludeName"
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class[] scanBasePackageClasses() default {};
}
2、设置exclude属性值,关闭特定的自动配置

Spring Boot核心配置_第2张图片

3、定制Banner
(1)在resources目录下创建banner.txt文件

Spring Boot核心配置_第3张图片

(2)通过http://patorjk.com/software/taag网站生成字符

Spring Boot核心配置_第4张图片

(3)将网站生成的字符复制到banner.txt文件里

Spring Boot核心配置_第5张图片

(4)启动应用程序,查看启动图案

Spring Boot核心配置_第6张图片

4、关闭Banner
Spring Boot核心配置_第7张图片
5、Spring Boot配置文件application.properties
(1)修改Tomcat的端口号

Spring Boot核心配置_第8张图片

(2)修改入口类BootDemoApplication

Spring Boot核心配置_第9张图片

(3)启动程序,访问 http://localhost:9090

Spring Boot核心配置_第10张图片

在application.properties文件里注释掉修改服务器端口号的语句,于是服务器端口号恢复成默认的8080。

6、使用XML配置
(1)创建User实体类

Spring Boot核心配置_第11张图片

package net.hw.bean;

/**
 * Created by howard on 2017/3/31.
 */
public class User {
    private int id;
    private String name;
    private String gender;
    private String age;
    private String telephone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age='" + age + '\'' +
                ", telephone='" + telephone + '\'' +
                '}';
    }
}
(2)创建Spring配置文件spring-config.xml

Spring Boot核心配置_第12张图片

(3)修改入口类,导入spring-config.xml配置文件

Spring Boot核心配置_第13张图片

(4)启动程序,访问http://localhost:8080

Spring Boot核心配置_第14张图片

其实,也可以创建Spring MVC配置文件,在里面定义内部资源视图解析器。

二、访问静态资源
静态资源(css、images、scripts)都放在resources\static目录里。
下面以访问图片为例说明静态资源的访问。
1、在static里创建images目录,拷贝一张图片

Spring Boot核心配置_第15张图片

2、修改入口类BootDemoApplication

Spring Boot核心配置_第16张图片

3、启动程序,访问 http://localhost:8080

Spring Boot核心配置_第17张图片

三、访问JSP资源
Spring Boot建议采用的模板引擎包括Thymeleaf, Freemarker, Groovy, Velocity,官方不建议使用JSP页面,当然要使用也可以,但是必须将JSP页面放在跟resources同级的webapp目录里。
1、创建webapp目录
2、在webapp里创建WEB-INF目录
3、在WEB-INF里创建views目录

Spring Boot核心配置_第18张图片

4、在pom.xml文件里添加对jsp的支持

   org.apache.tomcat.embed
   tomcat-embed-jasper


   javax.servlet
   jstl
5、在views目录里创建起始页面index.jsp

Spring Boot核心配置_第19张图片

6、在static/css目录创建main.css样式表文件

Spring Boot核心配置_第20张图片

7、在resources目录创建spring-mvc-config.xml文件

Spring Boot核心配置_第21张图片

8、在入口类上通过注解加载spring-mvc-config.xml配置文件

Spring Boot核心配置_第22张图片

9、在webmvc子包里创建HelloController

Spring Boot核心配置_第23张图片

10、启动程序,访问 http://localhost:8080/hello

Spring Boot核心配置_第24张图片

关于JSP页面的映射,还有另外两种方式可以实现。
1、通过配置类来实现映射
(1)在config子包里创建JspConfiguration配置类

Spring Boot核心配置_第25张图片

(2)修改入口类,不用加载spring-mvc-config.xml配置文件

Spring Boot核心配置_第26张图片

(3)启动程序,访问访问 http://localhost:8080/hello

Spring Boot核心配置_第27张图片

2、通过设置application.properties文件

Spring Boot核心配置_第28张图片

3、将JspConfiguration配置类里定义内部资源视图解析器给注释掉

Spring Boot核心配置_第29张图片

4、 启动程序,访问访问 http://localhost:8080/hello

Spring Boot核心配置_第30张图片

四、使用Freemarker模板引擎
1、创建book.properties属性文件

Spring Boot核心配置_第31张图片

2、在templates里创建模板文件showBook.ftl

Spring Boot核心配置_第32张图片




    显示图书信息


编号 ${bookId}
书名 ${bookName}
作者 ${bookAuthor}
单价 ${bookPrice}
出版社 ${bookPress}
3、在webmvc里创建BookController

Spring Boot核心配置_第33张图片

package net.hw.webmvc;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by howard on 2017/3/31.
 */
@Controller
@PropertySource("classpath:book.properties")
public class BookController {
    @Value("${book.id}")
    private String bookId;
    @Value("${book.name}")
    private String bookName;
    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.price}")
    private double bookPrice;
    @Value("${book.press}")
    private String bookPress;

    @RequestMapping("/showBook")
    public String showBook(ModelMap map) {
        map.addAttribute("bookId", bookId);
        map.addAttribute("bookName", bookName);
        map.addAttribute("bookAuthor", bookAuthor);
        map.addAttribute("bookPrice", bookPrice);
        map.addAttribute("bookPress", bookPress);
        return "showBook";
    }
}
4、在pom.xml文件增加jar包依赖

   org.springframework.boot
   spring-boot-starter-freemarker
   1.5.2.RELEASE
5、启动程序,访问 http://localhost:8080/showBook

Spring Boot核心配置_第34张图片

五、Spring Boot项目打包(jar形式)
1、配置Maven的环境变量

Spring Boot核心配置_第35张图片Spring Boot核心配置_第36张图片

Spring Boot核心配置_第37张图片

Spring Boot核心配置_第38张图片

2、在Intellij里切换到Terminal
发布 mvn package命令:

Spring Boot核心配置_第39张图片Spring Boot核心配置_第40张图片

3、进入target子目录,发布dir命令

Spring Boot核心配置_第41张图片

4、命令行方式运行程序

Spring Boot核心配置_第42张图片

(1)访问 http://localhost:9090

Spring Boot核心配置_第43张图片

访问 http://localhost:8080 ,也是同样结果:

Spring Boot核心配置_第44张图片

(2)访问 http://localhost:9090/hello

Spring Boot核心配置_第45张图片

但是访问 http://localhost:8080/hello 就没问题。

Spring Boot核心配置_第46张图片

(3)访问 http://localhost:9090/showBook

Spring Boot核心配置_第47张图片

此时,访问http://localhost:8080/showBook,同样结果:

Spring Boot核心配置_第48张图片

六、外部配置
Spring Boot允许使用properties文件或者命令行参数作为外部配置
1、命令行参数配置
Spring Boot可以基于jar包运行。
格式:java -jar xx.jar
还可通过命令参数修改Tomcat端口号。
java -jar xx.jar --server.port=9090

Spring Boot核心配置_第49张图片

Spring Boot核心配置_第50张图片Spring Boot核心配置_第51张图片

2、常规属性配置
在Spring Boot里,在application.properties文件里定义属性,直接使用@Value注入即可。如果在其它属性文件里定义属性,就需要添加@PropertySource注解来指定属性文件的位置。

(1)在application.properties里定义属性

Spring Boot核心配置_第52张图片

(2)在webmvc子包里创建ProductController

Spring Boot核心配置_第53张图片

package net.hw.webmvc;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by howard on 2017/3/31.
 */
@RestController
public class ProductController {
    @Value("${product.id}")
    private int id;
    @Value("${product.name}")
    private String name;
    @Value("${product.price}")
    private double price;

    @RequestMapping("/showProduct")
    public String showProduct() {
        return "产品编号:" + id 
                + "
产品名称:" + name + "
产品单价:" + price; } }
(3)运行程序,访问 http://localhost:8080/showProduct

Spring Boot核心配置_第54张图片

乱码一般是编码不一致导致的,我们在ProductController里通过@Value注解符读取application.properties文件里定义的属性,因此,我们去看看属性文件的编码形式。

Spring Boot核心配置_第55张图片

原来属性文件采用GBK编码,需要修改成UTF-8。

Spring Boot核心配置_第56张图片


此时,application.properties里就会出现乱码:

Spring Boot核心配置_第57张图片

删掉乱码,重新输入产品名:

Spring Boot核心配置_第58张图片

重新启动程序,访问 http://localhost:8080/showProduct

Spring Boot核心配置_第59张图片

2、类型安全的配置(基于properties)
Spring Boot提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个Bean及其属性关联,从而实现类型安全的配置。
(1)在application.properties里定义属性

Spring Boot核心配置_第60张图片

(2)在bean子包里创建Student(类型安全的Bean)

Spring Boot核心配置_第61张图片

package net.hw.bean;

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

/**
 * Created by howard on 2017/3/31.
 */
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private int id;
    private String name;
    private String gender;
    private int age;
    private String telephone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }   
}
(3)在webmvc子包里创建StudentController

Spring Boot核心配置_第62张图片

package net.hw.webmvc;

import net.hw.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by howard on 2017/3/31.
 */
@RestController
public class StudentController {
    @Autowired
    private Student student;

    @RequestMapping("/showStudent")
    public String showStudent() {
        return "学号:" + student.getId()
                + "
姓名:" + student.getName() + "
性别:" + student.getGender() + "
年龄:" + student.getAge() + "
电话:" + student.getTelephone(); } }
(4)启动程序,访问 http://localhost:8080/showStudent

Spring Boot核心配置_第63张图片

3、日志配置
默认情况下,Spring Boot采用Logback作为日志框架。

Spring Boot核心配置_第64张图片

运行程序,查看日志文件:

Spring Boot核心配置_第65张图片Spring Boot核心配置_第66张图片

4、Profile配置
Profile是Spring用来支持针对不同的环境提供不同的配置。
全局Profile配置使用application-{profile}.properties。
(1)创建生产环境下的配置文件application-prod.properties

Spring Boot核心配置_第67张图片

(2)创建开发环境下的配置文件application-dev.properties

Spring Boot核心配置_第68张图片

(3)在application.properties文件里设置当前环境配置

Spring Boot核心配置_第69张图片

(4)运行程序,访问 http://localhost:9999

Spring Boot核心配置_第70张图片Spring Boot核心配置_第71张图片Spring Boot核心配置_第72张图片

七、Spring Boot运行原理
Spring Boot关于自动配置的源码在spring-boot-autoconfigurer-1.5.2.RELEASE.jar内,主要包含了如下图所示的配置:

Spring Boot核心配置_第73张图片

查看当前项目中已启用和未启用的自动配置的报告。
(1)运行jar时增加--debug参数:
(2)在application.properties中设置属性

Spring Boot核心配置_第74张图片

启动程序,查看控制台。
已启用的自动配置:

Spring Boot核心配置_第75张图片

未启用的自动配置:

Spring Boot核心配置_第76张图片

(一)运作原理
@SpringBootApplication注解是一个组合注解,核心功能是由 @EnableAutoConfiguration 注解提供的。

其源码如下:

Spring Boot核心配置_第77张图片

这里的关键功能是 @Import 注解导入的配置功能, EnableAutoConfigurationImportSelector 使用SpringFactoriesLoader.loadFactoryNames方法扫描具有META-INF/spring.factories文件的jar包,而我们在spring-boot-autoconfigurer-1.5.2.RELEASE.jar里就有一个 spring.factories 文件,此文件中声明了有哪些自动配置:

Spring Boot核心配置_第78张图片

Spring Boot核心配置_第79张图片

(二)核心注解
打开上面任意一个AutoConfiguration文件,一般都有下面的条件注解。

Spring Boot核心配置_第80张图片

我们不妨来看一看@ConditionalOnWebApplicaiton注解。

Spring Boot核心配置_第81张图片

此注解使用的条件是OnWebApplicationCondition,分析其源码:
package org.springframework.boot.autoconfigure.condition;

import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Builder;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StandardServletEnvironment;

@Order(-2147483628)
class OnWebApplicationCondition extends SpringBootCondition {
    private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context.support.GenericWebApplicationContext";

    OnWebApplicationCondition() {
    }

    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        boolean required = metadata.isAnnotated(ConditionalOnWebApplication.class.getName());
        ConditionOutcome outcome = this.isWebApplication(context, metadata, required);
        return required && !outcome.isMatch()?ConditionOutcome.noMatch(outcome.getConditionMessage()):(!required && outcome.isMatch()?ConditionOutcome.noMatch(outcome.getConditionMessage()):ConditionOutcome.match(outcome.getConditionMessage()));
    }

    private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata, boolean required) {
        Builder message = ConditionMessage.forCondition(ConditionalOnWebApplication.class, new Object[]{required?"(required)":""});
        if(!ClassUtils.isPresent("org.springframework.web.context.support.GenericWebApplicationContext", context.getClassLoader())) {
            return ConditionOutcome.noMatch(message.didNotFind("web application classes").atAll());
        } else {
            if(context.getBeanFactory() != null) {
                String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
                if(ObjectUtils.containsElement(scopes, "session")) {
                    return ConditionOutcome.match(message.foundExactly("\'session\' scope"));
                }
            }

            return context.getEnvironment() instanceof StandardServletEnvironment?ConditionOutcome.match(message.foundExactly("StandardServletEnvironment")):(context.getResourceLoader() instanceof WebApplicationContext?ConditionOutcome.match(message.foundExactly("WebApplicationContext")):ConditionOutcome.noMatch(message.because("not a web application")));
        }
    }
}
(三)实例分析
了解了Spring Boot的运作原理和主要的条件注解后,现在来分析一个简单的Spring Boot内置的自动配置功能:http的编码配置。

我们在常规Spring项目中配置http编码时是在web.xml文件里配置一个filter:

    Character Encoding
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        UTF-8
    
    
        forceEncoding
        true
    
自动配置要满足两个条件:
(1)能配置CharacterEncodingFilter这个Bean;
(2)能配置encoding和forceEncoding这两个参数。

Spring Boot核心配置_第82张图片

1、配置参数(通过HttpEncodingProperties)

Spring Boot核心配置_第83张图片

package org.springframework.boot.autoconfigure.web;

import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(
    prefix = "spring.http.encoding"
)
public class HttpEncodingProperties {
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private Charset charset;
    private Boolean force;
    private Boolean forceRequest;
    private Boolean forceResponse;
    private Map mapping;

    public HttpEncodingProperties() {
        this.charset = DEFAULT_CHARSET;
    }

    public Charset getCharset() {
        return this.charset;
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }

    public boolean isForce() {
        return Boolean.TRUE.equals(this.force);
    }

    public void setForce(boolean force) {
        this.force = Boolean.valueOf(force);
    }

    public boolean isForceRequest() {
        return Boolean.TRUE.equals(this.forceRequest);
    }

    public void setForceRequest(boolean forceRequest) {
        this.forceRequest = Boolean.valueOf(forceRequest);
    }

    public boolean isForceResponse() {
        return Boolean.TRUE.equals(this.forceResponse);
    }

    public void setForceResponse(boolean forceResponse) {
        this.forceResponse = Boolean.valueOf(forceResponse);
    }

    public Map getMapping() {
        return this.mapping;
    }

    public void setMapping(Map mapping) {
        this.mapping = mapping;
    }

    boolean shouldForce(HttpEncodingProperties.Type type) {
        Boolean force = type == HttpEncodingProperties.Type.REQUEST?this.forceRequest:this.forceResponse;
        if(force == null) {
            force = this.force;
        }

        if(force == null) {
            force = Boolean.valueOf(type == HttpEncodingProperties.Type.REQUEST);
        }

        return force.booleanValue();
    }

    static enum Type {
        REQUEST,
        RESPONSE;

        private Type() {
        }
    }
}
代码解释:
(1)在application.properties配置http编码时前缀是spring.http.encoding;

(2)默认编码方式为UTF-8,如果修改可使用spring.http.encoding.charset=编码;

Spring Boot核心配置_第84张图片Spring Boot核心配置_第85张图片

2、配置Bean(通过HttpEncodingAutoConfiguration)

通过调用上述配置,并根据条件配置CharacterEncodingFilter的Bean,源码如下:

Spring Boot核心配置_第86张图片

代码解释:
(1) @EnableConfigurationProperties({HttpEncodingProperties. class }) 启用配置属性;
(2)@ConditionalOnWebApplication 在项目是Web应用的条件下;
(3)@ConditionalOnProperty(
prefix = "spring.http.encoding" ,
value = { "enabled" },
matchIfMissing = true
)
当设置spring.http.encoding=enabled的情况下,如果没有设置则默认为true,即条件符合;
(4)@ConditionalOnMissingBean({CharacterEncodingFilter. class }) 当容器中没有这个Bean的时候就新建Bean。

(四)实战练习
编写一个starter pom,意味着我们不仅有自动配置的功能,而且具有更通用的耦合度更低的配置。
(1)新建starter的Maven项目
Spring Boot核心配置_第87张图片 Spring Boot核心配置_第88张图片 Spring Boot核心配置_第89张图片 Spring Boot核心配置_第90张图片 Spring Boot核心配置_第91张图片

(2)修改pom.xml文件


    4.0.0

    net.hw
    spring-boot-starter-greet
    1.0-SNAPSHOT
    jar

    spring-boot-starter-greet
    http://maven.apache.org

    
        UTF-8
    

    
        
            org.springframework.boot
            spring-boot-autoconfigure
            1.5.2.RELEASE
        
        
            junit
            junit
            3.8.1
            test
        
    

(3)创建src/main/java目录

Spring Boot核心配置_第92张图片Spring Boot核心配置_第93张图片Spring Boot核心配置_第94张图片

(4)创建包net.hw.config和net.hw.service

Spring Boot核心配置_第95张图片

(5)在net.hw.config包里创建属性配置类

Spring Boot核心配置_第96张图片

package net.hw.config;

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

/**
 * Created by howard on 2017/4/2.
 */
@ConfigurationProperties(prefix = "greet")
public class GreetProperties {
    private static String NAME = "World";
    private String name = NAME;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
(6)在net.hw.service包里创建服务类

Spring Boot核心配置_第97张图片

package net.hw.service;

/**
 * Created by howard on 2017/4/2.
 */
public class GreetService {
    private String name;

    public String greet() {
        return "Hello, " + name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
(7)在net.hw包里创建自动配置类

Spring Boot核心配置_第98张图片

package net.hw;

import net.hw.config.GreetProperties;
import net.hw.service.GreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by howard on 2017/4/2.
 */
@Configuration
@EnableConfigurationProperties(GreetProperties.class)
@ConditionalOnClass(GreetService.class)
@ConditionalOnProperty(prefix = "greet", value = "enabled", matchIfMissing = true)
public class GreetServiceAutoConfiguration {
    @Autowired
    private GreetProperties greetProperties;
    
    @Bean
    @ConditionalOnMissingBean(GreetService.class) 
    public GreetService greetService() {
        GreetService greetService = new GreetService();
        greetService.setName(greetProperties.getName());
        return greetService;
    }
}
(8)注册配置
若想自动配置生效,需要注册自动配置类。
在src/main里创建resources目录,在里面创建META-INF/spring.factories。

Spring Boot核心配置_第99张图片Spring Boot核心配置_第100张图片

(9)新建Spring Boot项目

Spring Boot核心配置_第101张图片Spring Boot核心配置_第102张图片Spring Boot核心配置_第103张图片Spring Boot核心配置_第104张图片

(10)通过 mvn install 将项目安装到本地库

Spring Boot核心配置_第105张图片

(11)在pom.xml中添加spring-boot-starter-greet的依赖

   net.hw
   spring-boot-starter-greet
   1.0-SNAPSHOT

Spring Boot核心配置_第106张图片

(12)修改入口类TestMyStarterApplication
package net.hw;

import net.hw.service.GreetService;
import org.springframework.beans.factory.annotation.Autowired;
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
public class TestMyStarterApplication {
   @Autowired
   private GreetService greetService;

   @RequestMapping("/")
   public String home() {
      return greetService.greet();
   }

   public static void main(String[] args) {
      SpringApplication.run(TestMyStarterApplication.class, args);
   }
}
(13)启动程序,访问 http://localhost:8080

Spring Boot核心配置_第107张图片

(14)在applicaion.properties里添加属性

Spring Boot核心配置_第108张图片

(15)启动程序,访问 http://localhost:8080
Spring Boot核心配置_第109张图片

你可能感兴趣的:(Web框架)