Skip to content

Commit

Permalink
Set proxyBeanMethods to false (PebbleTemplates#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebussieres authored May 2, 2020
1 parent 3756cf2 commit 80d5896
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 189 deletions.
2 changes: 1 addition & 1 deletion pebble-spring/pebble-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<url>http://pebbletemplates.io</url>

<properties>
<boot.version>2.2.1.RELEASE</boot.version>
<boot.version>2.2.6.RELEASE</boot.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mitchellbosecke.pebble.boot.autoconfigure;

abstract class AbstractPebbleConfiguration {

protected String stripLeadingSlash(String value) {
if (value == null) {
return null;
}
if (value.startsWith("/")) {
return value.substring(1);
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,146 +5,58 @@
import com.mitchellbosecke.pebble.loader.ClasspathLoader;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.spring.extension.SpringExtension;
import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver;
import com.mitchellbosecke.pebble.spring.servlet.PebbleViewResolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import java.util.List;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.lang.Nullable;

import java.util.List;

@Configuration
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(PebbleEngine.class)
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
@EnableConfigurationProperties(PebbleProperties.class)
public class PebbleAutoConfiguration {
@Import({PebbleServletWebConfiguration.class, PebbleReactiveWebConfiguration.class})
public class PebbleAutoConfiguration extends AbstractPebbleConfiguration {

@Configuration
@Bean
@ConditionalOnMissingBean(name = "pebbleLoader")
public static class DefaultLoaderConfiguration {

@Autowired
private PebbleProperties properties;

@Bean
public Loader<?> pebbleLoader() {
ClasspathLoader loader = new ClasspathLoader();
loader.setCharset(this.properties.getCharsetName());
// classpath loader does not like leading slashes in resource paths
loader.setPrefix(stripLeadingSlash(this.properties.getPrefix()));
loader.setSuffix(this.properties.getSuffix());
return loader;
}
}

@Configuration
@ConditionalOnMissingBean(name = "pebbleEngine")
public static class PebbleDefaultConfiguration {

@Autowired
private PebbleProperties properties;

@Autowired
private Loader<?> pebbleLoader;

@Autowired(required = false)
private List<Extension> extensions;

@Bean
public SpringExtension pebbleSpringExtension() {
return new SpringExtension();
}

@Bean
public PebbleEngine pebbleEngine() {
PebbleEngine.Builder builder = new PebbleEngine.Builder();
builder.loader(this.pebbleLoader);
builder.extension(this.pebbleSpringExtension());
if (this.extensions != null && !this.extensions.isEmpty()) {
builder.extension(this.extensions.toArray(new Extension[this.extensions.size()]));
}
if (!this.properties.isCache()) {
builder.cacheActive(false);
}
if (this.properties.getDefaultLocale() != null) {
builder.defaultLocale(this.properties.getDefaultLocale());
}
builder.strictVariables(this.properties.isStrictVariables());
builder.greedyMatchMethod(this.properties.isGreedyMatchMethod());
return builder.build();
}
public Loader<?> pebbleLoader(PebbleProperties properties) {
ClasspathLoader loader = new ClasspathLoader();
loader.setCharset(properties.getCharsetName());
// classpath loader does not like leading slashes in resource paths
loader.setPrefix(this.stripLeadingSlash(properties.getPrefix()));
loader.setSuffix(properties.getSuffix());
return loader;
}

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
public static class PebbleWebMvcConfiguration {

@Autowired
private PebbleProperties properties;

@Autowired
private PebbleEngine pebbleEngine;

@Bean
@ConditionalOnMissingBean(name = "pebbleViewResolver")
public PebbleViewResolver pebbleViewResolver() {
PebbleViewResolver pvr = new PebbleViewResolver();
this.properties.applyToMvcViewResolver(pvr);

pvr.setPebbleEngine(this.pebbleEngine);
if (this.pebbleEngine.getLoader() instanceof ClasspathLoader) {
// classpathloader doesn't like leading slashes in paths
pvr.setPrefix(stripLeadingSlash(this.properties.getPrefix()));
}

return pvr;
}
@Bean
@ConditionalOnMissingBean
public SpringExtension pebbleSpringExtension() {
return new SpringExtension();
}

@Configuration
@ConditionalOnWebApplication(type = Type.REACTIVE)
public static class PebbleReactiveConfiguration {

@Autowired
private PebbleProperties properties;

@Autowired
private PebbleEngine pebbleEngine;

@Bean
@ConditionalOnMissingBean(name = "pebbleReactiveViewResolver")
public PebbleReactiveViewResolver pebbleReactiveViewResolver() {
String prefix = this.properties.getPrefix();
if (this.pebbleEngine.getLoader() instanceof ClasspathLoader) {
// classpathloader doesn't like leading slashes in paths
prefix = stripLeadingSlash(this.properties.getPrefix());
}
PebbleReactiveViewResolver resolver = new PebbleReactiveViewResolver(this.pebbleEngine);
resolver.setPrefix(prefix);
resolver.setSuffix(this.properties.getSuffix());
resolver.setViewNames(this.properties.getViewNames());
resolver.setRequestContextAttribute(this.properties.getRequestContextAttribute());
return resolver;
@Bean
@ConditionalOnMissingBean(name = "pebbleEngine")
public PebbleEngine pebbleEngine(PebbleProperties properties,
Loader<?> pebbleLoader,
SpringExtension springExtension,
@Nullable List<Extension> extensions) {
PebbleEngine.Builder builder = new PebbleEngine.Builder();
builder.loader(pebbleLoader);
builder.extension(springExtension);
if (extensions != null && !extensions.isEmpty()) {
builder.extension(extensions.toArray(new Extension[extensions.size()]));
}
}

private static String stripLeadingSlash(String value) {
if (value == null) {
return null;
if (!properties.isCache()) {
builder.cacheActive(false);
}
if (value.startsWith("/")) {
return value.substring(1);
if (properties.getDefaultLocale() != null) {
builder.defaultLocale(properties.getDefaultLocale());
}
return value;
builder.strictVariables(properties.isStrictVariables());
builder.greedyMatchMethod(properties.isGreedyMatchMethod());
return builder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mitchellbosecke.pebble.boot.autoconfigure;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.loader.ClasspathLoader;
import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.REACTIVE)
class PebbleReactiveWebConfiguration extends AbstractPebbleConfiguration {

@Bean
@ConditionalOnMissingBean(name = "pebbleReactiveViewResolver")
PebbleReactiveViewResolver pebbleReactiveViewResolver(PebbleProperties properties,
PebbleEngine pebbleEngine) {
String prefix = properties.getPrefix();
if (pebbleEngine.getLoader() instanceof ClasspathLoader) {
// classpathloader doesn't like leading slashes in paths
prefix = this.stripLeadingSlash(properties.getPrefix());
}
PebbleReactiveViewResolver resolver = new PebbleReactiveViewResolver(pebbleEngine);
resolver.setPrefix(prefix);
resolver.setSuffix(properties.getSuffix());
resolver.setViewNames(properties.getViewNames());
resolver.setRequestContextAttribute(properties.getRequestContextAttribute());
return resolver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mitchellbosecke.pebble.boot.autoconfigure;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.loader.ClasspathLoader;
import com.mitchellbosecke.pebble.spring.servlet.PebbleViewResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
class PebbleServletWebConfiguration extends AbstractPebbleConfiguration {

@Bean
@ConditionalOnMissingBean(name = "pebbleViewResolver")
PebbleViewResolver pebbleViewResolver(PebbleProperties properties,
PebbleEngine pebbleEngine) {
PebbleViewResolver pvr = new PebbleViewResolver();
properties.applyToMvcViewResolver(pvr);

pvr.setPebbleEngine(pebbleEngine);
if (pebbleEngine.getLoader() instanceof ClasspathLoader) {
// classpathloader doesn't like leading slashes in paths
pvr.setPrefix(this.stripLeadingSlash(properties.getPrefix()));
}

return pvr;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.mitchellbosecke.pebble.boot.autoconfigure;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.boot.NonWebApplication;
import static org.assertj.core.api.Assertions.assertThat;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.boot.Application;
import java.io.StringWriter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.StringWriter;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = NonWebApplication.class)
@SpringBootTest(classes = Application.class,
properties = "spring.main.web-application-type=none")
class NonWebAppTests {

@Autowired
Expand Down
Loading

0 comments on commit 80d5896

Please sign in to comment.