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();
      }
    }
  • 相关阅读:
    虚拟机下修改ip配置
    php cli 下 php.ini 配置
    centos 默认php 版本太低移到高版本的办法
    liux 防火墙以及开关
    [POI2006]OKR-Periods of Words(KMP)
    KMP
    [NOI1999]生日蛋糕(搜索)
    [HAOI2008]糖果传递
    [HEOI2015]兔子与樱花(贪心)
    [POJ3694]Network(Tarjan,LCA)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/9742356.html
Copyright © 2011-2022 走看看