Separate out the auto-configuration of Spring Data’s web support
Previously, Spring Data’s web support was auto-configured as part of the JPA repositories auto-configuration. However, Spring Data’s web support isn’t dependent on the use of Spring Data JPA or even repositories. This commit moves the auto-configuration into a standalone class, making it independent of the use of Spring Data JPA and Spring Data repositories. Closes gh-1097pull/1225/merge
parent
f9010c18cc
commit
4b9f14f55b
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.autoconfigure.data.web;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
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.data.rest.RepositoryRestMvcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
|
||||
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's web support.
|
||||
* <p>
|
||||
* When in effect, the auto-configuration is the equivalent of enabling Spring Data's web
|
||||
* support through the {@link EnableSpringDataWebSupport} annotation.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSpringDataWebSupport
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(PageableHandlerMethodArgumentResolver.class)
|
||||
@ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class)
|
||||
@AutoConfigureAfter(RepositoryRestMvcAutoConfiguration.class)
|
||||
public class SpringDataWebAutoConfiguration {
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.autoconfigure.data.web;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringDataWebAutoConfiguration}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class SpringDataWebAutoConfigurationTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webSupportIsAutoConfiguredInWebApplicationContexts() {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
((AnnotationConfigWebApplicationContext) this.context)
|
||||
.register(SpringDataWebAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
((AnnotationConfigWebApplicationContext) this.context)
|
||||
.setServletContext(new MockServletContext());
|
||||
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
|
||||
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
|
||||
assertThat(beans.size(), is(equalTo(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoConfigurationBacksOffInNonWebApplicationContexts() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
((AnnotationConfigApplicationContext) this.context)
|
||||
.register(SpringDataWebAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
|
||||
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
|
||||
assertThat(beans.size(), is(equalTo(0)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue