springboot常用功能简单开发

异步任务

在主启动类加上@EnableAsync(自动开启异步任务)

@EnableAsync
public class bootMain {
    public static void main(String[] args){

        SpringApplication.run(bootMain.class,args);
    }
}

在异步的类上添加@Async

@Service
@Async
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    
    public void asyn() throws InterruptedException {
        Thread.sleep(5000);
        System.out.println("数据处理中。。。");
    }
}

Swagger

引入依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

编写Swagger配置信息

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class swaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2)
                        .groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2)
                        .groupName("C");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2)
                        .groupName("B");
    }

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                        .groupName("wujianlong")//
                        .apiInfo(apiInfo())
                        .enable(true)
                        .select()
                //配置swagger扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.controller"))
                //过滤路径
                //.paths(PathSelectors.any())
                        .build();
    }

    private ApiInfo apiInfo(){
        Contact DEFAULT_CONTACT = new Contact("wujianlong", "https://blog.csdn.net/qq_41914899", "yourEmail@qq.com");
        return new ApiInfo("wujianlong的SWAGGER",
                "一切都是最好的安排!",
                "V1.0",
                "https://blog.csdn.net/qq_41914899",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

启动项目后访问:localhost:8888/swagger-ui.html
如果报404,就在实现WebMvcConfigurer 接口的类上添加如下代码

@Configuration
public class adminConfig implements WebMvcConfigurer {

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}

发邮件

引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

ymal配置文件配置

spring:
  mail:
    username: yourEmail@qq.com #你的邮箱
    password: QQ邮箱的授权码
    host: smtp.qq.com

开启QQ邮箱授权码登录
在这里插入图片描述
代码实现

package com;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
public class mail {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void sendMail(){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("wujianlong很棒!!!");
        simpleMailMessage.setTo("你的邮箱");
        simpleMailMessage.setFrom("你的邮箱");
        simpleMailMessage.setText("相信一切都是最好的安排!!!");
        mailSender.send(simpleMailMessage);
    }

    @Test
    void sendMain2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setTo("你的邮箱");
        mimeMessageHelper.setFrom("你的邮箱");
        //正文
        mimeMessageHelper.setSubject("永远相信自己可以!!!");
        mimeMessageHelper.setText("<p style='color:red'>相信自己能成功!!!</p>",true);

        //附件
        mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\wujianlong\\Pictures\\图片1.png"));

        mailSender.send(mimeMessage);

    }
}

定时任务

主启动类上添加@EnableScheduling注解

@EnableScheduling  //开启定时任务
public class bootMain {
    public static void main(String[] args){

        SpringApplication.run(bootMain.class,args);
    }
}

在方法上添加注解@Scheduled,使用cron=“秒,分,时,日,月,周”来定时执行这个方法

@Service
public class testService {

    @Scheduled(cron = "0/5 * * * * *")
    void sched(){
        System.out.println("哈哈哈,你被执行力---");
    }
}

直接启动项目即可测试

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值