SpringBoot 介绍 常用注解介绍

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博客 

posted @ 2022-07-28 18:31  在线电影制作人  阅读(3)  评论(0)    收藏  举报  来源