0

現在、solr サーバー インスタンスを Bean からコードに挿入しようとしているときに問題が発生しています。を取得し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" />

${solr.serverUrl}が正しいことが確認された場所。

web.xml ファイルには Context Listener が含まれています。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
</context-param> 

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>     

<servlet>
    .
    <servlet-class>
        com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    .
</servlet>

この Bean を注入しようとしているコードは@Autowired次のとおりです。

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    }

@Qualifier("solrServer")を と一緒に使用しようとしました@Autowiredが、どちらも機能しませんでした。

これはすべて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);
        }
    }

そして、SearchFactory

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();
    }
}

GenericClassImpl1GenericClassImpl2拡張との両方GenericClassImplが現在空です。

4

1 に答える 1

0

動作させるには、Springからインスタンス@Autowiredを取得する必要があります。GenericClassImplこれを行わないと、Springは依存関係を自動配線しません。

newキーワードを使用してそれらをインスタンス化し、依存性注入手法を使用することはできません。

あなたができることは、あなたへの静的参照を保持し、次にあなたのメソッドApplicationContextでそれらから関連するBeanのインスタンスをプルすることです。getGenericClass

Springのドキュメントで説明されているように、これは実際にはServiceLocatorパターンの優れたユースケースです。

于 2012-06-28T18:25:00.967 に答える