Null Pointer Exception while Autowiring LinkedBlockingQueue

六月ゝ 毕业季﹏ 提交于 2019-12-12 17:56:04

问题


I am getting null pointer exception while trying to add anything in my solrQueue. I checked in debugger and it is because solrQueue is null. But I have autowired it in my application context then why this error?

public class Check {
    @Autowired
    public LinkedBlockingQueue<SolrInputDocument> solrQueue;
    public SolrInputDocument solrDoc;   
    public void solradd(){
        solrDoc=new SolrInputDocument();
        solrDoc.addField("title", "abc");
        solrQueue.add(solrDoc);//solrQueue is null 
    }
}

application Context.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />
    <!--<context:component-scan base-package="com/abc" />   -->

    <bean id="solrQueue" class="java.util.concurrent.LinkedBlockingQueue" />
    <bean id="check" class="com.abc.Check" scope="prototype" />

</beans>

回答1:


You are creating an instance of Check class manually rather than asking Spring to create/return one for you:

Check c=new Check();
c.solradd();

This will never* work since Spring has no knowledge about you created Check class. Depending on how do you start you Spring context, you must either explicitly ask the application context:

Check check = applicationContext.getBean(Check.class)

or inject the check bean into some other compoent like controller:

@Autowired
private Check check;

See also:

  • Spring Dependency Injection Autowiring Null

* AspectJ weaving will do the trick, but this is like using a cannon to kill a fly




回答2:


The reason lies here:

<context:annotation-config />
<!--<context:component-scan base-package="com/abc" />   -->

By including annotation-config you are allowing Spring to be using such extensions as @Autowired annotation. This, however, doesn't mean that Spring will know how to do that by itself.

For @Autowired to work, you need to have a matching bean defined in your application context. You can do that either manually (by placing <bean> declarations in XML) or automatically (by using component-scan).

Solution

try to uncomment <context:component-scan /> and set proper base-package, matching the package of components you want to wire up automatically.

Note

if the components you want to wire are in third party library, it's usually more convenient to use an explicit <bean class="com.somecompany.SomeComponent" /> definition within XML.




回答3:


I don't think that you can mix annotation and XML based config. 2 solutions here :

  1. Remove the declaration of the Check bean in your XML file and add a @Component annotation on your Check class.

  2. Inject the solrQueue property to your bean in the XML :

    <bean id="check" class="com.abc.Check" scope="prototype"> <property name="solrQueue" ref="solrQueue"/> </bean>



来源:https://stackoverflow.com/questions/9053977/null-pointer-exception-while-autowiring-linkedblockingqueue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!