Spring AnnotationConfigApplicationContext

概述

  • 通常Spring XML文件用来作为ClassPathXmlApplicationContext 的初始化。@Configuration 标注的类能够用来初始化AnnotationConfigApplicationContext类,这样就可以提供Spring容器的完全的自由的无XML用法
  • AnnotationConfigApplicationContext继承GenericApplicationContext这个通用应用上下文,GenericApplicationContext内部定义了一个DefaultListableBeanFactory实例,GenericApplicationContext实现了BeanDefinitionRegistry接口,所以可以通过AnnotationConfigApplicationContext实例注册bean defintion,然后调用refresh()方法来初始化上下文。
  • AnnotationConfigApplicationContext继承AbstractApplicationContext,AbstractApplicationContext提供了ApplicationContext的抽象实现。

示例

package com.myapp.config;
import com.myapp.Entitlement;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name="entitlement")
public Entitlement entitlement() {
Entitlement ent= new Entitlement();
ent.setName("Entitlement");
ent.setTime(1);
return ent;
} @Bean(name="entitlement2")
public Entitlement entitlement2() {
Entitlement ent= new Entitlement();
ent.setName("Entitlement2");
ent.setTime(2);
return ent;
}
} package com.myapp;
public class Entitlement {
private String name;
private int time; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
} package com.myapp; import com.myapp.config.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class JavaConfigTest {
public static void main(String[] arg) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh(); Entitlement ent = (Entitlement)ctx.getBean("entitlement");
System.out.println(ent.getName());
System.out.println(ent.getTime()); Entitlement ent2 = (Entitlement)ctx.getBean("entitlement2");
System.out.println(ent2.getName());
System.out.println(ent2.getTime()); ctx.close();
}
}

Spring 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. spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象

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

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

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

  7. 阶段3 2.Spring_06.Spring的新注解_3 AnnotationConfigApplicationContext的使用

    目前这个配置文件除了导约束就没有其他的内容了. 删除这个bean.xml文件 但是测试类里面还是读取的xml的信息 注解 查看ApplicationContext的 关系图 查看实现类的实现类 之前我 ...

  8. spring framework源码之AnnotationConfigApplicationContext

    AnnotationConfigApplicationContext 内部使用了AnnotatedBeanDefinitionReader:ClassPathBeanDefinitionScanner ...

  9. 【译】Spring 4 基于TaskScheduler实现定时任务(注解)

    前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...

随机推荐

  1. STM32的RTC晶振不起振的原因及解决方法

    STM32的RTC晶振经常出现不起振的问题,这已经是“业界共识”了.很多人在各种电子论坛上求助类似于“求高手指点!RTC晶振不起振怎么办”的问题,而其答案基本可以概括为“这次高手帮不了你了” 更有阴谋 ...

  2. CodeForces 1182D

    图论的思维题,太秀了,网上答案也不多,我就也来bb吧 总之47个样例姑且是过了,不知道还有没有反例: 会求树的重心和中心了,挺好 #include<cstdio> #include< ...

  3. Java 连接 SQL Server 数据库

    //连接数据库 public Connection getConnection(){ //url为绝对路径 String url="jdbc:sqlserver://127.0.0.1:14 ...

  4. $CF24D\ Broken Robot\ DP+$高斯消元

    Luogu Description 你收到的礼物是一个非常聪明的机器人,行走在一块长方形的木板上.不幸的是,你知道它是坏的,表现得相当奇怪(随机).该板由n行和m列的单元格组成.机器人最初是在i行和j ...

  5. jenkins+ant+jmeter自动化环境搭建

    jmeter:测试接口的工具,支持java语言: ant:Apache Ant是一个Java库和命令行工具,其任务是将构建文件中描述的进程作为相互依赖的目标和扩展点.只要使用过Linux系统的读者,应 ...

  6. 克隆linux系统后,将eth1修改成eth0

    使用VMware克隆的linux系统之后,发现网卡信息只有eth1,却没有eth0.将eth1修改成eth0. 1.vi /etc/udev/rules.d/70-persistent-net.rul ...

  7. (一)Django项目架构介绍

    项目的架构为: 1.虚拟环境virtualenv 安装Django==2.1.3 安装pymysql 安装mysqlclient 安装其他等 2.项目结构为: 应用APP: blog -- 管理博客 ...

  8. Qt5学习(1)

    1. In Qt, if you want to apply styles to the main window  itself, you must apply it to  its central ...

  9. 【转】ArcGIS Server 10.1 动态图层

    ArcGISServer将GIS资源以服务的方式发布,能够让更多的人在Web上浏览.使用.不过,诸如气象.环保等方面的信息是实时变化的,按照之前常规的方法,我们先要将最新获得的信息组织成地图文档后再对 ...

  10. docker安装mtproto及报错解决方案

    安装docker:curl -sSL https://get.daocloud.io/docker | sh 给权限:usermod -aG docker [current_user] 启动:syst ...