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 ...
随机推荐
- android中finish和system.exit方法退出的区别
finish只是将此activity推向后台,并没有释放资源. 而system.exit则是杀死进程,会释放资源
- jetty分析
jetty处理过程: 1 new Server() (1)初试化线程池 生成固定大小线程数,新来的线程放入BlockingQueue. (2)初始化ServerConnector 初始化 sche ...
- CSS特性:white-space: nowrap;text-overflow: ellipsis;text-decoration: none
/*white-space: nowrap; text-overflow: ellipsis; text-decoration: none;*/ /*这三句话必须连着使用, * clip: 当对象内文 ...
- __tostring和__invoke 方法
首先放上代码: <?php class MagicTest{ //__tostring会在把对象转换为string的时候自动调用 public function __tostring() { r ...
- 主键primary key和唯一索引unique index
1)主键一定是唯一性索引,唯一性索引并不一定就是主键. 2)主键就是能够唯一标识表中某一行的属性或属性组,一个表只能有一个主键,但可以有多个候选索引. 3)主键常常与外键构成参照完整性约束,防止出现数 ...
- (转)嵌入式C开发人员的最好笔试题目
约定: 1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了 2)数据类型 char 一个字节 1 byte int 两个字节 2 byte ( ...
- linux环境启动django项目
BBS部署步骤 安装python3.6(如已安装无需重复) install python3.6 把BBS项目传上来 rz 选择文件 BBS.tar 解压文件 tar -xvf BBS.tar 安装my ...
- C++笔记--模板
一个string模板 简单的定义 template <class C>//模板形式,C是一个类型名字,不一定是某个类的名字 class String{ struct srep; srep ...
- java反射机制的进一步理解
承上一篇. JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...
- springcloud安全控制token的创建与解析
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorith ...