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

网站开发专业培训学校google chrome官网入口

网站开发专业培训学校,google chrome官网入口,在网站做直播,建设网站主机依赖 建一个工程,两个Module: 1. springboot模块,表示springboot框架的源码实现 2. user包,表示用户业务系统,用来写业务代码来测试我们所模拟出来的SpringBoot 首先,SpringBoot是基于的Spring,所以我…

依赖

建一个工程,两个Module:

1. springboot模块,表示springboot框架的源码实现
2. user包,表示用户业务系统,用来写业务代码来测试我们所模拟出来的SpringBoot
首先,SpringBoot是基于的Spring,所以我们要依赖Spring,然后我希望我们模拟出来的
SpringBoot也支持Spring MVC的那一套功能,所以也要依赖Spring MVC,包括Tomcat等,所以在
SpringBoot模块中要添加以下依赖
 <dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.18</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.3.18</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.18</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId><version>9.0.60</version></dependency></dependencies>
在User模块下我们进行正常的开发就行了,比如先添加SpringBoot依赖:
<dependencies><dependency><groupId>org.example</groupId><artifactId>springboot</artifactId><version>1.0-SNAPSHOT</version><exclusions><exclusion><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><version>9.4.43.v20210629</version></dependency></dependencies>
然后定义相关的Controller和Service:

@RestControllerpublic class UserController {@Autowiredprivate UserService userService;@GetMapping("test")public String test(){return userService.test();}}
因为我们模拟实现的是SpringBoot,而不是SpringMVC,所以我直接在user包下定义了
UserController和UserService,最终我希望能运行MyApplication中的main方法,就直接启动了项
目,并能在浏览器中正常的访问到UserController中的某个方法。
核心注解和核心类
我们在真正使用SpringBoot时,核心会用到SpringBoot一个类和注解:
        1. @SpringBootApplication,这个注解是加在应用启动类上的,也就是main方法所在的类
        2. SpringApplication,这个类中有个run()方法,用来启动SpringBoot应用的
所以我们也来模拟实现他们
一个@ZhouyuSpringBootApplication注解:
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Configuration@ComponentScanpublic @interface ZhouyuSpringBootApplication {}
一个用来实现启动逻辑的ZhouyuSpringApplication类。
public class ZhouyuSpringApplication {public static void run(Class clazz){}}
注意run方法需要接收一个Class类型的参数,这个class是用来干嘛的,等会就知道了。
有了以上两者,我们就可以在MyApplication中来使用了,比如:
@ZhouyuSpringBootApplicationpublic class MyApplication {public static void main(String[] args) {ZhouyuSpringApplication.run(MyApplication.class);}}
现在用来是有模有样了,但中看不中用,所以我们要来好好实现以下run方法中的逻辑了。
run方法
run方法中需要实现什么具体的逻辑呢?
        首先,我们希望run方法一旦执行完,我们就能在浏览器中访问到UserController,那势必在run方法 中要启动Tomcat,通过Tomcat就能接收到请求了。
        大家如果学过Spring MVC的底层原理就会知道,在SpringMVC中有一个Servlet非常核心,那就是 DispatcherServlet,这个DispatcherServlet需要绑定一个Spring容器,因为DispatcherServlet接收 到请求后,就会从所绑定的Spring容器中找到所匹配的Controller,并执行所匹配的方法。
所以,在run方法中,我们要实现的逻辑如下:
1. 创建一个Spring容器
2. 创建Tomcat对象
3. 生成DispatcherServlet对象,并且和前面创建出来的Spring容器进行绑定
4. 将DispatcherServlet添加到Tomcat中
5. 启动Tomcat
创建Spring容器
这个步骤比较简单,代码如下
public class ZhouyuSpringApplication {public static void run(Class clazz){AnnotationConfigWebApplicationContext applicationContext = new
AnnotationConfigWebApplicationContext();applicationContext.register(clazz);applicationContext.refresh();}}
        我们创建的是一个AnnotationConfigWebApplicationContext容器,并且把run方法传入进来的class 作为容器的配置类,比如在MyApplication的run方法中,我们就是把MyApplication.class 传入到了 run方法中,最终MyApplication就是所创建出来的Spring容器的配置类,并且由于MyApplication类 上有@ZhouyuSpringBootApplication注解,而@ZhouyuSpringBootApplication注解的定义上又 存在@ComponentScan注解,所以AnnotationConfigWebApplicationContext容器在执行refresh时,就会解析MyApplication这个配置类,从而发现定义了@ComponentScan注解,也就知道了要 进行扫描 ,只不过扫描路径为空,而AnnotationConfigWebApplicationContext容器会处理这种情 况,如果扫描路径会空,则会将MyApplication所在的包路径做为扫描路径 ,从而就会扫描到UserService和UserController。
所以Spring容器创建完之后,容器内部就拥有了UserService和UserController这两个Bean。
启动Tomcat
我们用的是Embed-Tomcat,也就是内嵌的Tomcat,真正的SpringBoot中也用的是内嵌的Tomcat,
而对于启动内嵌的Tomcat,也并不麻烦,代码如下:
public class TomcatWebServer implements WebServer{@Overridepublic void start() {System.out.println("启动Tomcat");}public 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(contextPath, "dispatcher", new DispatcherServlet(applicationContext));context.addServletMappingDecoded("/*", "dispatcher");try {tomcat.start();} catch (LifecycleException e) {e.printStackTrace();}}
}
        代码虽然看上去比较多,但是逻辑并不复杂,比如配置了Tomcat绑定的端口为8081,后面向当前 Tomcat中添加了DispatcherServlet,并设置了一个Mapping关系,最后启动,其他代码则不用太过
关心。
        而且在构造DispatcherServlet对象时,传入了一个ApplicationContext对象,也就是一个Spring容 器,就是我们前文说的,DispatcherServlet对象和一个Spring容器进行绑定。
接下来,我们只需要在run方法中,调用startTomcat即可:
        接下来,我们只需要在run方法中,调用startTomcat即可:
 public static void run(Class clazz){
AnnotationConfigWebApplicationContext applicationContext = new
AnnotationConfigWebApplicationContext();applicationContext.register(clazz); 
applicationContext.refresh(); startTomcat(applicationContext);}
        实际上代码写到这,一个极度精简版的SpringBoot就写出来了,比如现在运行MyApplication,就能正常的启动项目,并能接收请求。
启动能看到Tomcat的启动日志:
然后在浏览器上访问: http://localhost:8081/test
也能正常的看到结果:
        此时,你可以继续去写其他的Controller和Service了,照样能正常访问到,而我们的业务代码中仍然 只用到了ZhouyuSpringApplication类和@ZhouyuSpringBootApplication注解。
实现Tomcat和Jetty的切换
虽然我们前面已经实现了一个比较简单的SpringBoot,不过我们可以继续来扩充它的功能,比如现在 我有这么一个需求,这个需求就是我现在不想使用Tomcat了,而是想要用Jetty,那该怎么办?
我们前面代码中默认启动的是Tomcat,那我现在想改成这样子:
1. 如果项目中有Tomcat的依赖,那就启动Tomcat
2. 如果项目中有Jetty的依赖就启动Jetty
3. 如果两者都没有则报错
4. 如果两者都有也报错
这个逻辑希望SpringBoot自动帮我实现,对于程序员用户而言,只要在Pom文件中添加相关依赖就可以了,想用Tomcat就加Tomcat依赖,想用Jetty就加Jetty依赖。
那SpringBoot该如何实现呢?
         我们知道,不管是Tomcat还是Jetty,它们都是应用服务器,或者是Servlet容器,所以我们可以定义 接口来表示它们,这个接口叫做WebServer(别问我为什么叫这个,因为真正的SpringBoot源码中也 叫这个)。
并且在这个接口中定义一个start方法:

public interface WebServer {public void start();}
有了WebServer接口之后,就针对Tomcat和Jetty提供两个实现类:
public class TomcatWebServer implements WebServer{@Overridepublic void start() {System.out.println("启动Jetty");}}
public class JettyWebServer implements WebServer{@Overridepublic void start() {System.out.println("启动Tomcat");}}

而在ZhouyuSpringApplication中的run方法中,我们就要去获取对应的WebServer,然后启动对应的webServer,代码为:

public static void run(Class clazz){
AnnotationConfigWebApplicationContext applicationContext = new
AnnotationConfigWebApplicationContext();applicationContext.register(clazz); 
applicationContext.refresh(); WebServer webServer = getWebServer(applicationContext);
webServer.start(); }public static WebServer getWebServer(ApplicationContext applicationContext){return null;}
这样,我们就只需要在getWebServer方法中去判断到底该返回TomcatWebServer还是
JettyWebServer。
前面提到过,我们希望根据项目中的依赖情况,来决定到底用哪个WebServer,我就直接用
SpringBoot中的源码实现方式来模拟了。
模拟实现条件注解
首先我们得实现一个条件注解@ZhouyuConditionalOnClass,对应代码如下:
@Target({ ElementType.TYPE, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@Conditional(ZhouyuOnClassCondition.class)public @interface ZhouyuConditionalOnClass {String value() default "";}

注意核心为@Conditional(ZhouyuOnClassCondition.class)中的ZhouyuOnClassCondition,因为
它才是真正得条件逻辑:

public class ZhouyuOnClassCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Map<String, Object> annotationAttributes =
metadata.getAnnotationAttributes(ZhouyuConditionalOnClass.class.getName());String className = (String) annotationAttributes.get("value");try {context.getClassLoader().loadClass(className);return true;} catch (ClassNotFoundException e) {return false;}}}

        具体逻辑为,拿到@ZhouyuConditionalOnClass中的value属性,然后用类加载器进行加载,如果加载到了所指定的这个类,那就表示符合条件,如果加载不到,则表示不符合条件。

模拟实现自动配置类
有了条件注解,我们就可以来使用它了,那如何实现呢?


这里就要用到自动配置类的概念,我们先看代码:

@Configuration
public class WebServiceAutoConfiguration {@Bean@ZhouyuConditionalOnClass("org.apache.catalina.startup.Tomcat")public TomcatWebServer tomcatWebServer(){return new TomcatWebServer();}@Bean@ZhouyuConditionalOnClass("org.eclipse.jetty.server.Server")public JettyWebServer jettyWebServer(){return new JettyWebServer();}
}
这个代码还是比较简单的,通过一个WebServiceAutoConfiguration的Spring配置类,在里面定义了 两个Bean,一个TomcatWebServer,一个JettyWebServer,不过这两个要生效的前提是符合当前所指定的条件,比如:
只有存在"org.apache.catalina.startup.Tomcat"类,那么才有TomcatWebServer这个Bean
只有存在"org.eclipse.jetty.server.Server"类,那么才有TomcatWebServer这个Bean
并且我们只需要在ZhouyuSpringApplication中getWebServer方法,如此实现:
public static WebServer getWebServer(ApplicationContext applicationContext){// key为beanName, value为Bean对象
Map<String, 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启动逻辑就是这样的:
        1. 创建一个AnnotationConfigWebApplicationContext容器
        2. 解析MyApplication类,然后进行扫描
        3. 通过getWebServer方法从Spring容器中获取WebServer类型的Bean
        4. 调用WebServer对象的start方法
有了以上步骤,我们还差了一个关键步骤,就是Spring要能解析到WebServiceAutoConfiguration这 个自动配置类,因为不管这个类里写了什么代码,Spring不去解析它,那都是没用的,此时我们需要 SpringBoot在run方法中,能找到WebServiceAutoConfiguration这个配置类并添加到Spring容器 中。
        MyApplication是Spring的一个配置类,但是MyApplication是我们传递给SpringBoot,从而添加到 Spring容器中去的,而WebServiceAutoConfiguration就需要SpringBoot去自动发现,而不需要程 序员做任何配置才能把它添加到Spring容器中去,而且要注意的是,Spring容器扫描也是扫描不到 WebServiceAutoConfiguration这个类的,因为我们的扫描路径是"com.zhouyu.user",而
WebServiceAutoConfiguration所在的包路径为"com.zhouyu.springboot"。
        那SpringBoot中是如何实现的呢?通过SPI,当然SpringBoot中自己实现了一套SPI机制,也就是我们 熟知的spring.factories文件,那么我们模拟就不搞复杂了,就直接用JDK自带的SPI机制。
发现自动配置类
        为了实现这个功能,以及为了最后的效果演示,我们需要把springboot源码和业务代码源码拆分两个 maven模块,也就相当于两个项目,最后的源码结构为

现在我们只需要在springboot项目中的resources目录下添加如下目录(META-INF/services)和文
件:

SPI的配置就完成了,相当于通过com.zhouyu.springboot.AutoConfiguration文件配置了
springboot中所提供的配置类。
并且提供一个接口:
 public interface AutoConfiguration {}
并且WebServiceAutoConfiguration实现该接口:
@Configuration
public class WebServerAutoConfiguration implements AutoConfiguration {@Bean@ConditionalOnClass("org.apache.catalina.startup.Tomcat")public TomcatWebServer tomcatWebServer(){return new TomcatWebServer();}@Bean@ConditionalOnClass("org.eclipse.jetty.server.Server")public JettyWebServer jettyWebServer(){return new JettyWebServer();}
}
然后我们再利用spring中的@Import技术来导入这些配置类,我们在
@ZhouyuSpringBootApplication的定义上增加如下代码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Configuration
@ComponentScan
@Import(ZhouyuImportSeclet.class)
public @interface ZhouyuSpringBootApplication {
}
ZhouyuImportSelect类为:
public class ZhouyuImportSeclet implements DeferredImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {// 自动配置ServiceLoader<AutoConfiguration> loader = ServiceLoader.load(AutoConfiguration.class);List<String> list = new ArrayList<>();for (AutoConfiguration configuration : loader) {list.add(configuration.getClass().getName());}return list.toArray(new String[0]);}
}
这就完成了从com.zhouyu.springboot.AutoConfiguration文件中获取自动配置类的名字,并导入到
Spring容器中,从而Spring容器就知道了这些配置类的存在,而对于user项目而言,是不需要修改代 码的。
此时运行MyApplication,就能看到启动了Tomcat:
因为SpringBoot默认在依赖中添加了Tomcat依赖,而如果在User模块中再添加jetty的依赖:

<dependencies><dependency>
<groupId>org.example</groupId> 
<artifactId>springboot</artifactId> 
<version>1.0-SNAPSHOT</version> </dependency><dependency>
<groupId>org.eclipse.jetty</groupId> 
<artifactId>jetty-server</artifactId> 
<version>9.4.43.v20210629</version> 1</dependency></dependencies>
那么启动MyApplication就会报错:

只有先排除到Tomcat的依赖,再添加Jetty的依赖才能启动Jetty:

注意:由于没有了Tomcat的依赖,记得把最开始写的startTomcat方法给注释掉,并删除掉相关依


文章转载自:
http://sherbet.sqLh.cn
http://rotissomat.sqLh.cn
http://homeopath.sqLh.cn
http://ductule.sqLh.cn
http://contribution.sqLh.cn
http://evolutionism.sqLh.cn
http://typed.sqLh.cn
http://christmastime.sqLh.cn
http://hein.sqLh.cn
http://galloper.sqLh.cn
http://azc.sqLh.cn
http://flatter.sqLh.cn
http://diamagnetize.sqLh.cn
http://calliper.sqLh.cn
http://marantic.sqLh.cn
http://unprompted.sqLh.cn
http://polygala.sqLh.cn
http://replead.sqLh.cn
http://defocus.sqLh.cn
http://esthetic.sqLh.cn
http://descensional.sqLh.cn
http://perborax.sqLh.cn
http://requite.sqLh.cn
http://twopence.sqLh.cn
http://dataller.sqLh.cn
http://meniscocytosis.sqLh.cn
http://flexowriter.sqLh.cn
http://cholecystagogue.sqLh.cn
http://insolvency.sqLh.cn
http://footwarmer.sqLh.cn
http://variolar.sqLh.cn
http://stamping.sqLh.cn
http://hadal.sqLh.cn
http://speller.sqLh.cn
http://zinjanthropine.sqLh.cn
http://lozengy.sqLh.cn
http://erosive.sqLh.cn
http://condyloma.sqLh.cn
http://somewhy.sqLh.cn
http://tipster.sqLh.cn
http://indefinite.sqLh.cn
http://latinization.sqLh.cn
http://calamiform.sqLh.cn
http://blida.sqLh.cn
http://joskin.sqLh.cn
http://foxhound.sqLh.cn
http://occupancy.sqLh.cn
http://flicflac.sqLh.cn
http://digitated.sqLh.cn
http://journalistic.sqLh.cn
http://laminative.sqLh.cn
http://conoidal.sqLh.cn
http://cincinnati.sqLh.cn
http://discernable.sqLh.cn
http://gynaecic.sqLh.cn
http://lapactic.sqLh.cn
http://decolor.sqLh.cn
http://nonsingular.sqLh.cn
http://antistrophe.sqLh.cn
http://hardie.sqLh.cn
http://salamandrine.sqLh.cn
http://sleave.sqLh.cn
http://aerodynamic.sqLh.cn
http://spitz.sqLh.cn
http://recoronation.sqLh.cn
http://reprovingly.sqLh.cn
http://yourself.sqLh.cn
http://horae.sqLh.cn
http://robin.sqLh.cn
http://preludize.sqLh.cn
http://chasable.sqLh.cn
http://recalcitrance.sqLh.cn
http://tuscan.sqLh.cn
http://tempi.sqLh.cn
http://pediococcus.sqLh.cn
http://parrotry.sqLh.cn
http://republicanise.sqLh.cn
http://bodily.sqLh.cn
http://sleeveless.sqLh.cn
http://stolid.sqLh.cn
http://duet.sqLh.cn
http://manjak.sqLh.cn
http://hygienically.sqLh.cn
http://xanthoconite.sqLh.cn
http://admitted.sqLh.cn
http://disrepair.sqLh.cn
http://epigynous.sqLh.cn
http://stance.sqLh.cn
http://worldwide.sqLh.cn
http://pessimal.sqLh.cn
http://manteau.sqLh.cn
http://hayfield.sqLh.cn
http://subcentral.sqLh.cn
http://unsatisfactorily.sqLh.cn
http://turgidness.sqLh.cn
http://zirconolite.sqLh.cn
http://servitress.sqLh.cn
http://rightfully.sqLh.cn
http://eroticism.sqLh.cn
http://terminism.sqLh.cn
http://www.15wanjia.com/news/78409.html

相关文章:

  • 艺术公司网站定制美国疫情最新数据消息
  • 哪些网站可以找到做药人的信息公司网站建设多少钱
  • 个人网站怎么做微商百度网盘app下载
  • 厦门装修公司网站建设seo技术蜘蛛屯
  • 搭建独立站简述网站建设的基本流程
  • 销售管理系统介绍网站关键字排名优化
  • 宣传册排版设计与制作百度搜索seo优化技巧
  • 文本文档做网站直接进入网站的代码
  • 仓山网站建设品牌策划公司排行榜
  • 建网站有多少种方式百姓网推广电话
  • 英文网站建设点击器
  • 做网站需要雇什么人江东seo做关键词优化
  • iis网站找不到网页优化大师官网下载
  • 网站内部资源推广口碑推广
  • eclipse可以做门户网站嘛哪个推广平台推广最靠谱
  • 做贸易要看什么网站今日国内新闻大事件
  • 天津做网站美工正规引流推广公司
  • 个人网站视频建设太原网站排名推广
  • 58接网站建设seo站长论坛
  • 芜湖网站建设whwzjs网页设计与制作期末作品
  • 开通企业网站需要多少钱百度推广后台登陆首页
  • 北京有哪些网站公司典型的口碑营销案例
  • 用vs2010做网站教程百度seo关键词排名查询工具
  • 大良网站建设服务四川seo选哪家
  • 网站503错误怎么解决快速优化关键词排名
  • 如何设计一个简单网页seo关键词优化外包公司
  • 怎样python做网站seo店铺描述
  • 域名可以免费注册码衡阳seo排名
  • 专业律所网站建设北京朝阳区
  • 页网站设计推广代理