Java 不能调用";com.example.gestion.service.TestService.saveTest(testreport)";因为";此.testRepository";是空的
嗨,我正在尝试将多个线程与我的spring boot应用程序集成。我在我的服务中创建了一个类MyRunnable 这是我的班级:Java 不能调用";com.example.gestion.service.TestService.saveTest(testreport)";因为";此.testRepository";是空的,java,multithreading,spring-boot,Java,Multithreading,Spring Boot,嗨,我正在尝试将多个线程与我的spring boot应用程序集成。我在我的服务中创建了一个类MyRunnable 这是我的班级: import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.sql.Time; import javax.annotation.PostConstruct; import org.openqa.selenium.By; import
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Time;
import javax.annotation.PostConstruct;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.gestion.entity.Archive;
import com.example.gestion.entity.Lien;
import com.example.gestion.entity.TestRapport;
import com.example.gestion.repository.ApplicationsRepository;
import com.example.gestion.repository.ArchiveRepository;
public class MyRunnable implements Runnable {
private final Lien lien;
public static String message="";
@Autowired
TestService testRepository;
MyRunnable(Lien url) {
this.lien = url;
}
public static int hhttpcode(String site) throws IOException {
try {
URL url = new URL(site);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
return code;
}catch (Exception e)
{return 0;}}
public static void setMessage(String message) {
MyRunnable.message = message;
}
@Override
@PostConstruct
public void run() {
// TODO Auto-generated method stub
String msg="";
String type=lien.getTestType();
long millis=System.currentTimeMillis();
java.sql.Date date=new java.sql.Date(millis);
Time sqlTime = new Time(millis);
/////boucle if that gives me the String msg
/// enregistrer le test
TestRapport rapp=new TestRapport(lien,msg,date,sqlTime);
testRepository.saveTest(rapp);
}
}
当我运行它时,boucle if工作正常,但当涉及到保存实体时,它会给我以下错误:
线程“pool-1-thread-3”java.lang.NullPointerException中的异常:
无法调用
“com.example.gestion.service.TestService.saveTest(com.example.gestion.entity.testreport)”
因为“this.testRepository”在
com.example.gestion.service.MyRunnable.run(MyRunnable.java:298)位于
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
在
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
位于java.base/java.lang.Thread.run(Thread.java:832)
我试图将@PostConstruct添加到run()函数中,但没有成功
有人能帮我解决这个问题吗?PostConstruct和Autowired只能在托管bean中使用
要使MyRunnable成为托管bean,您可以使用@Component对其进行注释,我添加了它,还添加了@Service,但即使是应用程序也没有启动并提供给我:考虑在您的配置中定义一个类型为“com.example.gestion.entity.Lien”的bean。由于MyRunnable构造函数需要要注射的大豆。用
@Component
或@Service
注释Lien。它给出的错误与我在问题中咀嚼的错误相同。MyRunnable类中的TestService字段是否仍然用@Autowired注释?是的,我没有更改它。