<p><img class="alignright wp-image-4171 size-medium" src="https://dineshonjava.com/wp-content/uploads/2018/06/Spring-Boot-Solr-Example-300x93.jpg" alt="Spring Data Solr Configuration" width="300" height="93" />In this article, we will explore more about the Spring Data Solr Configuration using <strong><a href="https://dineshonjava.com/spring-boot-tutorial/">Spring Boot</a> </strong>Application. We will see how to customize Spring Data Solr configuration using <em>@EnableSolrRepositories</em> annotation in the Spring Boot Application.</p>
<p>In the <strong><a href="https://dineshonjava.com/installing-configuring-apache-solr/">previous article</a></strong> of <strong><a href="https://dineshonjava.com/spring-data-solr-tutorial/">Spring Data Solr tutorial</a></strong>, we have seen how to install and configure Solr and how to use Solr. The Solr provides a REST-like HTTP API to execute queries to the indexed data and also to add Solr index.</p>
<div align="center"><iframe style="width: 120px; height: 240px;" src="//ws-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&;OneJS=1&;Operation=GetAdHtml&;MarketPlace=IN&;source=ac&;ref=qf_sp_asin_til&;ad_type=product_link&;tracking_id=dineshonjav06-21&;marketplace=amazon&;region=IN&;placement=1788299450&;asins=1788299450&;linkId=05b0146b0a85f4472697901e353dc276&;show_border=true&;link_opens_in_new_window=true&;price_color=333333&;title_color=0066c0&;bg_color=ffffff" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"><br />
</iframe></div>
<h2>Spring Data Solr Configuration in Spring Boot Example</h2>
<p>Spring Boot application provides auto-configuration for the Spring Data Solr by adding following <strong>Maven</strong> dependency:</p>
<pre class="highlight"><;dependency>; 
<;groupId>;org.springframework.boot<;/groupId>; 
<;artifactId>;spring-boot-starter-data-solr<;/artifactId>; 
<;/dependency>; 
</pre>
<p>The above maven dependency of the Spring Data Solr starter with groupId <em>org.springframework.boot</em> and artifactId <em>spring-boot-starter-data-solr</em> includes the Spring Solr Data into your Spring Boot application.</p>
<p>Same dependency configuration we can use configure with the <strong>Gradle</strong> as following:</p>
<pre class="highlight">dependencies { 
 ... 
	compile('org.springframework.boot:spring-boot-starter-data-solr') 
 ... 
} 
</pre>
<p>The above maven dependency provide auto-configure Solr and you have to configure a property either in <em>application.properties</em> or in <em>application.yml</em> file. Let&#8217;s see the following configuration file:</p>
<p><strong>application.properties</strong></p>
<pre class="highlight">spring.data.solr.host=http://localhost:8983/solr/ 
</pre>
<p>Same configuration in the YML format as look like following:</p>
<p><strong>application.yml</strong></p>
<pre class="highlight">spring: 
	data: 
		solr: 
			host: http://localhost:8983/solr/ 
</pre>
<h3>Spring Data Solr Java Configuration</h3>
<p>Till now we have seen default auto-configuration of the Spring Data Solr provided by the Spring Boot application. If you want to customize this configuration, Spring Boot also allows you to customize it by creating a configuration class following:</p>
<pre class="highlight">package com.doj.app.config; 
 
import org.apache.solr.client.solrj.SolrClient; 
import org.apache.solr.client.solrj.impl.HttpSolrClient; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.solr.core.SolrTemplate; 
import org.springframework.data.solr.repository.config.EnableSolrRepositories; 
 
/** 
* @author Dinesh.Rajput 
* 
*/ 
@Configuration 
@EnableSolrRepositories( 
basePackages = "com.doj.app.repository", 
namedQueriesLocation = "classpath:solr-named-queries.properties") 
@ComponentScan 
public class SolrConfig { 
 
@Value("spring.data.solr.host") String solrURL; 
 
@Bean 
public SolrClient solrClient() { 
return new HttpSolrClient.Builder(solrURL).build(); 
} 
 
@Bean 
public SolrTemplate solrTemplate(SolrClient client) throws Exception { 
return new SolrTemplate(client); 
} 
} 
</pre>
<p>In the above configuration file, we have used <em>@EnableSolrRepositories</em> annotation to scan the packages for repositories. This annotation enables Spring Data Solr repositories and configuring the root package of our Solr repositories. And also this annotation uses <em>namedQueriesLocation</em> attribute to provide location of configuration file for named queries.</p>
<p>In the configuration class, we have created a method called <em>solrClient()</em> and annotate this method with the <em>@Bean</em> annotation. The implementation of this method creates a new <em>HttpSolrClient</em> using Builder and passed the <em>solrURL</em> of hosted Solr Server.</p>
<p>In the configuration class, we have created a method called <em>solrTemplate()</em> and annotate this method with the <em>@Bean</em> annotation. The implementation of this method creates a new <em>SolrTemplate</em> object and passed the created <em>SolrClient</em> implementation as a constructor argument.</p>
<h3>Enabling/Disabling Solr Repository in Spring Boot Application</h3>
<p>Spring Boot also allows you to disable and enable Solr repository by setting following property either in application.properties or in application.yml file.</p>
<p><strong>In application.properties</strong></p>
<pre class="highlight">spring.data.solr.repositories.enabled=false 
</pre>
<p><strong>In application.yml</strong></p>
<pre class="highlight">spring: 
	data: 
		solr: 
			repositories: 
				enabled: false 
</pre>
<p>By default, this property set to <em>true</em>.</p>
<p>We have seen in this tutorial about basic of the Spring Data Solr configuration in the Spring Boot application.</p>
<div align="center"><iframe style="width: 120px; height: 240px;" src="//ws-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&;OneJS=1&;Operation=GetAdHtml&;MarketPlace=IN&;source=ac&;ref=tf_til&;ad_type=product_link&;tracking_id=dineshonjav06-21&;marketplace=amazon&;region=IN&;placement=1787127567&;asins=1787127567&;linkId=a1e64f621b5e6128d8cb10e876eb1c48&;show_border=true&;link_opens_in_new_window=true&;price_color=333333&;title_color=0066c0&;bg_color=ffffff" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span><br />
</iframe></div>
<p>I hope, this article have given better understanding about the Spring Data Solr Configuration in your Spring Boot Application. We have now successfully obtained the required dependencies with Maven and configured Spring Data Solr for your.</p>
<p>In the next article, we will see how to create Spring Data Solr Repositories.</p>
<p>Thanks for learning with the <em><strong>Dineshonjava</strong></em>.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/installing-configuring-apache-solr/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/creating-spring-data-solr-repositories/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4170'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…