Skip to content

Commit 4fbaaea

Browse files
committed
Revised to not be an auto configuration class so much as a regular @configuration class that uses Spring Boot annotations.
1 parent 04a5b97 commit 4fbaaea

File tree

3 files changed

+47
-63
lines changed

3 files changed

+47
-63
lines changed

velocity/src/main/java/autoconfigure/velocity/VelocityAutoConfiguration.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

velocity/src/main/java/demo/Application.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
56
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
8+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
9+
import org.springframework.boot.bind.RelaxedPropertyResolver;
610
import org.springframework.boot.builder.SpringApplicationBuilder;
711
import org.springframework.boot.context.web.SpringBootServletInitializer;
12+
import org.springframework.context.EnvironmentAware;
13+
import org.springframework.context.annotation.Bean;
814
import org.springframework.context.annotation.ComponentScan;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.core.Ordered;
17+
import org.springframework.core.env.Environment;
918
import org.springframework.data.jpa.repository.JpaRepository;
1019
import org.springframework.stereotype.Controller;
1120
import org.springframework.ui.Model;
1221
import org.springframework.web.bind.annotation.RequestBody;
1322
import org.springframework.web.bind.annotation.RequestMapping;
1423
import org.springframework.web.bind.annotation.RequestMethod;
1524
import org.springframework.web.bind.annotation.RestController;
25+
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
26+
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
1627

1728
import javax.persistence.Entity;
1829
import javax.persistence.GeneratedValue;
1930
import javax.persistence.Id;
31+
import javax.servlet.Servlet;
2032
import java.util.*;
2133

2234

@@ -37,6 +49,41 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
3749

3850
}
3951

52+
@Configuration
53+
@ConditionalOnClass({Servlet.class})
54+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
55+
class VelocityAutoConfiguration implements EnvironmentAware {
56+
57+
public static final String DEFAULT_PREFIX = "/templates/";
58+
59+
public static final String DEFAULT_SUFFIX = ".vm";
60+
61+
private RelaxedPropertyResolver environment;
62+
63+
@Override
64+
public void setEnvironment(Environment environment) {
65+
this.environment = new RelaxedPropertyResolver(environment,
66+
"spring.velocity.");
67+
}
68+
69+
@Bean
70+
VelocityConfigurer velocityConfig() {
71+
return new VelocityConfigurer();
72+
}
73+
74+
@Bean
75+
VelocityViewResolver velocityViewResolver() {
76+
VelocityViewResolver resolver = new VelocityViewResolver();
77+
resolver.setSuffix(this.environment.getProperty("suffix", DEFAULT_SUFFIX));
78+
resolver.setPrefix(this.environment.getProperty("prefix", DEFAULT_PREFIX));
79+
// Needs to come before any fallback resolver (e.g. a
80+
// InternalResourceViewResolver)
81+
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20);
82+
return resolver;
83+
84+
}
85+
}
86+
4087
/**
4188
* Represents one (very under-specified) <em>booking</em>
4289
* for a named person at a theoretical establishment like a restaurant.

velocity/src/main/resources/META-INF/spring.factories

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)