Skip to content

Instantly share code, notes, and snippets.

@CrisLi
Created September 26, 2013 05:58
Show Gist options
  • Save CrisLi/6710358 to your computer and use it in GitHub Desktop.
Save CrisLi/6710358 to your computer and use it in GitHub Desktop.
Spring MVC java based config
package com.xerox.efui.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.chris.demo")
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
package com.xerox.efui.config;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = "org.chris.demo", excludeFilters = {
@Filter({
Configuration.class, Controller.class
})
})
@PropertySource(value = "classpath:/config/appConfig.properties")
public class AppConfig {
@Autowired
private Environment environment;
@Bean
public Mapper mapper() {
return new DozerBeanMapper();
}
}
package com.xerox.efui.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {
AppConfig.class
};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {
WebConfig.class
};
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment