(SpringBoot)(四)SpringBoot框架的介绍

由于自己码龄的限制,所见的项目跟各位大佬相比相差甚远。自己从入职老东家就一直在使用SpringBoot框架,期间也自学了Spring以及SpringMVC。发现这两个框架需要自己手动配置的地方太多了,而且大部分都是约定俗成的配置。大家也都听说过一句话:约定大于配置,这句话的大致理解就是比如Java中的系统配置,类库,以及框架应该有一个默认的初始值。我们可以不显式的配置它们,也可以显式的声明它们也就是覆盖原配置。根据这种大量的配置工作,SpringBoot应运而生了。使用Spring Boot可以让我们快速创建一个基于Spring的项目,而让这个Spring项目跑起来我们只需要很少的配置就可以。

SpringBoot的核心优势:
1、它是一个独立运行的Spring项目
2、SpringBoot内嵌了Servlet,因此可以用jar包来运行,只需要java -jar xxx.jar来运行
3、SpringBoot内嵌了Tomcat,无需打成war包,因此方便服务器端的部署
4、提供starter简化Maven配置,只需导入parent的依赖,即可使用大量的Spring工具

说了这么多,其实也没说全。关于它的优劣,网上也有很多介绍,在此不再赘述。下面我们写一个简单的SpringBoot的工程,作为我们这篇文章的案例。

一、项目的搭建

首先创建一个项目,选择Spring Initialzr,勾选指定的jdk(这里以jdk1.8为例)并选择next
在这里插入图片描述
将Group和Artifact填写指定的名称,点击next
在这里插入图片描述
下面的界面上我们可以勾选我们在这个项目中用到的技术,由于是入门级程序,所以我们只勾选Web,选择Spring Web,选择指定的Spring Boot版本,点击next
在这里插入图片描述
将项目的名称以及项目在本地的位置填写到指定的位置中,选择finish
在这里插入图片描述
到此,我们创建SpringBoot项目的工作就做完了。由于会下载SpringBoot的相关依赖包,第一次创建项目的时候,加载的时间会比较长,以后就很快了。

项目创建完以后,首先我们来看一下项目的目录结构,如下图
在这里插入图片描述
在项目的启动类SpringboothelloApplication中,有一个@SpringBootApplication注解,这是整个Spring Boot的核心注解,它的目的就是开启Spring Boot的自动配置。

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

@SpringBootApplication
public class SpringboothelloApplication {

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

}

我们在项目的启动类路径下创建一个类,起名为helloController。如下:
在这里插入图片描述

在这个类上添加一个@RestController注解,使之变为一个Controller,然后里边提供一个地址转换方法,如下:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello springboot";
    }

}

点击如下图位置的启动键,启动SpringBoot程序
在这里插入图片描述
使用IDEA的插件,访问localhost:8080/hello,就可以看到如下界面:(由于我本地8080端口被占用,所以我修改了默认端口为9090)
在这里插入图片描述
我们发现,我们根本没有配置任何地方,项目已经可以在web端进行访问了。那么它为什么能够运行起来呢?我们看一下启动类当中SpringBoot的最核心的一个注解:@SpringBootApplication

它组合了@SpringBootConfiguration、@EnableAutoConfiguration以及@ComponentScan,我们在开发的过程中如果不使用@SpringBootApplication,则可以组合使用这三个注解。这三个注解中,@SpringBootConfiguration表明这个类是一个配置类,@EnableAutoConfiguration则表示让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置,最后一个@ComponentScan的作用我也不赘述了,唯一要注意的是如果我们使用了@SpringBootApplication注解的话,系统会去入口类的同级包以及下级包中去扫描实体类,因此建议入口类的位置在groupId+arctifactID组合的包名下。

@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) })

接下来,我们看到我刚刚修改的端口号,也就是说一说SpringBoot的配置文件:

Spring Boot使用一个全局的配置文件application.properties或者application.yml,配置文件放在src/main/resources目录下。properties是我们常见的一种配置文件,Spring Boot不仅支持properties这种类型的配置文件,也支持yaml语言的配置文件。

在application.properties中修改配置如下:

server.port=9090

在application.yaml中修改配置如下:

server:
  port: 9090

关于获取配置文件键值的方法:(一)

在Spring框架中,我们要使用properties注入指定的值。而在SpringBoot中,这项工作会变得非常的简单。我们只需要在application.properties中定义属性,然后在代码中直接使用@Value注入即可。

在application.properties中添加要注入的值的键值对

name=莫德里奇
age=29
country=Croatia

在Java类中我们可以这样获取它们的值

@Value(value = "${name}")
    private String name;
    @Value(value = "${age}")
    private String age;
    @Value(value = "${country}")
    private String country;

如果不设置的话,中文会出现乱码,我们在application.properties中加入如下配置:

server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

以IEDA为例。选择File----Setting----Editor----File Encodings,将如下圈出的位置改成如图所示
在这里插入图片描述
在helloController类中写一个方法,用于测试获取配置文件中的值,如下:

@RequestMapping(value = "/getValue")
    @ResponseBody
    public String getValue(){
        return name + "---" + age + "---" + country;
    }

启动项目,访问指定路径,结果为:

莫德里奇---29---Croatia

关于获取配置文件键值的方法:(二)

刚刚说的这种方式我们在实际项目中使用的时候工作量略大,因为每个项目要注入的变量的值太多了,这种时候我们可以使用基于类型安全的配置方式,就是将properties属性和一个Bean关联在一起,这样使用起来会更加方便。

在application.properties中添加要注入的值的键值对

player.name=罗纳尔多
player.age=20
player.country=Brazil

创建Java类,命名为PlayerBean,提供getter和setter方法,并使用@ConfigurationProperties注解和@Component注解注入指定的值。其中prefix是指在配置文件中的前缀

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

@Component
@ConfigurationProperties(prefix = "player" )
public class PlayerBean {
    private String name;
    private String age;
    private String country;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

在controller层中添加注入

@Autowired
    private PlayerBean playerBean;

在helloController类中写一个方法,用于测试获取配置文件中的值,如下:

@RequestMapping(value = "/getPropertiesValue")
    public String getPropertiesValue(){
        return playerBean.getName() + "----" + 
        playerBean.getAge() + "----" + playerBean.getCountry();
    }

测试结果如下:

罗纳尔多----20----Brazil

这期关于SpringBoot的东西先说这么多,这点东西断断续续写了一天,中间也是在工作。突然之间发现,其实时间真的就是海绵一样,如果这些时间我没在写博客,可能大部分时间就是在发呆中度过。虽然我写的东西对于很多人来讲,实在是太简单,可能压根觉得看我的文章都是在浪费时间。可是谁不是从这个阶段过来的呢?加油!努力不一定成功,最坏的结果也只是大器晚成!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值