@SpringbootApplication注释是什么意思?

引入你需要的依赖,我这里引入了spring web,web项目要运行,它必不可少,点击next,后面是路径和名字的配置,可以自己填写,最后点击finsh,springboot项目就建好了。

请看新建项目的pom文件:

<?xml version="1.0" encoding="UTF-8"?>

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.6.RELEASE

com.ymy

first-springboot

0.0.1-SNAPSHOT

first-springboot

Demo project for Spring Boot

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

我们再来看一下项目的整个结构:

这里我们写一个测试controller:TestController

package com.ymy.controller;

import org.springframework.web.bind.annotation.GetMapping;

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

@RestController

public class TestController {

@GetMapping(value = “/test”)

public String test(){

return “HELLO-BUG!!!”;

}

}

代码很简单,就是一个普通的get请求,那现在如何启动项目呢?

2.启动项目

找到FirstSpringbootApplication这个类,你会发现这个类里面有一个main函数,没错,运行main函数即可。

package com.ymy;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class FirstSpringbootApplication {

public static void main(String[] args) {

SpringApplication.run(FirstSpringbootApplication.class, args);

}

}

这里还需要说明一点,我到目前为止并没有指定项目的端口号,那我启动的时候会不会报错呢?我们来看:

我们并没有指定tomcat和端口,为什么执行main函数的时候会加载这两个?这里都是springboot默认给我配置的,再不配置的情况下默认使用toamcat与端口8080,你可以将tomcat改成jetty。

下面我们来测试一下,浏览器输入:localhost:8080/test

到此,springboot项目就算搭建成功了,不知道你有没有发现在启动类中有这么一个注解:@SpringBootApplication,知道他代表者什么吗?

@SpringBootApplication


不知道你们有没有接触过springboot1.5之前的版本,如果接触过的话,应该会对这三个注解很熟:@SpringBootConfiguration、@ComponentScan、@Configuration,这三个注解构成了springboot启动的必要条件。

1.@SpringBootConfiguration:负责激活springboot自动装配机制。

2.@ComponentScan:激活@Component扫描。

3.@Configuration:声明被标注为配置类。

这三个注解让我们省去了大量的配置文件,但是在springboot1.5之后的版本基本上就看不到他们了,代替他们的是:@SpringBootApplication,它实现了上面三个注解实现的所有功能,我们一起来看一下这个神奇的注解:

这是 @SpringBootApplication注解的源码,注意我标记出来的三个注解,其中有两个是前面讲到的三个注解中的两个相同,那还有一个@Configuration注解在哪里实现的?注意看@SpringBootConfiguration这个注解:

藏得有点深,但最终还是引用了 @Configuration注解,所以@SpringBootApplication注解等同于@SpringBootConfiguration、@ComponentScan、@Configuration。

但是,有一点需要注意一下,那就是@SpringBootApplication中的@ComponentScan并非使用了默认值,它添加了排除的FilterType实现:TypeExcludeFilter与AutoConfigurationExcludeFilter,第一个是spring boot1.4引入的用于查找BeanFactory中已经注册的TypeExcludeFilter Bean,作为代理对象。

TypeExcludeFilter源码:

/*

  • Copyright 2012-2019 the original author or authors.

  • Licensed under the Apache License, Version 2.0 (the “License”);

  • you may not use this file except in compliance with the License.

  • You may obtain a copy of the License at

  •  https://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software

  • distributed under the License is distributed on an “AS IS” BASIS,

  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  • See the License for the specific language governing permissions and

  • limitations under the License.

*/

package org.springframework.boot.context;

import java.io.IOException;

import java.util.Collection;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.BeanFactoryAware;

import org.springframework.beans.factory.ListableBeanFactory;

import org.springframework.core.type.classreading.MetadataReader;

import org.springframework.core.type.classreading.MetadataReaderFactory;

import org.springframework.core.type.filter.TypeFilter;

/**

  • Provides exclusion {@link TypeFilter TypeFilters} that are loaded from the

  • {@link BeanFactory} and automatically applied to {@code SpringBootApplication}

  • scanning. Can also be used directly with {@code @ComponentScan} as follows:

  • @ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))

  • Implementations should provide a subclass registered with {@link BeanFactory} and

  • override the {@link #match(MetadataReader, MetadataReaderFactory)} method. They should

  • also implement a valid {@link #hashCode() hashCode} and {@link #equals(Object) equals}

  • methods so that they can be used as part of Spring test’s application context caches.

  • Note that {@code TypeExcludeFilters} are initialized very early in the application

  • lifecycle, they should generally not have dependencies on any other beans. They are

  • primarily used internally to support {@code spring-boot-test}.

  • @author Phillip Webb

  • @since 1.4.0

*/

public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {

private BeanFactory beanFactory;

@Override

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

this.beanFactory = beanFactory;

}

@Override

public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)

throws IOException {

if (this.beanFactory instanceof ListableBeanFactory && getClass() == TypeExcludeFilter.class) {

Collection delegates = ((ListableBeanFactory) this.beanFactory)

.getBeansOfType(TypeExcludeFilter.class).values();

for (TypeExcludeFilter delegate : delegates) {

if (delegate.match(metadataReader, metadataReaderFactory)) {

return true;

}

}

}

return false;

}

@Override

public boolean equals(Object obj) {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)

一线互联网大厂Java核心面试题库

image

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)

[外链图片转存中…(img-mh0I6syI-1713808063870)]

一线互联网大厂Java核心面试题库

[外链图片转存中…(img-HSaPlBHr-1713808063871)]

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值