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

蓝海国际版网站建设百度seo如何优化

蓝海国际版网站建设,百度seo如何优化,wordpress 功能介绍,公司管理系统框架前面我们介绍了许多 Spring 常用知识点上的常见应用错误。当然或许这些所谓的常用,你仍然没有使用,例如对于 Spring Data 的使用,,有的项目确实用不到。那么这一讲,我们聊聊 Spring Test,相信你肯定绕不开对…

前面我们介绍了许多 Spring 常用知识点上的常见应用错误。当然或许这些所谓的常用,你仍然没有使用,例如对于 Spring Data 的使用,,有的项目确实用不到。那么这一讲,我们聊聊 Spring Test,相信你肯定绕不开对它的使用,除非你不使用 Spring 来开发程序,或者你使用了 Spring 但是你不写测试。但话说回来,后者的情况就算你想如此,你的老板也不会同意吧。

那么在 Spring Test 的应用上,有哪些常见错误呢?这里我给你梳理了两个典型,闲话少叙,我们直接进入这一讲的学习。

案例 1:资源文件扫描不到

首先,我们来写一个 HelloWorld 版的 Spring Boot 程序以做测试备用。

先来定义一个 Controller:

@RestController
public class HelloController {@AutowiredHelloWorldService helloWorldService;@RequestMapping(path = "hi", method = RequestMethod.GET)public String hi() throws Exception{return  helloWorldService.toString() ;};}

当访问 http://localhost:8080/hi 时,上述接口会打印自动注入的 HelloWorldService 类型的 Bean。而对于这个 Bean 的定义,我们这里使用配置文件的方式进行。

1.定义 HelloWorldService,具体到 HelloWorldService 的实现并非本讲的重点,所以我们可以简单实现如下:

public class HelloWorldService {
}

2.定义一个 spring.xml,在这个 XML 中定义 HelloWorldServic 的 Bean,并把这个 spring.xml 文件放置在 /src/main/resources 中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloWorldService" class="com.spring.puzzle.others.test.example1.HelloWorldService"></bean>
</beans>

3.定义一个 Configuration 引入上述定义 XML,具体实现方式如下:

@Configuration
@ImportResource(locations = {"spring.xml"})
public class Config {
}

完成上述步骤后,我们就可以使用 main() 启动起来。测试这个接口,一切符合预期。那么接下来,我们来写一个测试:

@SpringBootTest()
class ApplicationTests {@Autowiredpublic HelloController helloController;@Testpublic void testController() throws Exception {String response = helloController.hi();Assert.notNull(response, "not null");}}

 ​当我们运行上述测试的时候,会发现测试失败了,报错如下:

为什么单独运行应用程序没有问题,但是运行测试就不行了呢?我们需要研究一下 Spring 的源码,来找找答案。

案例解析

在了解这个问题的根本原因之前,我们先从调试的角度来对比下启动程序和测试加载 spring.xml 的不同之处。

1. 启动程序加载 spring.xml

首先看下调用栈:

 可以看出,它最终以 ClassPathResource 形式来加载,这个资源的情况如下:

 而具体到加载实现,它使用的是 ClassPathResource#getInputStream 来加载 spring.xml 文件:

从上述调用及代码实现,可以看出最终是可以加载成功的。

2. 测试加载 spring.xml

首先看下调用栈:

可以看出它是按 ServletContextResource 来加载的,这个资源的情况如下:

 具体到实现,它最终使用的是 MockServletContext#getResourceAsStream 来加载文件:

@Nullable
public InputStream getResourceAsStream(String path) {String resourceLocation = this.getResourceLocation(path);Resource resource = null;try {resource = this.resourceLoader.getResource(resourceLocation);return !resource.exists() ? null : resource.getInputStream();} catch (IOException | InvalidPathException var5) {if (this.logger.isWarnEnabled()) {this.logger.warn("Could not open InputStream for resource " + (resource != null ? resource : resourceLocation), var5);}return null;}
}

 ​你可以继续跟踪它的加载位置相关代码,即 getResourceLocation():

protected String getResourceLocation(String path) {if (!path.startsWith("/")) {path = "/" + path;}//加上前缀:/src/main/resourcesString resourceLocation = this.getResourceBasePathLocation(path);if (this.exists(resourceLocation)) {return resourceLocation;} else {//{"classpath:META-INF/resources", "classpath:resources", "classpath:static", "classpath:public"};String[] var3 = SPRING_BOOT_RESOURCE_LOCATIONS;int var4 = var3.length;for(int var5 = 0; var5 < var4; ++var5) {String prefix = var3[var5];resourceLocation = prefix + path;if (this.exists(resourceLocation)) {return resourceLocation;}}return super.getResourceLocation(path);}
}

你会发现,它尝试从下面的一些位置进行加载:

classpath:META-INF/resources
classpath:resources
classpath:static
classpath:public
src/main/webapp

如果你仔细看这些目录,你还会发现,这些目录都没有 spring.xml。或许你认为源文件 src/main/resource 下面不是有一个 spring.xml 么?那上述位置中的 classpath:resources 不就能加载了么?

那你肯定是忽略了一点:当程序运行起来后,src/main/resource 下的文件最终是不带什么 resource 的。关于这点,你可以直接查看编译后的目录(本地编译后是 target\classes 目录),示例如下:

 所以,最终我们在所有的目录中都找不到 spring.xml,并且会报错提示加载不了文件。报错的地方位于 ServletContextResource#getInputStream 中:

问题修正

从上述案例解析中,我们了解到了报错的原因,那么如何修正这个问题?这里我们可以采用两种方式。

1. 在加载目录上放置 spring.xml

就本案例而言,加载目录有很多,所以修正方式也不少,我们可以建立一个 src/main/webapp,然后把 spring.xml 复制一份进去就可以了。也可以在 /src/main/resources 下面再建立一个 resources 目录,然后放置进去也可以。

2. 在 @ImportResource 使用 classpath 加载方式

@Configuration
//@ImportResource(locations = {"spring.xml"})
@ImportResource(locations = {"classpath:spring.xml"})
public class Config {
}

这里,我们可以通过 Spring 的官方文档简单了解下不同加载方式的区别,参考 https://docs.spring.io/spring-framework/docs/2.5.x/reference/resources.html:

很明显,我们一般都不会使用本案例的方式(即 locations = {"spring.xml"},无任何“前缀”的方式),毕竟它已经依赖于使用的 ApplicationContext。而 classPath 更为普适些,而一旦你按上述方式修正后,你会发现它加载的资源已经不再是 ServletContextResource,而是和应用程序一样的 ClassPathResource,这样自然可以加载到了。

所以说到底,表面上看,这个问题是关于测试的案例,但是实际上是 ImportResource 的使用问题。不过通过这个案例,你也会明白,很多用法真的只能在某个特定场合才能工作起来,你只是比较幸运而已。 

http://www.dtcms.com/wzjs/243531.html

相关文章:

  • 零基础学做网站页品牌网络推广方案
  • 哈尔滨做网站费用报价网络黄页推广软件哪个好用
  • 商丘做网站哪家好定制建站网站建设
  • 安庆网站建设推广全国防疫大数据平台
  • 可信网站是否有规定必须做做电商如何起步
  • 上海网站设计制作报价如何免费推广一个网站
  • 做网站使用明星照片可以吗百度收录规则2022
  • 京东网站建设步骤那种网站怎么搜关键词
  • 合肥网站建设设计电商代运营
  • 东莞seo关键字优化seo最强
  • 南昌网站搭建制作公司如何建网站
  • 徐州建筑网站百度精准营销获客平台
  • 网站运营需要做什么李江seo
  • 复制Wordpress网站快速网络推广
  • wap建站系统站长工具的使用seo综合查询运营
  • 网站建设心得体会总结流量大的推广平台有哪些
  • wordpress 仪表板主题福州seo按天收费
  • 数码网站建设的规模与类别好消息疫情要结束了
  • 宝安做棋牌网站建设有哪些公司如何线上推广自己产品
  • 企业所得税怎么算一般纳税人北京云无限优化
  • 新乡个人网站建设哪家好seo和sem的区别是什么
  • B2C网站可使用的交易功能为网络舆情分析研判报告
  • wordpress basic主题百度关键词优化公司
  • 如何制作小程序的详细步骤江北seo综合优化外包
  • 广西城乡建设部网站长沙网站排名推广
  • 河南省百城建设提质网站如何做网址
  • 怎么做家教网站全网营销软件
  • PHP网站新闻发布怎么做交换链接
  • 南宁网站建设_seo优化服务公司百度开车关键词
  • 购物商城外贸网站建设最新推广赚钱的app