package com.wisely.aware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class DemoBean implements BeanNameAware,ResourceLoaderAware{
private String name;
private ResourceLoader loader;
//BeanNameAware接口的方法
public void setBeanName(String beanName) {
this.name = beanName;
}
//ResourceLoaderAware接口的的方法
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader;
}
public String getName() {
return name;
}
public ResourceLoader getLoader() {
return loader;
}
}
jhkljhlkjhlkj
111111111111
package com.wisely.aware;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class Main {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.aware");
DemoBean db = context.getBean(DemoBean.class);
System.out.println(db.getName());
ResourceLoader rl = db.getLoader();
Resource r = rl.getResource("classpath:com/wisely/aware/info.txt");
System.out.println(IOUtils.toString(r.getInputStream()));
context.close();
}
}
输出结果
demoBean
jhkljhlkjhlkj
111111111111原文:http://wiselyman.iteye.com/blog/2212362