在我的play框架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的spring配置:
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()中执行以下操作,尝试在spring应用程序上下文中使用play frameworks类加载器
ctx = new AnnotationConfigApplicationContext(SolrConfig.class);
((AnnotationConfigApplicationContext)ctx).setClassLoader( Play.application().classloader() );这导致了相同的NoClassDefFoundError和堆栈跟踪。在设置类加载器之后,我还尝试在这个相同的函数中执行查询,但再次收到相同的错误。
发布于 2015-07-17 18:41:17
一种解决方法是在MappingMongoConverter中强制ReflectionEntityInstantiator,它将恢复到1.7.0之前的版本。基于反射的reverts实例化策略。例如:
mappingConverter.setInstantiators(
new EntityInstantiators(ReflectionEntityInstantiator.INSTANCE));https://stackoverflow.com/questions/30632240
复制相似问题