Skip to content

Instantly share code, notes, and snippets.

@mlaccetti
Created August 20, 2013 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlaccetti/6282536 to your computer and use it in GitHub Desktop.
Save mlaccetti/6282536 to your computer and use it in GitHub Desktop.
Bootstrap a Spring 3 annotation-only Java 6/Tomcat 6 app
package laccetti.spring.app;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "laccetti")
public class SpringConfiguration {
// bean definitions go here
}
package laccetti.spring.app;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@Slf4j
public class SpringWebConfiguration extends WebMvcConfigurerAdapter {
private List<HttpMessageConverter<?>> messageConverters;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/").setViewName("redirect:index.html");
}
/**
* The message converters for the content types we support.
*
* @return the message converters; returns the same list on subsequent calls
*/
private List<HttpMessageConverter<?>> getMessageConverters() {
if (messageConverters == null) {
messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter =
new MappingJackson2HttpMessageConverter();
messageConverters.add(mappingJacksonHttpMessageConverter);
}
return messageConverters;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.addAll(getMessageConverters());
}
@Bean
public static PropertySourcesPlaceholderConfigurer externalizedProperties() {
final PropertySourcesPlaceholderConfigurer props = new PropertySourcesPlaceholderConfigurer();
props.setLocation(new ClassPathResource("app.properties"));
props.setIgnoreResourceNotFound(true);
return props;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
</listener>
<servlet>
<description>Spring Web</description>
<servlet-name>Spring Web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>laccetti.spring.app.SpringConfiguration,laccetti.spring.app.SpringWebConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring Web</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment