spring mvc将java配置与xml配置结合起来

时间:2014-11-03 14:41:21

标签: java xml spring spring-mvc configuration

您好我正在使用spring mvc和thymeleaf,我的Web应用程序配置了这样的java配置:

这是我的扩展WebMvcConfigurerAdapter

的类
@EnableWebMvc
@ComponentScan(basePackages = {"com.abc.R10"})
@Configuration
@ImportResource( {"/WEB-INF/spring/servlet-context.xml"})
public class ConfigApp extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
        registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**");
        registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
      public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:messages/messages");
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(0);// # -1 : never reload, 0 always reload
        return messageSource;
    }


//  THYMELEAF

        @Bean 
        public ServletContextTemplateResolver templateResolver() {
            ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
            resolver.setPrefix("/WEB-INF/views/vistaHtml/");
            resolver.setSuffix(".html");
            resolver.setTemplateMode("HTML5");
            resolver.setCharacterEncoding("UTF-8");
            resolver.setOrder(0);
            resolver.setCacheable(false);
            return resolver;
        }

        //CONFIGURACION DIALECTS
        @Bean
        public SpringSecurityDialect springSecurityDialect() {
            return new SpringSecurityDialect();
        }

        @Bean 
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine engine  =  new SpringTemplateEngine();
            engine.setTemplateResolver( templateResolver() );
            engine.setMessageSource( messageSource() );

            //CONFIG DIALECTS
                Set<IDialect> dialects = new HashSet<IDialect>();
                dialects.add(new SpringSecurityDialect());
                engine.setAdditionalDialects(dialects);
            //CONFIG DIALECTS

            return engine;
        }




        @Bean 
        public ThymeleafViewResolver thymeleafViewResolver() {
            ThymeleafViewResolver resolver  =  new ThymeleafViewResolver();
            //String[] vistas = {"*.html", "*.jsp"};

            resolver.setTemplateEngine( templateEngine() );
            resolver.setOrder(1);
            //resolver.setViewNames(vistas);
            resolver.setCache( false );
            return resolver;
        }

        @Bean
        public SpringResourceTemplateResolver thymeleafSpringResource() {
            SpringResourceTemplateResolver vista = new SpringResourceTemplateResolver();
            vista.setTemplateMode("HTML5");
            return vista;
        }
    //

这是我的根配置:

@Configuration
@ComponentScan
@Import({ConfigApp.class)
public class ConfigRoot {
  // this is empty becasue i'm using spring security and when i initialize the security config it creates a root 
}

这是miy初始化类

public class InitApp extends AbstractAnnotationConfigDispatcherServletInitializer {



    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{ ConfigRoot.class };
    }


    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { ConfigApp.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new HiddenHttpMethodFilter() };
    }
}

我的问题是我如何添加xml配置也因为我想使用一些无法使用java配置的工具,我只想创建一个xml配置来配置这些工具

编辑:我将此公告@ImportResource( {"servlet-context.xml"})置于我的configApp之上,但我收到以下异常:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/servlet-context.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/servlet-context.xml]

EDIT2:我走的路很糟糕,现在可行了

0 个答案:

没有答案