package com.javahonk;

import java.io.InputStream;
import java.util.Scanner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

import com.javahonk.di.bean.SpringResourceInjection;

public class ReadSpringResourceFile {
    public static void main(String[] args) throws Exception {
	ApplicationContext applicationContext = 
		new ClassPathXmlApplicationContext(
		"spring/springResourceContext.xml");
	InputStream inputStream = null;
	Scanner scanner = null;
	try {

	    SpringResourceInjection springResourceInjection = applicationContext
		    .getBean("resourceInjection",
			    SpringResourceInjection.class);

	    Resource dynamicResourceViaResourceLoader = springResourceInjection
		    .loadDynamicResource();
	    inputStream = dynamicResourceViaResourceLoader
		    .getInputStream();
	    scanner = new Scanner(inputStream);
	    while (scanner.hasNext()) {
		System.out.println(scanner.nextLine());
	    }

	} finally {
	    if (inputStream != null) {
		inputStream.close();
	    }
	    if (null != scanner) {
		scanner.close();
	    }
	}
    }

}
