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();
}
}
AnnotationConfigApplicationContext的更多相关文章
- spring注解开发AnnotationConfigApplicationContext的使用
说明 使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文.避免使用application.xml进行配置.相比XML配置, ...
- 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)
非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...
- spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入
一.配置注解读取配置文件 (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值 实例: @PropertySource(val ...
- Spring源码解析 – AnnotationConfigApplicationContext容器创建过程
Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationC ...
- AnnotationConfigApplicationContext.的用法的核心代码
public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationCont ...
- new AnnotationConfigApplicationContext(MyBean.class)时,发生了什么?
当我们run一段代码,像下面这样两行.spring究竟做了什么些,让整个容器准备就绪,交付给用户直接可用的各种特性.为了弄清楚,默默梳理记录下来. public static void main (S ...
- spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象
一:spring的容器就是bean所居住的地点,这个居民点有很多的bean,有外来的bean(相当于创建了一个bean),有出去谋生的(相当于消亡了一个bean),他们之间都有某种联系 (bean与b ...
- spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 使用AnnotationConfig ...
- Spring5深度源码分析(三)之AnnotationConfigApplicationContext启动原理分析
代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian AnnotationCon ...
随机推荐
- 第2章 netty介绍与相关基础知识
NIO有一个零拷贝的特性.Java的内存有分为堆和栈,以及还有字符串常量池等等.如果有一些数据需要从IO里面读取并且放到堆里面,中间其实会经过一些缓冲区.我们要去读,它会分成两个步骤,第一块它会把我们 ...
- js面试题知识点全解(一原型和原型链)
1.如何准确判断一个变量是数组类型2.写一个原型链继承的例子3.描述new一个对象的过程4.zepto(或其他框架)源码中如何使用原型链知识点:1.构造函数2.构造函数-扩展3.原型规则和示例4.原型 ...
- 【转】nginx location匹配规则
转载请保留:http://www.nginx.cn/115.html location匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹配,不区分大小写^ ...
- tr td th是什么的缩写
tr是 table row 表格的行 td是table data th是table heading表格标题 ,一般表格第一行的数据都是table heading
- Java之IO流学习总结
流:可以理解为数据的流动,就是一个数据流,IO流最终要以对象来体现 流的分类: 按照流的方向:输入流和输出流 (输入流只能进行读操作,输出流只能进行写操作) 按照处理数据的不同:字节 ...
- 第二周个人作业:WordCount
github地址 https://github.com/lzwk/WordCount PSP表格 PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟) Planning 计划 20 40 · ...
- 关于LIst Set Map 异常的知识点---我的笔记
今天新的内容1.List接口2.Set接口3.Map集合4.异常==================================================================== ...
- java获取Excel的导入
先准备好这2个架包 import java.io.*; import org.apache.commons.io.FileUtils; import org.apache.poi.hssf.userm ...
- [译]Javascript中的do-while循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- c# get set 理解