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. mssql error 26

    右击数据库选择“方面”,将“RemoteAccessEnabled”属性设为“True”,点“确定”

  2. 根据/proc/meminfo对空闲内存进行占用

    #include <stdio.h> #include <sys/sysinfo.h> #include <linux/kernel.h> /* 包含sysinfo ...

  3. Ros学习——C++发布器publisher和订阅器subscriber

    1.编写发布器 初始化 ROS 系统 在 ROS 网络内广播我们将要在 chatter 话题上发布 std_msgs/String 类型的消息 以每秒 10 次的频率在 chatter 上发布消息 在 ...

  4. session和cookie个字消除的方法

    session消除的方法就是: session_destroy(); cookie消除的方法就是setcookie()函数的时间设为当前时间即可 if(isset($_COOKIE['adminId' ...

  5. Go语言-变量和常量

    我们在这里需要优先说明的是用于声明变量的关键字var,以及用于声明常量的关键字const.要知道,绝大多数的数据类型的值都可以被赋给一个变量,包括函数.而常量则不同,它只能被赋予基本数据类型的值本身. ...

  6. 开发同事 Linux 实用基本操作

    Linux 有复杂的体系,有很多的命令,开发同事日常开发时,不像运维同事需要熟练使用很多命令. 下面记录下我在工作中,常用的基本命令: 一 日志查看 对于开发同事来说,日常工作中,Linux 中最常用 ...

  7. p2657 windy数

    传送门 分析 首先这是一个询问一段区间内的个数的问题,所以我们可以用差分的思想用sum(R)-sum(L-1).然后我们考虑如何求出sum(n),我们用dp[i][j][k][t]表示考虑到第i位,最 ...

  8. poj3557 Map Generator

    传送门 题目大意 给定两个数n,p;表示n个点中任意两点连边的概率为p,求生成的图是个连通块的概率 分析 我们发现直接求产生联通块的概率并不容易,于是我们转而考虑计算不能生成联通块的概率,公式如下: ...

  9. linux环境安装python

    linux环境下安装python3,一步一步来吧! 安装python3 安装readline-devel依赖 ,用于解决python3安装完成后,退格和方向键乱码问题 yum install read ...

  10. Java接口基础

    接口(interface) 1.接口体中包含常量的声明(没有变量)和抽象方法两部分.接口体中只有抽象方法,没有普通的方法,而且接口体中所有的常量访问权限一定是public,而且是static常量(允许 ...