Spring完全基于Java配置和集成Junit单元测试

要点:

  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)注解
上一篇:【线段树】HDU 3397 Sequence operation 区间合并


下一篇:【Excle数据透视表】如何在数据透视表中使用合并单元格标志