![](/img/trans.png)
[英]MySQL DataSource in Spring Boot is not available in other Service/Control classes and throwing null pointer exception
[英]Spring beans, making them available to other classes? My components are null?
我有以下CountryISOService類:
package restServer.services;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import restServer.dto.commons.CountryWithISOCodes;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@Component
public class CountryISOService {
// Declaring map.
HashMap<String, String> countryNameMap = new HashMap<>();
public void setInitialValues(String jsonToMap) throws IOException {
// Code ommitted because it doesn't actually help with my question, and I don't want to make you read forever!
}
public String getAlpha2FromName(String countryName)
{
// Code ommitted because it doesn't actually help with my question, and I don't want to make you read forever!
}
}
然后我有以下Spring Boot應用程序...
@SpringBootApplication
@ComponentScan(basePackages = {"restServer"})
@EnableMongoRepositories("restServer.repos")
@PropertySource(value="classpath:config/testing.properties")
public class RestServer {
@Autowired
private ResourceLoader resourceLoader;
@Autowired
CountryISOService countryIsoService;
public static void main(String[] args)
{
SpringApplication.run(RestServer.class, args);
}
@PostConstruct
public void init() throws IOException, ParseException, URISyntaxException {
// Just populating the countryISOService with some JSON - that class actually maps it to individual JSON
// objects - but I ommitted the implementation of that in this question because it isn't important.
JSONParser parser = new JSONParser();
InputStream stream = resourceLoader.getResource("classpath:/json/countries.json").getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String line;
while ((line = streamReader.readLine()) != null) {
responseStrBuilder.append(line);
}
Debugger debugger = new Debugger();
// Try to use the instance of the CountryISOService class that resided in "debugger" - instead of the one in here, as a test.
debugger.seeIfSomethingExists("Afghanistan");
}
// Wasn't sure if the getters and setters are needed when using Annotation - kept them anyway, just in case.
public void setCountryIsoService(CountryISOService countryIsoService) {
this.countryIsoService = countryIsoService;
}
public CountryISOService getCountryIsoService() {
return countryIsoService;
}
}
這是調試器類:
// I have tried adding @Component here, it didn't help - but I don't think I want this class to be the bean anyway
// I just want it to access CountryISOService
public class Debugger {
@Autowired
CountryISOService countryIsoService;
public void seeIfSomethingExists(String country){
if(this.countryIsoService == null)
{
System.out.println("the service was null");
}
else{
System.out.println("the service was autowired correctly.");
}
}
public void setCountryIsoService(CountryISOService countryIsoService) {
this.countryIsoService = countryIsoService;
}
public CountryISOService getCountryIsoService() {
return countryIsoService;
}
}
我可以在RestServer中使用countryIsoService-但不能在Debugger中使用它-因為它為null-我通過執行null檢查並輸出行以說它為null來知道這一點-如果不是,它將輸出另一個。
我現在也不使用spring bean xml,因為我切換到了注解,這正確嗎?
我在哪里錯了?
如果您完全使用Spring引導應用程序。 然后你可以像
@SpringBootApplication
public class TestingApplication {
@Autowired
RestServer server;
public static void main(String[] args) {
SpringApplication.run(TestingApplication.class, args);
}
@Bean
public String value(@Autowired RestServer server){
return "";
}
}
和其他類看起來像這樣
@Component
public class CountryISOService {
//anything you want
}
豆注入可以像
@Service
public class RestServer {
@Autowired
CountryISOService countryIsoService;
//
}
@Component
public class Debugger {
@Autowired
CountryISOService countryIsoService;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.