展开

关键词

Spring5源码学习(1)

context = new AnnotationConfigApplicationContext(AppConfig.class); context.close(); }} 先来看一下AnnotationConfigApplicationContext AnnotationConfigApplicationContext点开AnnotationConfigApplicationContext(AppConfig.class);方法查看源码:public AnnotationConfigApplicationContext(Class... annotatedClasses) { 调用默认无参构造器,里面有一大堆初始化逻辑 this(); 把传入的Class 也就是说,reader对象里面包含了一个this对象,也就是AnnotationConfigApplicationContext对象。 AnnotationConfigApplicationContext 实现了BeanDefinitionRegistry接口。

21771

spring bean初始化过程

从这个简单的代码深入,使用AnnotationConfigApplicationContext看一下spring bean的初始化过程public static void main(String[] args ) { ApplicationContext context = new AnnotationConfigApplicationContext(Application.class); MessagePrinter bean创建第一个方法进入AnnotationConfigApplicationContext的构造函数public AnnotationConfigApplicationContext() { this.reader AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this);}public AnnotationConfigApplicationContext (Class... annotatedClasses) { this(); register(annotatedClasses); refresh(); }AnnotationConfigApplicationContext

11440
  • 广告
    关闭

    云产品限时秒杀

    云服务器1核2G首年50元,还有多款热门云产品满足您的上云需求

  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    Spring核心——纯Java运行与@Bean

    而到了3.X之后的版本Spring为创建容器新增了一个入口类——AnnotationConfigApplicationContextAnnotationConfigApplicationContext的其他使用方法除了以上常规的使用方法,AnnotationConfigApplicationContext还有其他方式向容器添加Bean 可以使用AnnotationConfigApplicationContext::register方法来添加配置和Bean:public static void main(String args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan(com.acme); ctx.refresh();} 执行原理和上面介绍的一样。 如果你的工程中需要使用AnnotationConfigApplicationContext::register、AnnotationConfigApplicationContext::scan等方法创建容器和其中

    15530

    Spring注解式编程

    user = applicationContext.getBean(User.class); String s = user.sayHello(); System.out.println(s); }}入口类AnnotationConfigApplicationContext image.png四个构造方法:public AnnotationConfigApplicationContext() { 初始化Bean的读取和扫描器 this.reader = new AnnotatedBeanDefinitionReader (this); this.scanner = new ClassPathBeanDefinitionScanner(this);}public AnnotationConfigApplicationContext AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }public AnnotationConfigApplicationContext Class... annotatedClasses) { this(); 注册bean配置类 register(annotatedClasses); 刷新上下文 refresh(); }public AnnotationConfigApplicationContext

    20340

    读源码,我们可以从第一行读起

    我们整个源码的解析将以下面这句代码为入口:AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class 上面这行代码就是执行了AnnotationConfigApplicationContext中的一个构造函数,其代码如下:public AnnotationConfigApplicationContext (Class... annotatedClasses) { 1.调用了AnnotationConfigApplicationContext的空参构造方法 this(); 2.将配置类注册到容器中 register (annotatedClasses); 3.刷新容器 refresh();} 找到第一行代码我们继续查看AnnotationConfigApplicationContext的空参构造其代码如下:public AnnotationConfigApplicationContext() { 1.1 构造一个reader对象 this.reader = new AnnotatedBeanDefinitionReader

    20420

    Spring、Spring Boot和TestNG测试指南 - 测试@Configuration

    context; @BeforeMethod public void init() { context = new AnnotationConfigApplicationContext(); } @AfterMethod context = new AnnotationConfigApplicationContext(); @Before public void init() { EmbeddedDatabaseConnection.override context; @BeforeMethod public void init() { context = new AnnotationConfigApplicationContext(); } @AfterMethod context; @BeforeMethod public void init() { context = new AnnotationConfigApplicationContext(); } @AfterMethod context; @BeforeMethod public void init() { context = new AnnotationConfigApplicationContext(); } @AfterMethod

    41420

    SpringFramework之AnnotatedBeanDefinitionReader

    AnnotationConfigApplicationContext中使用到,如下List-1,AnnotatedBeanDefinitionReader的构造方法中参数为BeanDefinitionRegistry List-1public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry AnnotatedBeanDefinitionReader reader; private final ClassPathBeanDefinitionScanner scanner; ** * Create a new AnnotationConfigApplicationContext populated * through {@link #register} calls and then manually {@linkplain #refresh refreshed}. * public AnnotationConfigApplicationContext

    59630

    原创 | 一文了解那些和Spring Bean有关的注解!

    探讨主题:基础概念:@Bean 和 @Configuration使用AnnotationConfigApplicationContext 实例化Spring容器使用@Bean 注解使用@Configuration 实例化Spring容器AnnotationConfigApplicationContext 基于注解的上下文是Spring3.0 新添加的注解,它是ApplicationContext的一个具体实现, 通过使用无参数的构造器并且使用register方法进行注册,它和AnnotationConfigApplicationContext带参数的构造器起到的效果相同。 public class ApplicationTests { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class, OtherConfig.class); ctx.register

    15040

    Feign源码分析之FeignClientFactoryBean

    不存在,则调用createContext方法进行创建AnnotationConfigApplicationContext.register方法把配置类注册到ApplicationContext中,还设置了AnnotationConfigApplicationContext 的parent为当前Spring上下文,这样当在AnnotationConfigApplicationContext中获取不到bean时,就会从父ApplicationContext中获取 List-6public T getInstance(String name, Class type) { AnnotationConfigApplicationContext context = getContext(name )) { this.contexts.put(name, createContext(name)); } } } return this.contexts.get(name);} protected AnnotationConfigApplicationContext createContext(String name) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext

    66510

    Spring中重要的注解

    通过注解启动:通过启动一个AnnotationConfigApplicationContext 来引导这个@Configuration 注解的类,比如:AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();ctx.register(AppConfig.class);ctx.refresh();在web项目中,也可以使用 AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class context = new AnnotationConfigApplicationContext(MyConfiguration.class); ApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MyConfiguration.class); context.refresh

    28420

    聊聊spring cloud netflix ribbon的eager load

    orgspringframeworkcloudnetflixribbonSpringClientFactory.javapublic class SpringClientFactory extends NamedContextFactory {...... protected AnnotationConfigApplicationContext abstract class NamedContextFactoryimplements DisposableBean, ApplicationContextAware { ...... protected AnnotationConfigApplicationContext (name)) {this.contexts.put(name, createContext(name));}}}return this.contexts.get(name);} protected AnnotationConfigApplicationContext createContext(String name) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext generateDisplayName(name));context.refresh();return context;} ......}NamedContextFactory的getContext主要是返回或者创建AnnotationConfigApplicationContext

    19420

    原创 | 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration

    通过注解启动通过启动一个AnnotationConfigApplicationContext 来引导这个@Configuration 注解的类,比如:AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();ctx.register(AppConfig.class);ctx.refresh();在web项目中,也可以使用 AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class context = new AnnotationConfigApplicationContext(MyConfiguration.class); ApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MyConfiguration.class); context.refresh

    22630

    我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration

    通过注解启动通过启动一个AnnotationConfigApplicationContext 来引导这个@Configuration 注解的类,比如:AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();ctx.register(AppConfig.class);ctx.refresh();在web项目中,也可以使用 AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class context = new AnnotationConfigApplicationContext(MyConfiguration.class); ApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MyConfiguration.class); context.refresh

    19440

    逐行阅读Spring5.X源码(三) BeanDefinition的实现类详解,拔高

    context = new AnnotationConfigApplicationContext(); 注册配置类 context.register(Config.class); 模板BeanDefinition context = new AnnotationConfigApplicationContext(); 注册配置类 context.register(Config.class); 模板BeanDefinition context = new AnnotationConfigApplicationContext(); 注册配置类 context.register(Config.class); context.refresh public class Test { public static void main(String args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); 注册配置类 context.register(Config.class); context.refresh();

    36421

    聊聊spring cloud netflix ribbon的eager load

    orgspringframeworkcloudnetflixribbonSpringClientFactory.javapublic class SpringClientFactory extends NamedContextFactory { ......​ protected AnnotationConfigApplicationContext abstract class NamedContextFactory implements DisposableBean, ApplicationContextAware {​ ......​ protected AnnotationConfigApplicationContext { this.contexts.put(name, createContext(name)); } } } return this.contexts.get(name); }​ protected AnnotationConfigApplicationContext createContext(String name) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext NamedContextFactory的getContext主要是返回或者创建AnnotationConfigApplicationContext小结RibbonAutoConfiguration启用了

    39100

    【小家Spring】Spring容器加载Bean定义信息的两员大将:AnnotatedBeanDefinitionReader和ClassPathBeanDefinitionScanner

    此处只能用RootConfig,而不能AppConfig(启动报错),因为它要web容器支持,比如Tomcat ApplicationContext applicationContext = new AnnotationConfigApplicationContext 因此接下来,就是要去分析源码,看看这两大工具起的作用IOC容器加载Bean定义信息分析AnnotationConfigApplicationContext(spring-context包下)的继承图谱如下 简单来说,BeanDefinitionRegistry可以用来管理BeanDefinition,所以理解AnnotationConfigApplicationContext很关键,它是spring加载bean 源码跟踪: public AnnotationConfigApplicationContext(Class... annotatedClasses) { this(); 把该配置类(们)注册进来 register public AnnotationConfigApplicationContext() { this.reader = new AnnotatedBeanDefinitionReader(this);

    1K10

    bean放入Spring容器

    applicationContext中取,成功拿到**@Import(Person.class)public class Demo1 { public static void main(String args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean (MyImportBeanDefinitionRegistrar.class) public class Demo1 { public static void main(String args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean applicationContext = new AnnotationConfigApplicationContext(); MyBeanDefinitionRegistryPostProcessor

    7920

    Spring5.0源码深度解析之Spring基于注解启动流程分析

    由类图可知AnnotationConfigApplicationContext的祖宗是ApplicationContext,所有可以这样写:ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);ApplicationContext 的子类 》》》》AnnotationConfigApplicationContext AnnotationConfig启动流程源码分析@Configurationpublic class MyConfig { }ApplicationContext applicationContext = new AnnotationConfigApplicationContext

    62230

    springBoot快速入门

    * 2、提供应用程序的入口执行 * *@SpringBootApplicationpublic class App { public static void main(String args ) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.boot.spring); User user = context.getBean(User.class * *public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.boot.spring); @Component方式创建的对象,name默认为类名的首字母小写 User user

    13510

    Dubbo的两种常规启动方式

    consumerConsumerApplication.javapublic class ConsumerApplication { public static void main(String args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderApplication.class); context.start(); System.err.println ConsumerApplication { 这里需要使用Dubbo来进行注入 @Reference DemoService demoService; public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerApplication.class); ConsumerApplication customer

    38210

    相关产品

    • 云服务器

      云服务器

      腾讯云服务器(CVM)为您提供安全可靠的弹性云计算服务。只需几分钟,您就可以在云端获取和启用云服务器,并实时扩展或缩减云计算资源。云服务器 支持按实际使用的资源计费,可以为您节约计算成本。

    相关资讯

    热门标签

    活动推荐

      运营活动

      活动名称
      广告关闭

      扫码关注云+社区

      领取腾讯云代金券