Spring – 17 – Resource

Merhaba Arkadaslar
Bu bolumde org.springframework.core.io.Resource ve
ResourceLoaderAware.ResourceLoaderAware arabirimini inceleyecegiz.
Spring’te, kaynaklari/resource(txt, properties vs ) yuklemek/load icin Resource getResource() metodunu kullanabiliriz.

getResource() metodunu ApplicationContext uzerinden kullanabiliriz.

ResourceFileTest.java

package _22.access.resource.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

public class ResourceFileTest {

	public static void main(String[] args) throws IOException {

		// configuration file is not necessary. we didnt define bean.
		ApplicationContext ctx = new ClassPathXmlApplicationContext();

		Resource resource = ctx.getResource("file:/home/levent/Desktop/source.txt");

		try {
			InputStream is = resource.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			String line;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			br.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		((ClassPathXmlApplicationContext) ctx).close();

	}
}


“file:” ile basladigimizda bir file system path bilgisi verebiliriz. Bilgisayarimda yer alan source.txt dosyasinin absolute path bilgisini arguman olarak metoda verdim.
Siz de eger windows tabanli bir bilgisayar kullaniyorsaniz c:\… seklinde bir adres verebilirsiniz.

Ornegimizi calistirdigimizda dosya icerigini okuyacak.

source.txt file content...

classpath’te yer alan kaynaklara ulasim saglayabiliriz. Bunun icin acik sekilde “classpath:” literali ile baslayabiliriz. Bu ifade opsiyoneldir.

ResourceClasspathTest.java

package _22.access.resource.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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

public class ResourceClasspathTest {

	public static void main(String[] args) throws IOException {

		// configuration file is not necessary. we didnt define bean.
		ApplicationContext ctx = new ClassPathXmlApplicationContext();

		Resource resource = ctx.getResource("classpath:22.test.properties");
		InputStream is = resource.getInputStream();

		Properties prop = new Properties();
		prop.load(is);

		String name = prop.getProperty("name");

		System.out.println(name);

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

Ornegimizi calistirdigimizda ;

Levent

ResourceLoaderAware

ResourceLoaderAware arabirimini uygulayalim. MessageSourceAware arabirimine benzer sekilde kullanacagiz.

ResourceLoaderService.java

package _23.resourceLoaderAware.service;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

public class ResourceLoaderService implements ResourceLoaderAware {
	private ResourceLoader resourceLoader;

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

	public ResourceLoader getResourceLoader() {
		return resourceLoader;
	}

}

ResourceLoaderAwareTest.java

package _23.resourceLoaderAware.test;

import java.io.IOException;

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

import _23.resourceLoaderAware.service.ResourceLoaderService;

public class ResourceLoaderAwareTest {

	public static void main(String[] args) throws IOException {

		// configuration file is not necessary. we didnt define bean.
		ApplicationContext ctx = new ClassPathXmlApplicationContext();

		ResourceLoaderService resourceLoaderService = ctx.getBean("resourceLoaderService", ResourceLoaderService.class);
		ResourceLoader resourceLoader = resourceLoaderService.getResourceLoader();
		Resource resource = resourceLoader.getResource("classpath:23.test.txt");

		System.out.println(resource.getURL());

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

23.resourceLoaderAware.xml

	<bean id="resourceLoaderService" class="_23.resourceLoaderAware.service.ResourceLoaderService" />

Ornegimizi calistirdigimizda ;

file:/home/levent/java/workscape/Injavawetrust.spring/target/classes/23.test.txt

Github kaynak dosyalar/ source folder
leventerguder/injavawetrust-spring-tutorial

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *