javaspring-bootpostconstruct

Autowired , Value annotation is not working


I am using below class and my @Autowired, @Value annotation is not working for spring boot application. Also, I use init method with @PostConstruct and initialize the value when the application starts. Then right before use of the variables, the value gets intialized to 0 or null.

Here is my code:

@Component
public class OsmXmlClient {
    
    private static final Logger log = 
    LoggerFactory.getLogger(OsmXmlClient.class);

    /*@Value("${osm.xml.service.url}") */
    private String osmXmlServiceUrl = "http://myurl/XMLAPI";
    /*#@Value("${osm.xml.service.userId}") */
    private String osmXmlServiceUserId = "abc";
    /*@Value("${osm.xml.service.password}")*/
    private String osmXmlServicePassword = "abc123";
    
    private Map<Integer, String> urlMap = new HashMap<>();
    private Random random = new Random();
    private int seviceUrlsCount = 0;
    
    
    @PostConstruct
    public void init() {
        List<String> servieUrlList = Arrays.asList(osmXmlServiceUrl.split(";"));
        log.info("servieUrlList = " + servieUrlList);
        seviceUrlsCount = servieUrlList.size();
        log.info("seviceUrlsCount in init = " + seviceUrlsCount);
        
        for(int i=0;i<seviceUrlsCount;i++) {
            log.info("servieUrlList.get(i) = " + servieUrlList.get(i));
            urlMap.put(i, servieUrlList.get(i));
        }
    } 
    
    private String getServiceUrl() {
        log.info("serviceUrlsCount  at getServiceUrl = " + seviceUrlsCount);
        return urlMap.get(random.nextInt(seviceUrlsCount));
    }
}

Here is the log:

[2020-07-31 17:07:30.373] [INFO] [Context:TomcatWebServer] [] [Tomcat initialized with port(s): 9014 (http)]
[2020-07-31 17:07:30.903] [DEBUG] [Context:OsmModuleApplication] [] [osmProperties = OsmProperties()]
[2020-07-31 17:07:30.920] [INFO] [Context:OsmXmlClient] [] [servieUrlList = [http://myurl/XMLAPI]]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [seviceUrlsCount in init = 1]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [servieUrlList.get(i) = http://myurl/XMLAPI]
[2020-07-31 17:07:31.041] [INFO] [Context:OrderController] [] [OrderControllerInit called]

Then I got this log:

[2020-07-31 17:07:45.403] [INFO] [Context:OrderDetailsService] [] [Entering 
retryOrderService with billerorderId = 22428040]
[2020-07-31 17:07:45.403] [INFO] [Context:OsmXmlClient] [] [serviceUrlsCount  at getServiceUrl = 0]
[2020-

I had experienced the @Autowired and @Value annotation issue before . But I had a workaround. I would like to know why this is hapenning and what the solution to it is.

Here is my service class that call OsmXmlClient class:

@Service
public class OrderDetailsService {

    private static final Logger log = 
    LoggerFactory.getLogger(OrderDetailsService.class);

    OSMClient osmClient = new OSMClient();
    OsmXmlClient osmXmlClient = new OsmXmlClient();
    OSMProvAdService provAdService = new OSMProvAdService();
    CommonService commonService = new CommonService();

    public RetryOrderResponseType retryOrderService(String orderId) {
        log.info("Entering retryOrderService with billerorderId = " + orderId);
    
        String billerOrderId = "";
    
        String orderProcessHistory = osmXmlClient.queryOrder(orderId);
        log.info("orderProcessHistory = " + orderProcessHistory);
    
        RetryOrderResponseType retryOrderResponse = null;
        List<GetOrderResponseType> orderResponseTypes = new ArrayList<>();
        FindOrderResponseType findOrderResponseType = null;
    
        return retryOrderResponse;
    }
}

As you can see I have to instatiate using new keyword instead of Autowiring.


Solution

  • @Value properties are not initialized by Spring unless the component is initialized by Spring. Do not instantiate classes with new yourself if you want dependency injection, property injection et cetera to work. Instead, use @Autowired to supply instances of your Component.