ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml")调试收获

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml")代码调试收获



1.通过super(parent)调用了父类AbstractApplicationContext的构造方法,使AbstractApplicationContext中

ResourcePatternResolver resourcePatternResolver=new PathMatchingResourcePatternResolver;引发学习http://cainiaoboke.iteye.com/blog/2327685

 

2.setConfigLocations(configLocations)调用父类AbstractRefreshableConfigApplicationContext的setConfigLocations方法,使中的private String[] configLocations=[Beans.xml]

 

3.重点refresh开始调用AbstractApplicationContext的refresh()方法

public void refresh() throws BeansException, IllegalStateException {  
        synchronized (this.startupShutdownMonitor) {  
            // 容器启动的预先准备,记录容器启动的时间和标记  
            prepareRefresh();  
  
            // 创建BeanFactory,如果已有就销毁,没有就创建。此类实现了对BeanDefinition的装载  
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();  
  
            // 配置BeanFactory标准上下文特性,如类装载器,PostProcesser等  
            prepareBeanFactory(beanFactory);  
  
            try {  
                // 在bean被装载后,提供一个修改BeanFactory的入口  
                postProcessBeanFactory(beanFactory);  
  
                // 调用postProcessBeanFactory  
                invokeBeanFactoryPostProcessors(beanFactory);  
  
                // 注册用于拦截bean创建过程中的BeanPostProcessors  
                registerBeanPostProcessors(beanFactory);  
  
                // Initialize message source for this context.  
                initMessageSource();  
  
                // Initialize event multicaster for this context.  
                initApplicationEventMulticaster();  
  
                // Initialize other special beans in specific context subclasses.  
                onRefresh();  
  
                // 注册监听器  
                registerListeners();  
  
                // 完成容器的初始化,里面的preInstantiateSingletons()完成对单例对象的创建  
                finishBeanFactoryInitialization(beanFactory);  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package org.example; public interface StudentDao { void list(); void delete(int id); } package org.example; public interface StudentService { void list(); void delete(int id); } package org.example; import org.springframework.stereotype.Repository; @Repository("mockStudentDao") public class MockStudentDao implements StudentDao{ @Override public void list() { System.out.println("All students are listed in MockStudentDao."); } @Override public void delete(int id) { System.out.println("Student No."+id+"is deleted in MockStudentDao."); } } package org.example; import org.springframework.stereotype.Repository; @Repository("myBatisStudentDao") public class MyBatisStudentDao implements StudentDao{ @Override public void list() { System.out.println("All students are listed in MyBatisStudentDao."); } @Override public void delete(int id) { System.out.println("Student No."+id+"is deleted in MyBatisStudentDao."); } } package org.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service("studentServiceImpl") public class StudentServiceImpl implements StudentService{ private final StudentDao studentDao; @Autowired public StudentServiceImpl(@Qualifier("myBatisStudentDao")StudentDao studentDao){ this.studentDao=studentDao; } @Override public void list() { this.studentDao.list(); } @Override public void delete(int id) { this.studentDao.delete(id); } } package org.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); StudentService studentService = (StudentService) ctx.getBean("studentServiceImpl"); studentService.list(); studentService.delete(10); } }
最新发布
05-17

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值