Add custom behavior to all SolrRepositories as described in Spring Data Commons reference manual

  • Download Gist
ITestSolrRepositoriesWithCustomBehavior.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
* Copyright 2012 - 2013 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.data.solr.repository.support;
 
import java.io.IOException;
 
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
/**
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ITestSolrRepositoriesWithCustomBehavior {
 
@Autowired
private ProductRepositoryWithCustomBehavior repo;
 
@Autowired
private SolrServer solrServer;
 
@Test
public void testCustomBehavior() throws SolrServerException, IOException {
repo.ping();
Mockito.verify(solrServer, Mockito.times(1)).ping();
}
 
}
ProductRepositoryWithCustomBehavior.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
* Copyright 2012 - 2013 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.data.solr.repository.support;
 
import org.springframework.data.solr.repository.ProductBean;
 
public interface ProductRepositoryWithCustomBehavior extends SolrCustomRepository<ProductBean, String> {
 
}
SolrCustomRepository.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
* Copyright 2012 - 2013 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.data.solr.repository.support;
 
import java.io.Serializable;
 
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.solr.repository.SolrRepository;
 
@NoRepositoryBean
public interface SolrCustomRepository<T, ID extends Serializable> extends SolrRepository<T, ID> {
 
SolrPingResponse ping();
 
}
SolrCustomRepositoryFactoryBean.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
* Copyright 2012 - 2013 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.data.solr.repository.support;
 
import java.io.Serializable;
 
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.solr.core.SolrOperations;
import org.springframework.data.solr.repository.SolrRepository;
 
/**
* @author Christoph Strobl
*/
public class SolrCustomRepositoryFactoryBean<R extends SolrRepository<T, ID>, T, ID extends Serializable> extends
SolrRepositoryFactoryBean<R, T, ID> {
 
@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new SolrCustomRepositoryFactory<T, ID>(getSolrOperations());
}
 
private static class SolrCustomRepositoryFactory<T, ID extends Serializable> extends SolrRepositoryFactory {
 
private final SolrOperations solrOperations;
 
public SolrCustomRepositoryFactory(SolrOperations solrOperations) {
super(solrOperations);
this.solrOperations = solrOperations;
}
 
@SuppressWarnings("unchecked")
protected Object getTargetRepository(RepositoryMetadata metadata) {
return new SolrCustomRepositoryImpl<T, ID>(solrOperations, (Class<T>) metadata.getDomainType());
}
 
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return SolrCustomRepository.class;
}
 
}
 
}
SolrCustomRepositoryImpl.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
* Copyright 2012 - 2013 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.data.solr.repository.support;
 
import java.io.Serializable;
 
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.springframework.data.solr.core.SolrOperations;
 
public class SolrCustomRepositoryImpl<T, ID extends Serializable> extends SimpleSolrRepository<T, ID> implements
SolrCustomRepository<T, ID> {
 
private SolrOperations solrOperations;
 
public SolrCustomRepositoryImpl(SolrOperations solrOperations, Class<T> entityClass) {
super(solrOperations, entityClass);
this.solrOperations = solrOperations;
}
 
@Override
public SolrPingResponse ping() {
return solrOperations.ping();
}
 
}
context.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
<solr:repositories base-package="org.springframework.data.solr.repository.support" factory-class="org.springframework.data.solr.repository.support.SolrCustomRepositoryFactoryBean" />
<!-- used a mock here for testing -->
<bean id="solrServer" class="org.springframework.data.solr.test.util.GenericMockFactory">
<property name="type" value="org.apache.solr.client.solrj.SolrServer" />
</bean>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.