Skip to content

Instantly share code, notes, and snippets.

@chrisi
Created April 24, 2014 10:38
Show Gist options
  • Save chrisi/11249866 to your computer and use it in GitHub Desktop.
Save chrisi/11249866 to your computer and use it in GitHub Desktop.
Spring Boot Velocity Autoconfigurer
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.gtidev.sandbox</groupId>
<artifactId>boot101</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>
<dependencies>
<dependency>
<groupId>net.gtidev.sandbox</groupId>
<artifactId>plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
package net.gtidev.sandbox.core;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PluginController {
@RequestMapping("/index")
String home() {
return "index";
}
}
package net.gtidev.sandbox.core;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan("net.gtidev.sandbox")
public class SpringConfig {
}
package net.gtidev.sandbox.core;
import org.springframework.boot.SpringApplication;
public class Starter {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringConfig.class, args);
}
}
package net.gtidev.spring.boot.autoconfigure;
import javax.servlet.Servlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.velocity.VelocityConfig;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityToolboxView;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
/**
* Minimal Spring-Boot Auto-Configuration for the Velocity Template Engine
*
* @author Christian Gebauer
*/
@Configuration
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class VelocityAutoConfiguration {
@Configuration
@ConditionalOnClass({ Servlet.class })
public static class DefaultTemplateResolverConfiguration {
@Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
@Bean
public VelocityConfig velocityConfig() {
VelocityConfigurer cfg = new VelocityConfigurer();
cfg.setResourceLoader(resourceLoader);
return cfg;
}
@Bean
public ViewResolver viewResolver() {
VelocityViewResolver resolver = new VelocityViewResolver();
resolver.setViewClass(VelocityToolboxView.class);
resolver.setPrefix("/templates/");
resolver.setSuffix(".vm");
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20);
return resolver;
}
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.gtidev.spring.boot.autoconfigure.VelocityAutoConfiguration
<h2>Static Page from the Core-Module</h2>
<h2>Dynamic Page from the Core-Module</h2>
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.gtidev.sandbox</groupId>
<artifactId>boot101</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>plugin</artifactId>
</project>
package net.gtidev.sandbox.plugin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DefaultController {
@RequestMapping("/plugin")
String home() {
return "plugin";
}
}
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.1.RELEASE</version>
</parent>
<groupId>net.gtidev.sandbox</groupId>
<artifactId>boot101</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<modules>
<module>core</module>
<module>plugin</module>
</modules>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment