有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringbeans,使它们可用于其他类?我的组件为空?

我有以下服务类别:

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;
    }
}

这是调试器类:

// 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中使用它,因为它是空的,我通过执行空检查并输出一行表示它为空知道这一点,如果不是空的,它将输出另一行

我现在也不使用SpringbeansXML,因为我切换到了annotation,这对吗

我哪里出了问题


共 (1) 个答案

  1. # 1 楼答案

    如果您完全使用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;
    }