当前位置: 首页 > news >正文

安庆市建设办事处网站苏州网站推广如何

安庆市建设办事处网站,苏州网站推广如何,网站制作资源,es网站开发你好#xff0c;我是柳岸花开。 在这篇文章中#xff0c;我们将手写模拟SpringBoot的核心流程#xff0c;让大家能够以一种简单的方式了解SpringBoot的大概工作原理。 项目结构 我们创建一个工程#xff0c;包含两个模块#xff1a; springboot模块#xff0c;表示Spring… 你好我是柳岸花开。 在这篇文章中我们将手写模拟SpringBoot的核心流程让大家能够以一种简单的方式了解SpringBoot的大概工作原理。 项目结构 我们创建一个工程包含两个模块 springboot模块表示SpringBoot框架的源码实现。 user包表示用户业务系统用来写业务代码来测试我们所模拟出来的SpringBoot。 首先在springboot模块中添加以下依赖 dependencies    dependency        groupIdorg.springframework/groupId        artifactIdspring-context/artifactId        version5.3.18/version    /dependency    dependency        groupIdorg.springframework/groupId        artifactIdspring-web/artifactId        version5.3.18/version    /dependency    dependency        groupIdorg.springframework/groupId        artifactIdspring-webmvc/artifactId        version5.3.18/version    /dependency    dependency        groupIdjavax.servlet/groupId        artifactIdjavax.servlet-api/artifactId        version4.0.1/version    /dependency    dependency        groupIdorg.apache.tomcat.embed/groupId        artifactIdtomcat-embed-core/artifactId        version9.0.60/version    /dependency/dependencies 在user模块下我们进行正常的开发比如先添加SpringBoot依赖然后定义相关的Controller和Service dependencies    dependency        groupIdorg.example/groupId        artifactIdspringboot/artifactId        version1.0-SNAPSHOT/version    /dependency/dependencies 定义UserController和UserService 在user包下定义UserController和UserService希望能通过运行MyApplication中的main方法直接启动项目并能在浏览器中正常访问到UserController中的某个方法。 RestControllerpublic class UserController {    Autowired    private UserService userService;    GetMapping(test)    public String test(){        return userService.test();    }} 核心注解和核心类 SpringBootApplication这个注解加在应用启动类上即 main方法所在的类。 SpringApplication这个类中有个 run()方法用来启动SpringBoot应用。 Target(ElementType.TYPE)Retention(RetentionPolicy.RUNTIME)ConfigurationComponentScanpublic interface BobSpringBootApplication {}public class BobSpringApplication {    public static void run(Class clazz){        AnnotationConfigWebApplicationContext applicationContext  new AnnotationConfigWebApplicationContext();        applicationContext.register(clazz);        applicationContext.refresh();        startTomcat(applicationContext);    }    private static void startTomcat(WebApplicationContext applicationContext) {        Tomcat tomcat  new Tomcat();Server server  tomcat.getServer();Service service  server.findService(Tomcat);Connector connector  new Connector();connector.setPort(8081);Engine engine  new StandardEngine();engine.setDefaultHost(localhost);Host host  new StandardHost();host.setName(localhost);String contextPath  ;Context context  new StandardContext();context.setPath(contextPath);context.addLifecycleListener(new Tomcat.FixContextListener());host.addChild(context);engine.addChild(host);service.setContainer(engine);service.addConnector(connector);        Tomcat.addServlet(context, dispatcher, new DispatcherServlet(applicationContext));        context.addServletMappingDecoded(/, dispatcher);        try {            tomcat.start();        } catch (LifecycleException e) {            e.printStackTrace();        }        tomcat.getServer().await();    }}BobSpringBootApplicationpublic class MyApplication {    public static void main(String[] args) {        BobSpringApplication.run(MyApplication.class);    }} 实现Tomcat和Jetty的切换 如果项目中有Tomcat的依赖那就启动Tomcat如果有Jetty的依赖就启动Jetty如果两者都没有则报错如果两者都有也报错。 public interface WebServer {    void start();}public class TomcatWebServer implements WebServer {    Override    public void start() {        System.out.println(启动Tomcat);    }}public class JettyWebServer implements WebServer {    Override    public void start() {        System.out.println(启动Jetty);    }} 模拟实现条件注解 Target({ElementType.TYPE, ElementType.METHOD})Retention(RetentionPolicy.RUNTIME)Conditional(BobOnClassCondition.class)public interface BobConditionalOnClass {    String value() default ;}public class BobOnClassCondition implements Condition {    Override    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {        MapString, Object annotationAttributes  metadata.getAnnotationAttributes(BobConditionalOnClass.class.getName());        String className  (String) annotationAttributes.get(value);        try {            context.getClassLoader().loadClass(className);            return true;        } catch (ClassNotFoundException e) {            return false;        }    }} 模拟实现自动配置类 Configurationpublic class WebServiceAutoConfiguration {    Bean    BobConditionalOnClass(org.apache.catalina.startup.Tomcat)    public TomcatWebServer tomcatWebServer() {        return new TomcatWebServer();    }    Bean    BobConditionalOnClass(org.eclipse.jetty.server.Server)    public JettyWebServer jettyWebServer() {        return new JettyWebServer();    }} 在BobSpringApplication中添加获取WebServer的方法 public static WebServer getWebServer(ApplicationContext applicationContext) {    MapString, WebServer webServers  applicationContext.getBeansOfType(WebServer.class);    if (webServers.isEmpty()) {        throw new NullPointerException();    }    if (webServers.size()  1) {        throw new IllegalStateException();    }    return webServers.values().stream().findFirst().get();} 通过以上步骤我们实现了一个简单的SpringBoot并且可以根据依赖切换Tomcat和Jetty。 关注我下期了解 ​ SpringBoot自动配置 ​ ​ ​ ​ 回复 222获取Java面试题合集 ​ 关于我 ​ 一枚爱折腾的Java程序猿专注Spring干货。把路上的问题记录下来帮助那些和我一样的人。 ​ 好奇心强喜欢并深入研究古天文。 ​ 崇尚 个人系统创建做一些时间越长越有价值的事情。思考 把时间留下来 又 每刻都是新的。 本文由 mdnice 多平台发布
http://www.dnsts.com.cn/news/204612.html

相关文章:

  • 用工备案的系统的网站美食网站开发可行性分析报告
  • wordpress整合百度关键字优化
  • 做电影下载网站好网站静态和动态那个好
  • 怎么做平台网站会外语和做网站
  • 百度地图添加到网站iis访问网站打开要很久
  • 宁国市城乡与住房建设网站wordpress 免费版
  • 手机网站怎么备案gov域名网站有哪些
  • 怎么做视频直播网站响应式网站哪里做
  • 深圳网站设计兴田德润i简介湖南手机版建站系统信息
  • 境外建网站建设网站的不足
  • 青铜峡建设局网站中山网站seo优化
  • 网站建设要准备什么软件论坛空间申请
  • 昆山哪家做网站好专业网站推广引流
  • 泉港区住房和城乡规划建设局网站wordpress地理定位
  • 缠绕机东莞网站建设技术支持装修平台网站
  • 做外贸的数据网站可以发锚文本的网站
  • 免费游戏网站建设杭州建设网站
  • 加强二级部门网站建设网址怎么创建
  • 广州市网站建设企业邮箱登陆登录入口
  • 网站需求分析报告企业应用软件开发
  • 网站开发必学的技巧有哪些论坛网站html模板
  • 门户网站建设管理工作方案网上做电商怎么做
  • 房地产网站建设方案书做视频网站需要执照吗
  • 做装修的有那些网站做公益活动的网站
  • 做网站建设的建行网站企业网银
  • 中小型网站设计哪家好建设网站门户
  • frontpage导入网站范县网站建设价格
  • ftp网站备份网页开发者模式
  • 手机商城网站源码wordpress网站资源
  • 科技网站建设方案北京市新闻