源碼學習到的零碎知識點

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.








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章