diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 6f02f081fa..afb9179af0 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -3054,6 +3054,16 @@ Hibernate autoconfig is active because the `ddl-auto` settings are more fine-gra +[[boot-features-jpa-in-web-environment]] +==== Open EntityManager in View +If you are running a web application, Spring Boot will by default register +{spring-javadoc}/orm/jpa/support/OpenEntityManagerInViewInterceptor.html[`OpenEntityManagerInViewInterceptor`] +to apply the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web +views. If you don't want this behavior you should set `spring.jpa.open-in-view` to +`false` in your `application.properties`. + + + [[boot-features-sql-h2-console]] === Using H2's web console The http://www.h2database.com[H2 database] provides a diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java index 9fa88ff166..3ee21b054d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -121,7 +121,7 @@ public class ServletContextInitializerBeans } else { addServletContextInitializerBean(ServletContextInitializer.class, beanName, - initializer, beanFactory, null); + initializer, beanFactory, initializer); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java b/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java new file mode 100644 index 0000000000..86446b3565 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java @@ -0,0 +1,121 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.web.servlet; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; + +import org.junit.Test; + +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ServletContextInitializerBeans}. + * + * @author Andy Wilkinson + */ +public class ServletContextInitializerBeansTests { + + private ConfigurableApplicationContext context; + + @Test + public void servletThatImplementsServletContextInitializerIsOnlyRegisteredOnce() { + load(ServletConfiguration.class); + ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans( + this.context.getBeanFactory()); + assertThat(initializerBeans.size()).isEqualTo(1); + assertThat(initializerBeans.iterator()).hasOnlyElementsOfType(TestServlet.class); + } + + @Test + public void filterThatImplementsServletContextInitializerIsOnlyRegisteredOnce() { + load(FilterConfiguration.class); + ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans( + this.context.getBeanFactory()); + assertThat(initializerBeans.size()).isEqualTo(1); + assertThat(initializerBeans.iterator()).hasOnlyElementsOfType(TestFilter.class); + } + + private void load(Class configuration) { + this.context = new AnnotationConfigApplicationContext(configuration); + } + + static class ServletConfiguration { + + @Bean + public TestServlet testServlet() { + return new TestServlet(); + } + + } + + static class FilterConfiguration { + + @Bean + public TestFilter testFilter() { + return new TestFilter(); + } + + } + + static class TestServlet extends HttpServlet implements ServletContextInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + + } + + } + + static class TestFilter implements Filter, ServletContextInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + + } + + @Override + public void destroy() { + + } + + } + +}