源码学习到的零碎知识点

1.框架中的初始化配置,首先是加载各种配置,采用抽象的常量类去定义各种配置参数,定义受检异常,当捕捉不到具体的参数时终止程序;

2.多采用继承classPathXmlApplication的方式设置上下文环境,配置参数设置后,由于有些配置是单例的,为使他们重新实例化,要采用refresh()方法,刷新内存;

3.ConfigurableApplicationContext通常将自定义的上下文环境装载至此配置里,

SPI interface(串行外设接口) to be implemented by most if not all application contexts. Provides facilities to configure an application context in addition to the application context client methods in theorg.springframework.context.ApplicationContext interface.

Configuration and lifecycle methods are encapsulated here to avoid making them obvious to ApplicationContext client code. The present methods should only be used by startup and shutdown code.

4.Runtime.getRuntime().addShutdownHook(new线程),用于设置通知虚拟机运行的,常用system.exit()方法和CTRL+C均可停止虚拟机;

5.父Context和子Context有各自独立的生命周期,当虚拟机终止时,要将各种环境通过configurableContext.close方法关闭掉;主要目的是释放资源,解除线程占用的各种锁,将单例的类从内存中擦除掉;

5.1 需要注意的是这种环境上下文要对所有的线程设置可见性,private static volatile ConfigurableApplicationContext;

6.程序终止还有一点就是要通过网络协议,将占用的端口关掉,我公司的框架是通过dubbo中的ProtocolConfig.destryAll()方法实现;

7.更改异常提示消息,在Catch代码块中对异常进行友好的提示配置;

String msg =ex.getMessage();

System.err.println(msg);
ex.printStackTrace(System.err);
startupLogger.error(msg, ex);

8.log4j中的mdc和udc工具可以设置请求资源的RequestId;

9.程序终止的代码块中要定义程序从开始执行到终止总共用了多少时间;

if (startupLogger.isInfoEnabled()) {
/*  61 */         startupLogger.info("PizzaStartuped,times=" + (System.nanoTime() - t1) / 1000L / 1000.0D + "ms.");
/*     */       }


10.app程序上下文环境构建过程中基本上所有的属性和方法都设定了Static修饰,以确保其初始化对 优先顺序;

11,static方法中不能调用this方法的,且其staic方法中的变量都得是static,一句话总结:方便在不创建对象的情形下进行调用方法或者变量;

12.配置文件资源的获取和读取要设置线程安全的,资源获取方法上要添加synchronized修饰,spring中的ResourceUtil类可以方便的获取各种配置资源文件,将其转化为输入流,如:ResourceUtils.getURL(configResource).openStream();


13.System.getenv(环境参数的key),获取已经被写入内存的参数值;

14. properties.load(new InputStreamReader(resourceInput, "UTF-8"));此方法可以将输入流中inputStream中的编码设置为UTF-8;

       properties.load()方法也可以获取xml格式的文件资源;

15.Properties extends Hashtable<Object,Object>

16获取配置项.for (Object key : properties.keySet()) {
     String value = properties.getProperty((String)key, "");
  if ((value != null) && (value.length() > 0)) {
System.setProperty((String)key, value);
++++++++++++++++++++++++++++++++++++++++++++++++++++++

 17.断言是用来判定配置参数或者方法中的参数是否符合要求,如果不符合要求则抛出IllegalArgument异常;

Assert.notNull(clazz, "The class must not benull");
Assert.isTrue(i > 0, "The value must be greater than zero");

——————————————————————————————————————————————

18.Threadlocal创建线程的局部变量副本,使线程之间的变量相互隔离;

————————————————————————————————————————————————

19.NamedThreadLocal为线程副本起一个名字

ThreadLocal<Set<EncodedResource>>resourcesCurrentlyBeingLoaded =

new NamedThreadLocal<Set<EncodedResource>>("XMLbean definition resources currently being loaded");

——————————————————————————————————————————————————

20.HashSet是一个有初始化容量大小的HashMap,

Constructs a new, empty set; the backing HashMap instance has thespecified initial capacity and default load factor (0.75).

Parameters: initialCapacity the initial capacityof the hash table

HashSet(intinitialCapacity) {

        map = newHashMap<>(initialCapacity);}

——————————————————————————————————————————————

21.解析xml,注意inputsource类

InputStream inputStream = encodedResource.getResource().getInputStream();
            try {
                InputSource inputSource = new InputSource(inputStream);
                if (encodedResource.getEncoding() != null) {
                    inputSource.setEncoding(encodedResource.getEncoding());
                }
                return doLoadBeanDefinitions(inputSource, encodedResource.getResource());

-------------------------------------------

22.xml文件中解析到的bean也要到上下文环境中进行注册,参见spring中的doLoadBeanDefinitions(inputSource, encodedResource.getResource())方法;

------------------------

23.资源文件经解析注册后,已经实现了其价值,要将他们从内存中释放掉,各资源在set中也要用remove()方法释放掉;

HashSet对象.remove(encodedResource);

————————————————————————————

24.rootxml文件加载注册后,要关闭beanFactory()

if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }

——————————————————————

25.








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值