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();
      }
    }
  • 相关阅读:
    WPF TextBox 一些设置技巧
    Rust 初始配置
    Framework​Element.​Find​Name 根据名字查找控件
    C# SQLite 数据库操作
    MP3 信息读取
    C# event 事件学习
    Nginx 整合 Lua 实现动态生成缩略图
    Spring Cloud 入门 之 Config 篇(六)
    Spring Cloud 入门 之 Zuul 篇(五)
    Flyway 简单入门教程
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/9742356.html
Copyright © 2011-2022 走看看