码迷,mamicode.com
首页 > 移动开发 > 详细

AnnotationConfigApplicationContext

时间:2018-10-04 16:38:28      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:new t   conf   start   wired   cto   .net   work   try   factor   

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();
  }
}

 

AnnotationConfigApplicationContext

标签:new t   conf   start   wired   cto   .net   work   try   factor   

原文地址:https://www.cnblogs.com/tonggc1668/p/9742356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!