From a7fce6b0485cf7455c0a281adbcc17ed241b2592 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Mon, 16 Jan 2017 00:39:05 +0100 Subject: [PATCH 1/3] Document use of `OpenEntityManagerInViewInterceptor` Closes gh-7993 --- .../src/main/asciidoc/spring-boot-features.adoc | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 c4c0e71e9e..f28f5d6143 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -3060,6 +3060,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 From 600e74b6c94c38fab787b9092b498de9dcbde074 Mon Sep 17 00:00:00 2001 From: Karsten Sperling Date: Thu, 7 Jan 2016 22:57:20 +1300 Subject: [PATCH 2/3] Avoid double-reg when a Servlet or Filter implements ServletContextInitializer See gh-4902 --- .../boot/web/servlet/ServletContextInitializerBeans.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); } } From 5158f6e5fc7516a69a821e411711617ef0bcab4f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 13 Jan 2017 19:21:25 -0500 Subject: [PATCH 3/3] Test that double registration of ServletContextInitializers does not happen Closes gh-4902 --- .../ServletContextInitializerBeansTests.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java 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() { + + } + + } + +}