Spring bean,让它们可用于其他类?我的组件是空的?

时间:2017-05-12 05:57:45

标签: java spring spring-bean

我有以下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 - 但是我不能在调试器中使用它 - 因为它是null - 我知道这是通过执行null检查并输出行来说它是null - 如果它不是会输出另一个。

我现在也不使用spring beans xml,因为我切换到了注释,这是正确的吗?

我在哪里错了?

1 个答案:

答案 0 :(得分:0)

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