springboot入门


推荐导航

0.系统学习导航传送
>注解
>入门导航

一.版本与要求

1.Servlet 容器

在这里插入图片描述

2.maven使用

Spring Boot 兼容 Apache Maven 3.2 或更高版本。 Spring Boot 还提供了一个可选的 Maven 插件来创建可执行 jar。

<?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.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</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>

3.gradle使用

Spring Boot 兼容 Gradle 2 (2.9 或者更高版本)和 Gradle 3. Spring Boot 提供了一个有用的 Gradle 插件,可用于简化依赖声明和创建可执行 jar 文件

plugins {
    id 'org.springframework.boot' version '1.5.4.RELEASE'
    id 'java'
}


jar {
    baseName = 'myproject'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    jcenter()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

4.打包和执行

要创建可执行 jar,我们需要将 spring-boot-maven-plugin 添加到 pom.xml 文件中。
在这里插入图片描述

 <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 多个main,指定打包启动类 -->
                <configuration>
                    <mainClass>com.ganyz.bz.BzSpringBootApplication2</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

5.starter

springboot的starter

一、入门hello生成

官网入口

1.idea方式创建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、注解

1.参数校验的注解

  • @NotNull, @NotEmpty, @NotBlank

@NotNull:不能为null,但可以为empty
@NotEmpty:不能为null,而且长度必须大于0
@NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0
注意在使用@NotBlank等注解时,一定要和@valid一起使用,不然@NotBlank不起作用


  • @Validated和@Valid区别

@Validated: Spring Validation验证框架对参数的验证机制提供了(Spring’s JSR-303规范,是标准JSR-303的一个变种) 可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上
@Valid javax提供了(标准JSR-303规范) 可以用在方法、构造函数、方法参数和成员属性(字段)上

总结一下@Validated和@Valid在嵌套验证功能上的区别:
@Validated:用在方法入参上无法单独提供嵌套验证功能。不能用在成员属性(字段)上,也无法提示框架进行嵌套验证。能配合嵌套验证注解@Valid进行嵌套验证。
@Valid:用在方法入参上无法单独提供嵌套验证功能。能够用在成员属性(字段)上,提示验证框架进行嵌套验证。能配合嵌套验证注解@Valid进行嵌套验证。


  • @Min, @Max

@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值

2.springboot相关注解

2.1基础注解

  • @ComponentScan

该注解就是用来自动扫描被这些@Service,@Repository,@Component,@Controller注解标识的类,最终生成ioc容器里的bean;
可以通过设置@ComponentScan 的 basePackages,includeFilters,excludeFilters属性来动态确定自动扫描范围,类型已经不扫描的类型.默认情况下:它扫描所有类型,并且扫描范围是@ComponentScan注解所在配置类包及子包的类


  • @SpringBootConfiguration,@Configuration

这个注解的作用与@Configuration作用相同,都是用来声明当前类是一个配置类.可以通过@Bean注解生成IOC容器管理的bean


  • @EnableAutoConfiguration

@EnableAutoConfiguration是springboot实现自动化配置的核心注解,通过这个注解把spring应用所需的bean注入容器中.@EnableAutoConfiguration源码通过@Import注入了一个ImportSelector的实现类AutoConfigurationImportSelector,这个ImportSelector最终实现根据我们的配置,动态加载所需的bean.


  • @ConditionalOnProperty

2.2spring的常用注解

  • @ImportResource

@ImportResource({ “ classpath *:applicationContext.xml ” }) 奖xml文件内容加载进spring容器中


  • @EnableScheduling,@Scheduled

定时任务在配置类上添加@EnableScheduling开启对定时任务的支持,在相应的方法上添加@Scheduled声明需要执行的定时任务。

cron 是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次
zone 表示执行时间的时区
fixedDelay和fixedDelayString 表示一个固定延迟时间执行,上个任务完成后,延迟多长时间执行
fixedRate和fixedRateString 表示一个固定频率执行,上个任务开始后,多长时间后开始执行
initialDelay和initialDelayString 表示一个初始延迟时间,第一次被调用前延迟的时间

@Scheduled(initialDelay = 1000*10, fixedDelay = 1000*15)	//第一次调用延迟10s后,每过一个任务间隔周期15s
@Scheduled(fixedRateString = "2000",initialDelay = 1000)  //初始延迟1秒,每隔2秒
@Scheduled(fixedDelayString= "2000")   //每次执行完延迟2秒
@Scheduled(cron="0/3 * * * * ?") //每隔3秒执行一次


2.3其他

  • @Value注解

  • @PostConstruct

@PostConstruct该注解被用来修饰一个非静态的void()方法. 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
https://blog.csdn.net/qq360694660/article/details/82877222

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值