【 @ComponentScan】

纠正:可以成功 Autowired 的原因是我在另外一个 config 文件中扫描了根包,这会顺带扫描所有该包的子包

还有,,上面的写法容易出错,建议这样写,

@ComponentScan(basePackageClasses={Spittle.class},

效果上是完全等价的,只不过不再需要自己写字符串了, Spittle 是根包下的类。

----------------------------------------------------------------------------------------------------------------------------------------

原文(错):发现一件奇怪的事情,我明明没有指定扫描那个包,却能 Autowired 那个包中的类的对象不出现异常。

按理来说,要自动装配某一个类的对象,首先,需要在那个类上标注 @Component 或者是 @Controller ;其次,需要扫描那个类所在的那个包。我的情况是条件二并不满足却获得了那个对象。我尝试给那个类去掉 @Component 注释,结果报错了,所以并不是条件一有问题,明显是由于 Spring 自主扫描了我没有显式指定的那个包,并为那个带有组件声明的类创建了对象。

更加显式地指定要扫描的那个包:

@ComponentScan(basePackageClasses={HomeController.class})

结果仍然是不报错地获得了我没有显式指定扫描的那个包的类的对象,实验之后发现,成功自动装配的条件应该是:

  1. 在类的上面有相应的组件声明,例如 @Component 或者是 @Controller ,或者其它等价注释。
  2. 类所在的包被直接显式指定扫描,或者该类被直接显示指定扫描的那个(些)包中的类所 import

总之不管怎样,作为组件的声明都是必须的。

【 mock Spring MVC 】

基于使用概率导入大量的依赖包有个缺点就是一旦依赖包出现问题很难锁定问题到底出在哪里!!!

这些问题通常是由于依赖包的版本造成的 ( ╯□╰ ) , 因此对于依赖包的导入务必保证不必要不导入,并添加一些适当的注释

最好是能够清楚地了解各个包的用途、来源,这样也方便查相应的文档:

【pom.xml 模板】

标记为红色的是进行 mock 测试可能用到的包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spittr</groupId>
<artifactId>spittr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<!-- spring-web 包括 web 、 bean 、aop 、 context 、 core 等 spring-webmvc 的子集-->
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.9.RELEASE</version>
</dependency> --> <!-- jSTL taglibs-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency> <!-- spring-web 包括 web 、 bean 、aop 、 context 、 core、 webmvc 等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <!-- spring-test 包括 test 等,含 mock -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
<scope>test</scope>
</dependency> <!-- mock 相关的类库 -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency> <!-- hamcrest 相关的类库 -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- junit 包括 junit 、 hamcrest -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- commons-lang 仅一个包,包括各种各样的工具,简化 hashCode 、 Equal 代码 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
</project>

【 mock Mvc】

package spittr.web;

import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
// 附上 API 文档地址: http://docs.spring.io/spring/docs/current/javadoc-api/
public class HomeControllerTest {
@Test
public void testHomePage() throws Exception {
HomeController controller = new HomeController();
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.view().name("home"));
}
}

可以修改为静态导入:

package spittr.web;

import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
// 附上 API 文档地址: http://docs.spring.io/spring/docs/current/javadoc-api/
public class HomeControllerTest {
@Test
public void testHomePage() throws Exception {
HomeController controller = new HomeController();
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(get("/")).andExpect(view().name("home"));
}
}

 【 mock 一个接口的实现类】

package spittr.web;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.view.InternalResourceView; import spittr.Spittle;
import spittr.data.SpittleRepository; public class SpittleControllerTest {
@Test
public void shouldShowRecentSpittles() throws Exception {
List<Spittle> expectedSpittles = createSpittleList(20);
SpittleRepository mockRepository = mock(SpittleRepository.class);
when(mockRepository.findSpittles(Long.MAX_VALUE, 20)).thenReturn(expectedSpittles);
SpittleController controller = new SpittleController(mockRepository);
MockMvc mockMvc = standaloneSetup(controller).setSingleView(new InternalResourceView("/WEB-INF/views/spittles.jsp")).build();
mockMvc.perform(get("/spittles")).andExpect(view().name("spittles")).andExpect(model().attributeExists("spittleList"));
} public List<Spittle> createSpittleList(int count) {
List<Spittle> spittles = new ArrayList<>();
for (int i = 0; i < count; ++i) {
spittles.add(new Spittle("Spittle" + i, new Date()));
}
return spittles;
}
}

Spring 之 @ComponentScan以及mock Spring MVC的更多相关文章

  1. Spring Data MongoDB example with Spring MVC 3.2

    Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...

  2. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  3. 结合源码浅谈Spring容器与其子容器Spring MVC 冲突问题

    容器是整个Spring 框架的核心思想,用来管理Bean的整个生命周期. 一个项目中引入Spring和SpringMVC这两个框架,Spring是父容器,SpringMVC是其子容器,子容器可以看见父 ...

  4. Spring:基于注解的Spring MVC

    什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  5. Spring使用ComponentScan扫描Maven多模块工程的其它模块

    说明:在新建好了Maven多模块工程后,如果想要在其它模块也能使用Spring的对象管理,比如@Autowrited这些注入方式,那么就必须开启包扫描的功能才能使其进行注入到Spring的对象管理中. ...

  6. Spring入门(十四):Spring MVC控制器的2种测试方法

    作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...

  7. spring <context:component-scan>使用说明(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

  8. spring <context:component-scan>(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

  9. Spring context:component-scan中使用context:include-filter和context:exclude-filter

    Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...

随机推荐

  1. 【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3314 一眼就是维护一个距离为d的单调递减队列... 第一次写.....看了下别人的代码... 这一题 ...

  2. 【Raspberry Pi】webpy+mysql+GPIO 实现手机控制

    1.mysql http://dev.mysql.com/doc/refman/5.5/en/index.html 安装 sudo apt-get install update sudo apt-ge ...

  3. CodeFirst中DbContext动态添加DbSet

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. sql case when then else end sql_variant

    /****************************************************************************** ** Name: usp_cfg_Get ...

  5. poj万人题

    poj提交数量过万的题,除了水爆了的题就是无比经典的,不得不刷的题. 准备将poj上提交次数过万的题刷个遍. 持续更新中... poj 2828(线段树) 此题乃是Zhu, Zeyuan神牛出的,拿到 ...

  6. HTTP 筛选器 DLL C:\Windows\Microsoft.Net\Framework\v4.0.30319\aspnet_filter.dll 加载失败。数据是错误。

    今天在一台win2003的云主机上,安装.net 4.0时,所有的网站都打不开了.打开事件查看器,发现以下错误: HTTP 筛选器 DLL C:\Windows\Microsoft.Net\Frame ...

  7. TGI指数

    TGI指数 目标人群中国具有某一特征的群体占比/总体中具有相同特征的群体的占比*标准数100

  8. python pip源配置,pip配置文件存放位置

    https://blog.csdn.net/u013066730/article/details/54580789/ pip源配置文件可以放置的位置: Linux/Unix: /etc/pip.con ...

  9. leetcode -day 15 Distinct Subsequences

    1.  Distinct Subsequences  Given a string S and a string T, count the number of distinct subsequen ...

  10. 使用pycharm操作django

    新建项目,选择已经建立好的虚拟环境 进入指令界面 新建app 添加一些文件和文件夹用于以后存放各种数据 settings设置 TEMPLATES设置 TEMPLATES = [ { 'BACKEND' ...