有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java spring注释NullPointerException

@Component
public class AnnotationTest extends TestCase{
    ServiceTest serviceTest;
    @Autowired(required=false)
    public  void setServiceTest(ServiceTest serviceTest) {
        this.serviceTest = serviceTest;
    }
    public void testAnnotation () {
        new ClassPathXmlApplicationContext(new String[]{"/com/test/spring/ioc/beans.xml"});
        serviceTest.annotationTest("testName", "testPassword");
    }

错误:

Exception in thread "main" java.lang.NullPointerException
    at com.test.spring.ioc.AnnotationTest.invokeIoC(AnnotationTest.java:16)
    at com.test.spring.ioc.AnnotationTest.main(AnnotationTest.java:13)

服务类别:

@Service
public class ServiceTestImpl implements ServiceTest{
    @Autowired
    AnnotationDAO annotationDAO;
    public List<String> annotationTest(String name, String pssword) {       
        return annotationDAO.annotationTest(name, pssword);
    }
}

DAO类:

@Repository("AnnotationDAO")
public class AnnotationDAOImpl implements AnnotationDAO{

    public List<String> annotationTest(String name, String pssword) {
        List<String> list = new ArrayList<String>();
        list.add("Test 1");
        list.add("Test 2");
        return list;
    }
}

豆子。xml:

<bean id="service" class="com.test.spring.ioc.service.ServiceTestImpl"/>    
    <context:annotation-config />
        <context:component-scan base-package="com.test.spring" />

我怎样才能解决这个问题

编辑:

它发出警告信息:

    WARNING: Autowired annotation is not supported on static methods: public static void 
com.test.spring.ioc.AnnotationTest.setServiceTest(com.test.spring.ioc.service.ServiceTest)

我通过添加@Component(我不确定这是否是必需的),转到了junit测试,而不是上面所示的java main方法。 现在我得到了新的错误,如下所示:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'annotationTest': Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
com.test.spring.ioc.service.ServiceTest com.test.spring.ioc.AnnotationTest.serviceTest; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
unique bean of type [com.test.spring.ioc.service.ServiceTest] is defined: expected single 
matching bean but found 2: [service, serviceTestImpl]   at     org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProce   ssPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)

共 (2) 个答案

  1. # 1 楼答案

    问题是,您两次定义同一个bean:

    一旦明确:

    <bean id="service" class="com.test.spring.ioc.service.ServiceTestImpl"/>    
    

    和一次性组件扫描:

    <context:component-scan base-package="com.test.spring" />
    

    除掉其中一个,它就会起作用

    顺便说一句:测试时您不需要自己实例化容器,只需使用Spring TestContext Framework

    @RunWith(SpringJUnit4ClassRunner.class)
    // ApplicationContext will be loaded from "/applicationContext.xml"
    // and "/applicationContext-test.xml"
    // in the root of the classpath
    @ContextConfiguration({"/applicationContext.xml",
                           "/applicationContext-test.xml"})
    public class MyTest {
        // class body...
    }
    

    当然,还有JUnit4。x您不应再从TestCase扩展,而应使用带注释的方法:

    @Test
    public void testAnnotation () {
        serviceTest.annotationTest("testName", "testPassword");
    }
    
  2. # 2 楼答案

    AnnotationTest类不是由spring实例化的,因此spring从来没有机会注入serviceTest依赖项:

    您可以显式地获取bean:

    public static void main(String[] args) {
        ApplicationContext ctx = 
             new ClassPathXmlApplicationContext(
                 new String[]{"/com/test/spring/ioc/beans.xml"});
        serviceTest = ctx.getBean(ServiceTest.class);
        invokeIoC();
    }
    

    或者你可以告诉我用@Component注释你的AnnotationTest类,这样component-scan就会创建一个实例。请注意,尽管我认为这有点麻烦,因为您将实例化一个AnnotationTest对象,而从不使用它,所以我将取消main中的自动连接并使用getBean

    编辑:

    为了实现静态字段的自动连接,您还必须愚弄spring,使其认为serviceTest实际上不是静态的(同样,我认为这不是一个好主意):

    @Component
    public class AnnotationTest {
        static ServiceTest serviceTest;
    
        ...
    
        @Autowired
        public void setServiceTest(ServiceTest serviceTest) {
            AnnotationTest.serviceTest = serviceTest;
        }
    }