Created
May 28, 2013 04:45
-
-
Save christophstrobl/5660566 to your computer and use it in GitHub Desktop.
Add custom behavior to all SolrRepositories as described in Spring Data Commons reference manual
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
<?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> |
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
/* | |
* 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(); | |
} | |
} |
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
/* | |
* 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> { | |
} |
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
/* | |
* 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(); | |
} |
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
/* | |
* 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; | |
} | |
} | |
} |
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
/* | |
* 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment