spring中Aware 接口 感知捕获

刺骨的言语ヽ痛彻心扉 2023-02-28 04:06 26阅读 0赞

Aware 接口 感知捕获

内容感知方式一:

  1. package cn.mesmile.desk.config;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.beans.factory.BeanFactoryAware;
  5. import org.springframework.beans.factory.BeanNameAware;
  6. import org.springframework.context.EnvironmentAware;
  7. import org.springframework.context.ResourceLoaderAware;
  8. import org.springframework.context.annotation.PropertySource;
  9. import org.springframework.core.env.Environment;
  10. import org.springframework.core.io.Resource;
  11. import org.springframework.core.io.ResourceLoader;
  12. import org.springframework.stereotype.Service;
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. /** * @author zb * @version 1.0 * @date 2020/7/18 17:50 * @description 内容感知 * * BeanNameAware 获取bean的名称 * BeanFactoryAware 获取BeanFactory * ResourceLoaderAware 获取资源加载器 * EnvironmentAware 获取环境信息 */
  17. @Service
  18. @PropertySource(value = "classpath:test.properties")
  19. public class AwareService implements BeanNameAware, BeanFactoryAware,
  20. ResourceLoaderAware, EnvironmentAware {
  21. private String beanName;
  22. private ResourceLoader resourceLoader;
  23. private Environment environment;
  24. /** * 获取数据 * @throws IOException */
  25. public void getValue() throws IOException {
  26. System.out.println("beanName: "+ beanName);
  27. Resource resource = resourceLoader.getResource("classpath:test.txt");
  28. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
  29. String s = bufferedReader.readLine();
  30. System.out.println("s: "+ s);
  31. bufferedReader.close();
  32. String property = environment.getProperty("my.address");
  33. System.out.println("property: "+ property);
  34. }
  35. /** * * @param beanFactory * @throws BeansException */
  36. @Override
  37. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  38. }
  39. @Override
  40. public void setBeanName(String s) {
  41. this.beanName = s;
  42. }
  43. @Override
  44. public void setEnvironment(Environment environment) {
  45. this.environment = environment;
  46. }
  47. @Override
  48. public void setResourceLoader(ResourceLoader resourceLoader) {
  49. this.resourceLoader = resourceLoader;
  50. }
  51. }

内容感知方式二:

  1. package cn.mesmile.desk.config;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. import org.springframework.context.annotation.PropertySource;
  6. import org.springframework.core.env.Environment;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.stereotype.Component;
  9. /** * @author zb * @version 1.0 * @date 2020/7/18 18:14 * @description */
  10. @PropertySource(value = "classpath:test.properties")
  11. @Component
  12. public class contextAware implements ApplicationContextAware {
  13. private ApplicationContext applicationContext;
  14. public void getValue(){
  15. // 判断 bean 是否存在
  16. boolean beanName = applicationContext.containsBean("beanName");
  17. Object beanName1 = applicationContext.getBean("beanName");
  18. AwareService bean = applicationContext.getBean(AwareService.class);
  19. AwareService awareService = applicationContext.getBean("awareService", AwareService.class);
  20. Environment environment = applicationContext.getEnvironment();
  21. String property = environment.getProperty("my.address");
  22. Resource resource = applicationContext.getResource("test.txt");
  23. String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
  24. }
  25. @Override
  26. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  27. this.applicationContext = applicationContext;
  28. }
  29. }

classpath下面的配置文件:

test.properties

my.address=测试

test.txt

这是一个测试文件

发表评论

表情:
评论列表 (有 0 条评论,26人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring-aware接口

    在springmvc中如果某个bean需要获取spring 的某些容器,可以通过实现其接口的方式,来获得,以此达到在Bean被初始后,能进行CRUD、数据源动态注入到全局应用等