SpringBoot 介绍 常用注解介绍

本文介绍了SpringBoot的主要特点,如自动配置、起步依赖、版本冲突解决和内置Tomcat等。通过一个简单的SpringBoot项目实例展示了如何创建启动类和测试控制器。此外,还提到了一些常用注解,如@ConfigurationProperties、@ComponentScan、@Import和@Bean,并简述了其作用。
摘要由CSDN通过智能技术生成

SpringBoot是一个基于spring的框架,简化开发配置,提高开发效率。

特点:

1,尽可能的自动配置

2,提供起步依赖的简化配置

3,解决版本冲突问题

4,直接嵌入tomcat等其他servlet容器

5,不需要xml配置


中文手册

二、入门 · Spring Boot 中文文档

简单使用

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.angen</groupId>
    <artifactId>springboot_test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动类

package com.angen;

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


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

测试

package com.angen.controller;

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

@RestController
@RequestMapping("hello")
public class HelloController {
    @GetMapping("/")
    public String index(){
        return "hello";
    }
}

或者使用idea创建springboot项目

 下一步

想要哪些依赖自己选

 然后创建就ok了


常用注解介绍

@ConfigurationProperties

将配置文件与类的属性进行映射

prefix属性可以设置前缀

@ComponentScan

加在类上自动扫描本包以及下面的子包

@Import

1,直接导入Bean

2,导入配置类

3,导入ImportSelector的实现类,通常用于加载配置文件中的Bean

4,导入ImportBeanDefinitionRegistrar实现类

@AutoConfigurationPackage

自动包扫描

@EnableConfigurationProperties

如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。
@EnableConfigurationProperties 是把指定类的属性又注入了一次。

@Bean

就相当于xml配置文件中的bean标签,和@Component等一样,把方法交给Spring核心容器管理。

该注解可以自动注入又spring核心容器管理的类的参数 ,@Bean修饰的方法参数的注入方式:

方法参数默认注入方式为

@Autowired
private User user;
上面代码可以不用写

可以通过参数自动注入
@Bean
public void test(User user){
}

@Conditional

条件注解,满足条件就继续注册到ioc容器

参考:Spring的Condition接口常用注解实现方式01_Volunteer Technology的博客-CSDN博客 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值