无法通过@Autowired使用Spring将Solr服务器注入应用程序
我目前在尝试将bean中的solr服务器实例注入代码时遇到问题。我得到了一个无法通过@Autowired使用Spring将Solr服务器注入应用程序,spring,maven,solr,jersey,autowired,Spring,Maven,Solr,Jersey,Autowired,我目前在尝试将bean中的solr服务器实例注入代码时遇到问题。我得到了一个NullPointerException,并检查了服务器实例本身并验证了它为null。我可以创建服务器而无需注入它 我的spring.xml文件声明bean如下: <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">` <constructor-arg index="0" value="$
NullPointerException
,并检查了服务器实例本身并验证了它为null。我可以创建服务器而无需注入它
我的spring.xml文件声明bean如下:
<bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">`
<constructor-arg index="0" value="${solr.serverUrl}" />
</bean>
<context:annotation-config />
<context:component-scan base-package="my.base.package" />
我试图注入这个bean的代码正在自动连接,如下所示:
34 @Component
35 public class GenericClassImpl implements GenericClass{
36
37 @Autowired
38 private SolrServer solrServer;
39
40 public String doQuery(String query){
41 try{
42 SolrQuery solrQuery = new SolrQuery(query);
43 QueryResponse rsp = solrServer.query(solrQuery);
44 SolrDocumentList docs = rsp.getResults();
45
46 return docs.toString();
47 }catch(Exception e){
48 e.printStackTrace();
49 }
50 }
51 }
我尝试将@限定符(“solrServer”)
与@自动连线
一起使用,但也没有成功
这一切都是使用maven完成的,maven本身似乎没有任何问题。我得到的结果是
java.lang.NullPointerException
at ...GenericClassImpl.doQuery(GenericClassImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:662)
再说一次,如果我这样做的话
SolrServer solrServer = new HttpSolrServer(serverUrl);
代码工作正常,我可以在solr服务器上执行查询。
关于我做错了什么的任何想法都不会被注入
获取GenericClassImpl
实例的代码为
public class GenericServiceImpl implements GenericService{
protected GenericClass gc;
@GET
@Path({pathParam})
public String doGet(@PathParam({pathParam}) String pathParam,
@DefaultValue("*:*") @QueryParam("q") String query){
gc = SearchFactory.getInstance().getGenericClass(pathParam);
return gc.doQuery(query);
}
}
而搜索工厂
是
public class SearchFactory{
private static SearchFactory instance;
private SearchFactory(){}
public static SearchFactory getInstance() {
if (instance == null)
instance = new SearchFactory();
return instance;
}
public GenericClass getGenericClass(String type){
GenericClass result = null;
if(type.toLowerCase().equals("case1")){
result = new GenericClassImpl1();
return result;
}
else if(type.toLowerCase().equals("case2")){
result = new GenericClassImpl2();
return result;
}
else throw new Exception();
}
}
GenericClassImpl1
和GenericClassImpl2
扩展了GenericClassImpl
,当前都是空的。为了让@Autowired
工作,您需要从Spring获取GenericClassImpl
实例。如果不这样做,Spring将不会自动关联依赖项
不能使用new
关键字实例化它们,也不能使用依赖项注入技术
您可以做的是保持对ApplicationContext
的静态引用,然后在getGenericClass
方法中从中提取相关bean的实例
正如Spring文档中所讨论的,这实际上是ServiceLocator模式的一个很好的用例。您能给我们展示一下获取GenericClassImpl实例的代码吗?我刚刚添加了获取GenericClassImpl实例的代码。谢谢我最后做的是在SearchFactory、GenericClassImpl1和GenericClassImpl2类上添加一个组件,并对其中的新实例变量使用自动连线,然后返回这些私有变量,让spring处理它们的构造并能够管理它们的生命周期
public class SearchFactory{
private static SearchFactory instance;
private SearchFactory(){}
public static SearchFactory getInstance() {
if (instance == null)
instance = new SearchFactory();
return instance;
}
public GenericClass getGenericClass(String type){
GenericClass result = null;
if(type.toLowerCase().equals("case1")){
result = new GenericClassImpl1();
return result;
}
else if(type.toLowerCase().equals("case2")){
result = new GenericClassImpl2();
return result;
}
else throw new Exception();
}
}