forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto configuration support for Spring Data Solr
Registers required components in application context if not available to set up environment for usage with Spring Data Solr. Will listen on SolrServer and SolrRepositories for configuration. By default an HttpSolrServer is registered unless a zkHost (zookeeper host) is defined. In that case an instance of CloudSolrServer will be created. By default multicore support is enabled, creating instances of SolrServer for each core defined via @SolrDocument.
- Loading branch information
1 parent
40b55b0
commit 6ed6970
Showing
21 changed files
with
797 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...n/java/org/springframework/boot/autoconfigure/data/SolrRepositoriesAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 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; | ||
|
||
import org.apache.solr.client.solrj.SolrServer; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.solr.repository.SolrRepository; | ||
import org.springframework.data.solr.repository.support.SolrRepositoryFactoryBean; | ||
|
||
/** | ||
* Enables auto configuration for Spring Data Solr repositories. | ||
* <p> | ||
* Activates when there is no bean of type | ||
* {@link org.springframework.data.solr.repository.support.SolrRepositoryFactoryBean} | ||
* found in context, and both | ||
* {@link org.springframework.data.solr.repository.SolrRepository} and | ||
* {@link org.apache.solr.client.solrj.SolrServer} can be found on classpath. | ||
* </p> | ||
* If active auto configuration does the same as | ||
* {@link org.springframework.data.solr.repository.config.EnableSolrRepositories} would | ||
* do. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass({ SolrServer.class, SolrRepository.class }) | ||
@ConditionalOnMissingBean(SolrRepositoryFactoryBean.class) | ||
@Import(SolrRepositoriesAutoConfigureRegstrar.class) | ||
public class SolrRepositoriesAutoConfiguration { | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...va/org/springframework/boot/autoconfigure/data/SolrRepositoriesAutoConfigureRegstrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 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; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.data.repository.config.RepositoryConfigurationExtension; | ||
import org.springframework.data.solr.repository.config.EnableSolrRepositories; | ||
import org.springframework.data.solr.repository.config.SolrRepositoryConfigExtension; | ||
|
||
/** | ||
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Solr | ||
* repositories. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
public class SolrRepositoriesAutoConfigureRegstrar extends | ||
AbstractRepositoryConfigurationSourceSupport { | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see org.springframework.boot.autoconfigure.data. | ||
* AbstractRepositoryConfigurationSourceSupport#getAnnotation() | ||
*/ | ||
@Override | ||
protected Class<? extends Annotation> getAnnotation() { | ||
return EnableSolrRepositories.class; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see org.springframework.boot.autoconfigure.data. | ||
* AbstractRepositoryConfigurationSourceSupport#getConfiguration() | ||
*/ | ||
@Override | ||
protected Class<?> getConfiguration() { | ||
return EnableSolrRepositoriesConfiguration.class; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see org.springframework.boot.autoconfigure.data. | ||
* AbstractRepositoryConfigurationSourceSupport#getRepositoryConfigurationExtension() | ||
*/ | ||
@Override | ||
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() { | ||
return new SolrRepositoryConfigExtension(); | ||
} | ||
|
||
@EnableSolrRepositories(multicoreSupport = true) | ||
private static class EnableSolrRepositoriesConfiguration { | ||
|
||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...gure/src/main/java/org/springframework/boot/autoconfigure/solr/SolrAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 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.solr; | ||
|
||
import javax.annotation.PreDestroy; | ||
|
||
import org.apache.solr.client.solrj.SolrServer; | ||
import org.apache.solr.client.solrj.impl.CloudSolrServer; | ||
import org.apache.solr.client.solrj.impl.HttpSolrServer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Enables auto configuration for Solr. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(SolrServer.class) | ||
@EnableConfigurationProperties(SolrProperties.class) | ||
public class SolrAutoConfiguration { | ||
|
||
private @Autowired SolrProperties properties; | ||
|
||
private SolrServer solrServer; | ||
|
||
@PreDestroy | ||
public void close() { | ||
if (this.solrServer != null) { | ||
this.solrServer.shutdown(); | ||
} | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public SolrServer solrServer() { | ||
|
||
this.solrServer = createSolrServer(); | ||
return this.solrServer; | ||
} | ||
|
||
private SolrServer createSolrServer() { | ||
|
||
if (StringUtils.hasText(this.properties.getZkHost())) { | ||
return new CloudSolrServer(this.properties.getZkHost()); | ||
} | ||
return new HttpSolrServer(this.properties.getHost()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...toconfigure/src/main/java/org/springframework/boot/autoconfigure/solr/SolrProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 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.solr; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* Configuration properties for Solr. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
@ConfigurationProperties(prefix = "spring.data.solr") | ||
public class SolrProperties { | ||
|
||
private String host = "http://127.0.0.1:8983/solr"; | ||
|
||
private String zkHost; | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public String getZkHost() { | ||
return zkHost; | ||
} | ||
|
||
public void setZkHost(String zkHost) { | ||
this.zkHost = zkHost; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...a/org/springframework/boot/autoconfigure/data/SolrRepositoriesAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 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; | ||
|
||
import static org.hamcrest.core.IsInstanceOf.*; | ||
import static org.hamcrest.core.IsNull.*; | ||
import static org.junit.Assert.*; | ||
|
||
import org.apache.solr.client.solrj.SolrServer; | ||
import org.apache.solr.client.solrj.impl.HttpSolrServer; | ||
import org.junit.Test; | ||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; | ||
import org.springframework.boot.autoconfigure.data.alt.CitySolrRepository; | ||
import org.springframework.boot.autoconfigure.data.solr.City; | ||
import org.springframework.boot.autoconfigure.data.solr.CityRepository; | ||
import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.solr.repository.config.EnableSolrRepositories; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public class SolrRepositoriesAutoConfigurationTests { | ||
|
||
private AnnotationConfigApplicationContext context; | ||
|
||
@Test | ||
public void testDefaultRepositoryConfiguration() { | ||
|
||
initContext(TestConfiguration.class); | ||
|
||
assertThat(this.context.getBean(CityRepository.class), notNullValue()); | ||
assertThat(this.context.getBean(SolrServer.class), instanceOf(HttpSolrServer.class)); | ||
} | ||
|
||
@Test | ||
public void testNoRepositoryConfiguration() { | ||
|
||
initContext(EmptyConfiguration.class); | ||
assertThat(this.context.getBean(SolrServer.class), instanceOf(HttpSolrServer.class)); | ||
} | ||
|
||
@Test | ||
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() { | ||
|
||
initContext(CustomizedConfiguration.class); | ||
assertThat(this.context.getBean(CitySolrRepository.class), notNullValue()); | ||
} | ||
|
||
private void initContext(Class<?> configClass) { | ||
|
||
this.context = new AnnotationConfigApplicationContext(); | ||
this.context.register(configClass, SolrAutoConfiguration.class, SolrRepositoriesAutoConfiguration.class, | ||
PropertyPlaceholderAutoConfiguration.class); | ||
this.context.refresh(); | ||
} | ||
|
||
@Configuration | ||
@TestAutoConfigurationPackage(City.class) | ||
static class TestConfiguration { | ||
|
||
} | ||
|
||
@Configuration | ||
@TestAutoConfigurationPackage(SolrRepositoriesAutoConfigurationTests.class) | ||
static class EmptyConfiguration { | ||
|
||
} | ||
|
||
@Configuration | ||
@TestAutoConfigurationPackage(SolrRepositoriesAutoConfigurationTests.class) | ||
@EnableSolrRepositories(basePackageClasses = CitySolrRepository.class, multicoreSupport = true) | ||
protected static class CustomizedConfiguration { | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.