Created
May 12, 2015 14:03
-
-
Save woemler/7d5fc7099f924232be4a to your computer and use it in GitHub Desktop.
Spring web app, full-Java configuration (no web.xml)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.woemler.myapp.config; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@ComponentScan(basePackages = { "me.woemler.myapp" }) | |
public class ApplicationConfig { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.username=user | |
db.password=pw | |
db.url=jdbc:mysql://localhost:3306/mydatabase | |
db.driverClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource | |
db.driver=com.mysql.jdbc.Driver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.woemler.myapp.config; | |
import com.zaxxer.hikari.HikariConfig; | |
import com.zaxxer.hikari.HikariDataSource; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.PropertySource; | |
import org.springframework.core.env.Environment; | |
import javax.sql.DataSource; | |
@Configuration | |
@ComponentScan({"me.woemler.myapp"}) | |
@PropertySource({"classpath:config/data-source.properties"}) | |
public class DataSourceConfig { | |
@Autowired | |
Environment env; | |
@Bean | |
public HikariConfig hikariConfig(){ | |
HikariConfig dataSourceConfig = new HikariConfig(); | |
dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver")); | |
dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url")); | |
dataSourceConfig.setUsername(env.getRequiredProperty("db.username")); | |
dataSourceConfig.setPassword(env.getRequiredProperty("db.password")); | |
dataSourceConfig.setPoolName("springHikariCP"); | |
dataSourceConfig.setConnectionTestQuery("SELECT 1"); | |
dataSourceConfig.setMaximumPoolSize(10); | |
dataSourceConfig.setIdleTimeout(30000); | |
return dataSourceConfig; | |
} | |
@Bean(destroyMethod = "close") | |
public DataSource dataSource(){ | |
return new HikariDataSource(hikariConfig()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | |
http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>me.woemler</groupId> | |
<artifactId>myapp</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<name>My Web App</name> | |
<properties> | |
<spring.platform.version>1.1.1.RELEASE</spring.platform.version> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>io.spring.platform</groupId> | |
<artifactId>platform-bom</artifactId> | |
<version>${spring.platform.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<!-- Spring --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jdbc</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webmvc</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-beans</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-expression</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.hateoas</groupId> | |
<artifactId>spring-hateoas</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.plugin</groupId> | |
<artifactId>spring-plugin-core</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-oxm</artifactId> | |
</dependency> | |
<!-- Jackson --> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-core</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-annotations</artifactId> | |
</dependency> | |
<!-- Servlet --> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet.jsp</groupId> | |
<artifactId>javax.servlet.jsp-api</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>jstl</artifactId> | |
</dependency> | |
<!-- MySQL --> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<!-- HikariCP --> | |
<dependency> | |
<groupId>com.zaxxer</groupId> | |
<artifactId>HikariCP-java6</artifactId> | |
</dependency> | |
<!-- Logging --> | |
<dependency> | |
<groupId>log4j</groupId> | |
<artifactId>log4j</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>commons-logging</groupId> | |
<artifactId>commons-logging</artifactId> | |
</dependency> | |
<!-- Testing --> | |
<dependency> | |
<groupId>org.hamcrest</groupId> | |
<artifactId>hamcrest-all</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<scope>test</scope> | |
<exclusions> | |
<exclusion> | |
<artifactId>hamcrest-core</artifactId> | |
<groupId>org.hamcrest</groupId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<pluginManagement> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>maven-war-plugin</artifactId> | |
<configuration> | |
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- This is important if you are doing Java-only config --> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>maven-surefire-plugin</artifactId> | |
<configuration> | |
<includes> | |
<include>**/*Tests.java</include> | |
</includes> | |
</configuration> | |
</plugin> | |
</plugins> | |
</pluginManagement> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.woemler.myapp.config; | |
import com.blueprint.centromere.web.util.FilteringJackson2HttpMessageConverter; | |
import com.blueprint.centromere.web.util.TextMessageConverter; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.converter.HttpMessageConverter; | |
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; | |
import org.springframework.oxm.xstream.XStreamMarshaller; | |
import org.springframework.web.servlet.config.annotation.*; | |
import org.springframework.web.servlet.view.InternalResourceViewResolver; | |
import java.nio.charset.Charset; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Configuration | |
@EnableWebMvc | |
@ComponentScan({ "me.woemler.myapp.web" }) | |
public class WebAppConfig extends WebMvcConfigurerAdapter { | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry){ | |
registry.addResourceHandler("/static/**").addResourceLocations("/static/"); | |
} | |
@Override | |
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ | |
configurer.enable(); | |
} | |
@Bean | |
public InternalResourceViewResolver getInternalResourceViewResolver(){ | |
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); | |
resolver.setPrefix("/WEB-INF/views/"); | |
resolver.setSuffix(".jsp"); | |
return resolver; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.woemler.myapp.config; | |
import org.springframework.web.WebApplicationInitializer; | |
import org.springframework.web.context.ContextLoaderListener; | |
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | |
import org.springframework.web.servlet.DispatcherServlet; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletRegistration; | |
public class WebAppInitializer implements WebApplicationInitializer { | |
@Override | |
public void onStartup(ServletContext servletContext){ | |
// Root context | |
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); | |
rootContext.register(ApplicationConfig.class, DataSourceConfig.class); | |
servletContext.addListener(new ContextLoaderListener(rootContext)); | |
// Web app context | |
AnnotationConfigWebApplicationContext webAppContext = new AnnotationConfigWebApplicationContext(); | |
webAppContext.register(WebAppConfig.class, SwaggerConfig.class); | |
// Dispatcher servlet | |
DispatcherServlet dispatcher = new DispatcherServlet(webAppContext); | |
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcherServlet", dispatcher); | |
registration.setLoadOnStartup(1); | |
registration.addMapping("/"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment