要点:

  1. 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件
  2. 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法的返回值为一个Bean,相应于配置applicationContext.xml等spring的xml配置文件

配置启动类

继承WebApplicationInitializer并重新onStartup方法,加载配置类,相当于配置web.xml文件

 public class WebAppInitConfig implements WebApplicationInitializer {

     @Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class);//这是自定义的配置类,这个配置类会影响项目全局 // Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebMvcConfig.class);//这是自定义的配置类,这个配置类只会影响当前模块 // Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}

AnnotationConfigApplicationContext:用来把使用注解的配置类加载到spring容器中,也就是把那些在配置类中定义的bean交给spring管理

也可以使用WebApplicationInitializer的实现类AbstractDispatcherServletInitializerAbstractAnnotationConfigDispatcherServletInitializer来进行更为简洁的配置,官方文档地址,只要实现了WebApplicationInitializer将会被servlet容器自动识别并加载

使用AbstractAnnotationConfigDispatcherServletInitializer配置示例如下

 public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

     @Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebMvcConfig.class};;
} @Override
protected String[] getServletMappings() {
return new String[]{"/"};
} }

自定义配置类

配置spring管理bean,相应于配置applicationContext.xml等spring的xml配置文件,就是上面使用WebApplicationInitializer加载的类

 @Configuration
@EnableWebMvc //注解驱动springMVC 相当于xml中的<mvc:annotation-driven>
@ComponentScan(basePackages = "com.woncode") //启用组件扫描
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//配置静态资源的处理,要求DispatchServlet对静态资源的请求转发到servlet容器中默认的Servlet上
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/statics/**").addResourceLocations("/WEB-INF/classes/statics/");
super.addResourceHandlers(registry);
} @Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver= new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/classes/templates/");
resolver.setSuffix(".html");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} }

@Import注解:用来引入其他配置类到当前配置类,然后就可以在当前配置类中使用其他配置类中的bean了,这在分模块的项目中很有用,因为分模块就是希望分割配置,但是有时又需要使用一些其他已经定义过的配置类,所以可以用此导入

集成JUnit单元测试

添加sprig-test的maven依赖

在测试类头上加如下注解,其中配置类HibernateConfig参考另一篇博文《基于Java配置Spring加Hibernate和再加SpringData时的差别》,是配置使用Spring+hibernate的

 @Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebMvcConfig.class, HibernateConfig.class})
public class SpringTest {
}

注意:

  1. 如果使用Spring MVC则一定要加@WebAppConfiguration注解,否则无法载入web上下文环境,我在这个坑好久
  2. 在测试方法中如果进行一些数据库事物操作,比如要测试插入数据的方法,因为Spring测试套件在测试事务方法执行完成后会自动回滚,如果想要插入的数据持久保存到数据库中,则要在测试方法头上加@Rollback(false)注解

最新文章

  1. 解决代码着色组件SyntaxHighlighter行号显示问题
  2. android gravity属性 和 weight属性
  3. 如何理解和熟练使用JS 中的call apply
  4. My安装Eclipse三种方法插件
  5. VC++客户端 缩小包的尺寸
  6. Unity --- 如何简单的判断图片是否含有 alpha channel
  7. UVA - 1625 Color Length[序列DP 提前计算代价]
  8. SQL Server查询优化中的两个选项
  9. UML类图和时序图
  10. Django使用manage.py备份与恢复数据
  11. Json转list,二层解析转换
  12. jsPlumb学习笔记
  13. Y480&amp;Y580 刷slic2.1全自动教程
  14. Maven 高级应用
  15. Precision/Recall、ROC/AUC、AP/MAP等概念区分
  16. luogu P1768 天路
  17. linux下javadoc生成文件出现中文乱码
  18. 7.vs的基本设置
  19. 网易云音乐PC客户端加密API逆向解析
  20. dinner 后台 nodemon 部署 Koa (关闭everything 安装或排除node_modules) # mysql 没开192.168.x.x 需要设置一下 #Navicat Premium,mysql 数据库版本有要求:mysql-5.7.17.msi 对??的支持

热门文章

  1. linux系统环境与文件权限
  2. 菜鸟容易中的招__setattr__
  3. 深入探究Lua的GC算法(下)-《Lua设计与实现》
  4. Java入门2
  5. BST讲解
  6. java基本数据类型的包装类
  7. [LeetCode] Merge Two Binary Trees 合并二叉树
  8. [LeetCode] Array Partition I 数组分割之一
  9. [HNOI 2013]游走
  10. [CODEVS 1288]埃及分数