zoukankan      html  css  js  c++  java
  • AnnotationConfigApplicationContext

    package com.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.Properties;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.core.env.PropertiesPropertySource;
    import org.springframework.core.env.PropertySource;
    
    public class ReportRunner {
    
      public static void main(String... args) {
        try {
          new ReportRunner().parseAndExecute();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
    
      private void parseAndExecute() throws ClassNotFoundException, IOException {
        prepareAndExecute("uat.properties","com.test.TestReportContext");
      }
    
    
    
    
      private void prepareAndExecute(String propName, String contextClassName)
          throws IOException, ClassNotFoundException {
        //log.info("Preparing execute report runner, properties name: {}, contextClassName: {}", propName,
            //contextClassName);
        URL url = getClass().getClassLoader().getResource(propName);
        if (url == null) {
          //log.error("Properties name is wrong, cannot find resource with name: {}", propName);
          return;
        }
        // log the properties which used in the jar
        Properties properties = getProperties(url);
        Class contextClass = Class.forName(contextClassName);
        execute(properties, contextClass);
      }
    
    
      private void execute(Properties props, Class contextClass) {
        //log.info("Starting execute report runner");
        PropertySource<?> ps = new PropertiesPropertySource("main", props);
        buildContextAndRun(ps, contextClass);
        //log.info("Stopping report runner");
      }
    
    
      private Properties getProperties(URL url) throws IOException {
        try (InputStream is = url.openStream()) {
          Properties properties = new Properties();
          properties.load(is);
          //log.info("All defined properties: {}", properties);
          return properties;
        }
      }
    
    
      /**
       * First build {@link AnnotationConfigApplicationContext} with contextClass, then build and send
       * report.
       */
      private void buildContextAndRun(PropertySource ps, Class contextClass) {
        try (AnnotationConfigApplicationContext reportContext =
            new AnnotationConfigApplicationContext()) {
          reportContext.getEnvironment().getPropertySources().addLast(ps);
    
          reportContext.register(contextClass);
          reportContext.refresh();
    
          TestTopology testTopology = reportContext.getBean(TestTopology.class);
          System.out.println(testTopology);
        }
      }
    }
    package com.test;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    
    @Configuration
    @Import({
      Person.class,
      Student.class,
      TestTopology.class
    })
    public class TestReportContext {
      
     /* @Bean
      public Person testPerson() {
        return new Person();
      }
      
      @Bean
      public Student testStudent() {
        return new Student();
      }
      
      @Bean
      public TestTopology testTopology() {
        return new TestTopology();
      }*/
    }
    package com.test;
    
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    
    public class TestTopology {
    
      //@Resource
      @Autowired
      private Person testPerson;
      //@Resource
      @Autowired
      private Student testStudent;
    }
    package com.test;
    
    public class Student {
      public void say() {
        System.out.println("hello");
      }
    }
    package com.test;
    
    import javax.annotation.Resource;
    
    public class Person {
      @Resource
      private Student student;
    
      public void say() {
        this.student.say();
      }
    }
  • 相关阅读:
    京东咚咚架构演讲读后感
    京东峰值系统设计读后感
    游戏服务器的架构演讲读后感
    菜鸟弹性调度系统的架构设计读后感
    阿里如何实现秒级百万TPS?搜索离线大数据平台架构解读读后感
    阿里游戏高可用架构设计实践读后感
    淘宝架构背后——零售业务中台架构设计探讨及实践读后感
    本地存储的时候需要类型转换
    禁止输入框显示用户历史输入历史记录
    项目必备!永无 bug 注释
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/9742356.html
Copyright © 2011-2022 走看看