Issue
I have the following CountryISOService class :
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!
}
}
Then I have the following Spring Boot application...
@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;
}
}
This is the debugger class :
// 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;
}
}
I can use countryIsoService in the RestServer - but I cannot use it in Debugger - as it is null - I know this from doing that null check and outputting the line to say it is null - if it wasn't it would output the other one.
I also do not use the spring beans xml now, because I switched to annotation, is this correct?
Where am I going wrong here?
Solution
If you are totally using Spring boot application. Then you can do it like
@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 "";
}
}
and Other classes will look like so
@Component
public class CountryISOService {
//anything you want
}
Bean injection can be done like
@Service
public class RestServer {
@Autowired
CountryISOService countryIsoService;
//
}
@Component
public class Debugger {
@Autowired
CountryISOService countryIsoService;
}
Answered By - Afridi