在我的play framework v2.3.8应用程序中进行spring-data-solr查询时,我一直遇到问题。查询似乎成功但是然后类加载器spring正在使用无法找到我的数据模型类的类文件并抛出NoClassDefFoundError
。这是一些相关的代码。
首先是'model'包中的数据模型:
package model;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
public class Theatre
{
@Id
String id;
@Field
String docType;
// other fields, getters, and setters omitted for brevity
}
这是存储库:
package model;
import java.util.List;
import org.springframework.data.solr.repository.SolrCrudRepository;
public interface TheatreRepository extends SolrCrudRepository<Theatre, String>
{
List<Theatre> findByDocType(String doc__type);
void delete(Theatre deleted);
List<Theatre> findAll();
Theatre findOne(String id);
@SuppressWarnings("unchecked")
Theatre save(Theatre persisted);
}
Play框架中的Application类:
package controllers;
import java.util.ArrayList;
import java.util.List;
import model.Theatre;
import model.TheatreRepository;
import play.*;
import plugins.solr.SolrPlugin;
import views.html.*;
public class Application extends Controller {
public static Result index() {
TheatreRepository repository = Play.application().plugin( SolrPlugin.class ).getTheatreRepository();
if( repository != null )
{
List<Theatre> allTheatres = null;
try
{
allTheatres = repository.findByDocType("theatre");
Logger.info( "List of theatres found: " + allTheatres.toString() );
for(Theatre theatre: allTheatres)
{
Logger.info( "This theatre is: " + theatre.toString() );
}
}
catch( NoClassDefFoundError | ClassNotFoundException e )
{
Logger.error( "Classpath: " + System.getProperty( "java.class.path" ) );
}
}
else
{
Logger.error( "TheatreRepository is NULL!" );
}
return ok(index.render("Your new application is ready."));
}
}
Spring的ApplicationContext:
package plugins.solr;
import play.Plugin;
import model.TheatreRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import play.Logger;
import play.Play;
import play.api.Application;
public class SolrPlugin extends Plugin
{
private TheatreRepository theatreRepository;
ApplicationContext ctx;
public SolrPlugin(Application app)
{
}
public TheatreRepository getTheatreRepository()
{
return theatreRepository;
}
@Override
public void onStart()
{
super.onStart();
Logger.info( "Solr plugin started" );
ctx = new AnnotationConfigApplicationContext(SolrConfig.class);
theatreRepository = ctx.getBean( TheatreRepository.class );
}
}
这是基于JavaConfig的弹簧配置:
package plugins.solr;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.solr.core.SolrOperations;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.data.solr.server.support.HttpSolrServerFactory;
import play.Play;
@ComponentScan
@Configuration
@EnableSolrRepositories(basePackages = { "model" })
public class SolrConfig
{
public SolrConfig()
{
}
@Bean
public SolrServer solrServer()
{
HttpSolrServerFactory factory = new HttpSolrServerFactory( new HttpSolrServer( Play.application().configuration().getString( "ems.solr.url" ) ) );
return factory.getSolrServer();
}
@Bean
public SolrOperations solrTemplate()
{
return new SolrTemplate( this.solrServer() );
}
}
因此,在重新加载浏览器窗口时,在运行时调用Application.index()
,结果调用theatreRepository.findByDocType("theatre");
。这是该调用的堆栈跟踪:
java.lang.NoClassDefFoundError: model/Theatre
at model.Theatre_Instantiator_982rn.newInstance(Unknown Source) ~[na:na]
at org.springframework.data.convert.BytecodeGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(BytecodeGeneratingEntityInstantiator.java:193) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
at org.springframework.data.convert.BytecodeGeneratingEntityInstantiator.createInstance(BytecodeGeneratingEntityInstantiator.java:76) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:127) ~[spring-data-solr-1.4.0.RELEASE.jar:na]
at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:119) ~[spring-data-solr-1.4.0.RELEASE.jar:na]
Caused by: java.lang.ClassNotFoundException: model.Theatre
at java.lang.ClassLoader.findClass(ClassLoader.java:531) ~[na:1.7.0_80]
at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[na:1.7.0_80]
at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[na:1.7.0_80]
at model.Theatre_Instantiator_982rn.newInstance(Unknown Source) ~[na:na]
at org.springframework.data.convert.BytecodeGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(BytecodeGeneratingEntityInstantiator.java:193) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
我注意到model.Theatre
类文件在play frameworks类加载器中,但在系统类加载器中没有。但是,这似乎不应该是一个问题,因为Spring需要在实例化该类的实例时找到TheatreRespoitory
类文件。我认为必须有一些简单的事情,我做错了但是在尝试了一些事情并进行研究后,我仍然感到茫然。
我确实找到了以下帖子,但它似乎没有相关性,因为它引用了spring-data-mongodb版本之间的差异。
另外,我刚试过在SolrPlugin.onStart()
ctx = new AnnotationConfigApplicationContext(SolrConfig.class);
((AnnotationConfigApplicationContext)ctx).setClassLoader( Play.application().classloader() );
这导致了相同的NoClassDefFoundError
和堆栈跟踪。我也尝试在设置类加载器后立即在同一个函数中进行查询,并再次收到相同的错误。
答案 0 :(得分:0)
解决方法是在ReflectionEntityInstantiator
中强制MappingMongoConverter
恢复到基于反射的1.7.0.RELEASE实例化策略。例如:
mappingConverter.setInstantiators(
new EntityInstantiators(ReflectionEntityInstantiator.INSTANCE));