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的更多相关文章

  1. spring注解开发AnnotationConfigApplicationContext的使用

    说明 使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文.避免使用application.xml进行配置.相比XML配置, ...

  2. 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)

    非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...

  3. spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入

    一.配置注解读取配置文件         (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值   实例:           @PropertySource(val ...

  4. Spring源码解析 – AnnotationConfigApplicationContext容器创建过程

    Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationC ...

  5. AnnotationConfigApplicationContext.的用法的核心代码

    public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationCont ...

  6. new AnnotationConfigApplicationContext(MyBean.class)时,发生了什么?

    当我们run一段代码,像下面这样两行.spring究竟做了什么些,让整个容器准备就绪,交付给用户直接可用的各种特性.为了弄清楚,默默梳理记录下来. public static void main (S ...

  7. spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象

    一:spring的容器就是bean所居住的地点,这个居民点有很多的bean,有外来的bean(相当于创建了一个bean),有出去谋生的(相当于消亡了一个bean),他们之间都有某种联系 (bean与b ...

  8. spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)

    import org.springframework.context.annotation.AnnotationConfigApplicationContext; 使用AnnotationConfig ...

  9. Spring5深度源码分析(三)之AnnotationConfigApplicationContext启动原理分析

    代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian AnnotationCon ...

随机推荐

  1. saltstact的安装与配置

    Saltstack是一个服务器基础架构集中化管理平台,具备配置管理.远程执行.监控等功能,人们一般习惯把saltstack比作成简化版的puppet和加强版的func.saltstack基于Pytho ...

  2. CURD 操作 [2]

    一.数据读取 在之前的课程中,我们已经大量使用了数据读取的功能,比如 select()方法.结合各种连贯方法可以实现数据读取的不同要求,支持连贯的方法有: 1.where,查询或更新条件:2.tabl ...

  3. 前端学习笔记2017.6.12 HTML的结构以及xhtml、html、xml的区别

    HTML的结构 一个HTML文档可分为几个部分,如下图所示: DOCTYPE部分.head部分和body部分 DOCTYPE部分,这个很重要,可以理解为不同的DOCTYPE意味着不同的html标准,因 ...

  4. Java面试问题列表

  5. Umbraco 中获取一个media item的文件路径 file path

    我们要使用UmbracoHelper, 这里就需要用到我们在之前的blog里面写的UmbracoContext 参看这个blog     https://www.cnblogs.com/wphl-27 ...

  6. 【C#】EF学习<一> CodeFist

    [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) 目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) http://www.cnblogs.com/ ...

  7. [Spring Boot]Request method 'GET' not supported

    在网上查了些资料没有解决,检查代码发现 @PostMapping public String add(Employee employee){ System.out.println(employee); ...

  8. Schema技术

    Schema 技术 Schema 是 DTD 的代替者,名称为 XML Schema,用于描述XML 文档结构,即对XML文档做出规范,比 DTD 更加强大,最主要的特征之一就是XML Schema ...

  9. C#中工厂模式的作用

    1.比如,主要用于对扩展性有要求的功能. 以简单工厂为例: 接口Fun有三个实现 class FunA FunB FunC工厂 class Fac {   public static Fun getF ...

  10. [Oracle入门级]知识概况

    oracle各个版本间的主要技术更新 oracle 增加数据库创建和存储对象 oracle 8i 整体性能提升 oracle9i 实施应用集群 oracle 10g 支持网格计算 oracle 11g ...