Spring 个 beans,让其他 类 可以使用它们?我的组件是空的?
Spring beans, making them available to other classes? My components are null?
我有以下 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!
}
}
然后我有以下 Spring 启动应用程序...
@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;
}
}
这是调试器 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;
}
}
我可以在 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
}
Bean注入可以像
一样完成
@Service
public class RestServer {
@Autowired
CountryISOService countryIsoService;
//
}
@Component
public class Debugger {
@Autowired
CountryISOService countryIsoService;
}
我有以下 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!
}
}
然后我有以下 Spring 启动应用程序...
@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;
}
}
这是调试器 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;
}
}
我可以在 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
}
Bean注入可以像
一样完成 @Service
public class RestServer {
@Autowired
CountryISOService countryIsoService;
//
}
@Component
public class Debugger {
@Autowired
CountryISOService countryIsoService;
}