栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot自定义starter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot自定义starter

  • 本章我们介绍如果自定义一个starter
  • 创建springboot 项目
  • 创建factoryAutoConfiguration 自动配置类
package com.example.factorystart.auto;

import com.example.factorystart.bean.FactoryProperties;
import com.example.factorystart.server.Taskfactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@EnableConfigurationProperties(FactoryProperties.class)
public class factoryAutoConfiguration {


    @ConditionalOnMissingBean(Taskfactory.class)
    @Bean
    public Taskfactory getTaskFactory(){
        System.out.printf("加载自动发配置");
        return new Taskfactory();
    }


}


@Component
@ConfigurationProperties("hy.task")
@Data
public class FactoryProperties {

   private   int ThreadCore;
   private int MaxThread;
   private int keepAliveTime;

}

public class Taskfactory {

    class MyFactory implements ThreadFactory {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread();
        }
    }

    private ThreadPoolExecutor executor;
    public Taskfactory(){
        System.out.printf("创建了对象n");
        executor=new ThreadPoolExecutor(
               properties.getThreadCore(),
                properties.getMaxThread(),
                20,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(properties.getMaxThread()),
                new MyFactory()
        );
    }

    @Autowired
    FactoryProperties properties;

    public void Sumbit(Runnable runnable){
        executor.execute(runnable);
        System.out.printf("你提交了一个任务");
    }
}
  • 创建spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.example.factorystart.auto.factoryAutoConfiguration
  • 引入该starter
		
            com.example
            factoryStart
            0.0.1-SNAPSHOT
        
  • 填写文件
hy.task.MaxCore=100
hy.task.MaxThread=1000
  • 创建个控制器测试
@RestController
@RequestMapping("/Api/Login")
public class LoginController {
    @Autowired
    Taskfactory taskfactory;
   

    @RequestMapping(value = "/SendCode",method ={RequestMethod.GET})
    public String SendCode(@RequestParam String email){
        taskfactory.Sumbit(new Runnable() {
            @Override
            public void run() {
				
            }
        });
        return "发送成功";
    }


}
  • 目录
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/884490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号