spring中Aware 接口 感知捕获
Aware 接口 感知捕获
内容感知方式一:
package cn.mesmile.desk.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/** * @author zb * @version 1.0 * @date 2020/7/18 17:50 * @description 内容感知 * * BeanNameAware 获取bean的名称 * BeanFactoryAware 获取BeanFactory * ResourceLoaderAware 获取资源加载器 * EnvironmentAware 获取环境信息 */
@Service
@PropertySource(value = "classpath:test.properties")
public class AwareService implements BeanNameAware, BeanFactoryAware,
ResourceLoaderAware, EnvironmentAware {
private String beanName;
private ResourceLoader resourceLoader;
private Environment environment;
/** * 获取数据 * @throws IOException */
public void getValue() throws IOException {
System.out.println("beanName: "+ beanName);
Resource resource = resourceLoader.getResource("classpath:test.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
String s = bufferedReader.readLine();
System.out.println("s: "+ s);
bufferedReader.close();
String property = environment.getProperty("my.address");
System.out.println("property: "+ property);
}
/** * * @param beanFactory * @throws BeansException */
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
}
@Override
public void setBeanName(String s) {
this.beanName = s;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
}
内容感知方式二:
package cn.mesmile.desk.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
/** * @author zb * @version 1.0 * @date 2020/7/18 18:14 * @description */
@PropertySource(value = "classpath:test.properties")
@Component
public class contextAware implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void getValue(){
// 判断 bean 是否存在
boolean beanName = applicationContext.containsBean("beanName");
Object beanName1 = applicationContext.getBean("beanName");
AwareService bean = applicationContext.getBean(AwareService.class);
AwareService awareService = applicationContext.getBean("awareService", AwareService.class);
Environment environment = applicationContext.getEnvironment();
String property = environment.getProperty("my.address");
Resource resource = applicationContext.getResource("test.txt");
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
classpath下面的配置文件:
test.properties
my.address=测试
test.txt
这是一个测试文件
还没有评论,来说两句吧...