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 ...
随机推荐
- Niginx +Tomcat 集群搭建
1 安装niginx服务器,然后启动,访问localhost;出现欢迎界面证明niginx启动成功: 如上图的目录是nginx1.1的目录结构: 2 之后下载tomcat,然后赋值一份,总共两个tom ...
- Lucene打分公式的数学推导
原文出自:http://www.cnblogs.com/forfuture1978/archive/2010/03/07/1680007.html 在进行Lucene的搜索过程解析之前,有必要单独的一 ...
- cocos2dx帧动画
//帧动画的创建 //方式一,通过多张图片来创建 auto sprite1 = Sprite::create("grossini_dance_05.png"); sprite1-& ...
- 用fontcreator创建了一个半成品的字体
下效果,哈哈. 为啥说半成品呢?因为只制作了0到9这几个字符,其他的字母.汉字.符号啥的都没有制作,唯一感觉就是字体设计是一个非常有设计感的活儿,而且需要付出很多的精力,尤其是汉字字体,常见的有6k多 ...
- 一道java笔试题目:Vector和ArrayList的区别
Vector和ArrayList的区别 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构这些类均在java.util包中本文试图通过 ...
- R: 绘图 pie & hist
问题: 绘制 pie .hist 图 解决方案: 饼图函数 pie( ) pie(x, labels = names(x), edges = 200, radius = 0.8, clockwise ...
- SDUT 3375 数据结构实验之查找三:树的种类统计
数据结构实验之查找三:树的种类统计 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 随着卫星成 ...
- Netty的Channel
Channel是一个网络端口连接,或者是可以进行读,写,链接,绑定端口的组件的连接. Channel就是一个链接,它提供了如下的功能. 1:获取当前链接的状态 2:配置当前链接参数 3:进行read ...
- 适配器设计模式及GenericServlet(九)
一共两个知识点:1.Servlet 里面已经有适配器了,名字叫:GenericServlet. 2.什么是适配器模式. 如果这个接口里面有好多方法,那创建A/B/C这个三个类的时候如果必须继 ...
- android报错:org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapObject
今天在写一个webservice时一直报错,报Caused by: java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast ...