簡體   English   中英

NullPoinerException自動裝配的bean-Java,Spring

[英]NullPoinerException Autowired bean - Java, Spring

我不知道出什么問題了:/當我運行autowiredServiceTest()時,我得到了NullPointerException 我認為配置應該是正確的,在IDE中,我可以單擊依賴關系,當我手動拉出appContext ,bean可以工作,但是在下面的代碼中出現錯誤:

上下文:appCtx-watchdog-test-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:component-scan base-package="cz.aegis.watchdog" />
    <context:annotation-config />

    <bean id="helloWorldService" class="cz.aegis.watchdog.server.impl.HelloWorldServiceImpl" />

</beans>

接口:

package cz.aegis.watchdog.api.model;

public interface HelloWorldService {
    public boolean getTrue();
    public String getString();
}

Impl:

package cz.aegis.watchdog.server.impl;

import cz.aegis.watchdog.api.model.HelloWorldService;

public class HelloWorldServiceImpl implements HelloWorldService {
    public boolean getTrue() {
        return true;
    }

    public String getString(){
        return "helloWorld";
    }
}

測試班:

import cz.aegis.util.security.server.UserServiceHibernate;
import cz.aegis.watchdog.api.DtoTranslator;
import cz.aegis.watchdog.api.dto.MonitorTestDto;
import cz.aegis.watchdog.api.model.HelloWorldService;
import cz.aegis.watchdog.api.model.MonitorTest;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;

import java.sql.SQLException;

import org.testng.Assert;
import org.testng.annotations.Test;

@ContextConfiguration(locations = {"classpath:appCtx-watchdog-test-server.xml"})
public class Testing {

    @Autowired
    private HelloWorldService helloWorldService;

    @Test
    public void autowiredServiceTest() {
        System.out.println(helloWorldService.getString());
        Assert.assertTrue(helloWorldService.getTrue());
    }
}

錯誤:

java.lang.NullPointerException
at Testing.autowiredServiceTest(Testing.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1172)
at org.testng.TestRunner.privateRun(TestRunner.java:757)
at org.testng.TestRunner.run(TestRunner.java:608)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1158)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1083)
at org.testng.TestNG.run(TestNG.java:999)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

解決,僅添加:擴展AbstractTestNGSpringContextTests

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM