================================================
PS 憑著記憶來把他問的問題整理一下,并列出來,準備一一理解清楚
最開始的幾個問題我現在已經記不清楚了, 估計當時緊張了。
===================================================
你對Java的集合框架了解嗎? 能否說說常用的類?
說說Hashtable與HashMap的區別: 源代碼級別的區別呢?
平時用過的List有哪些? (除了ArrayList和LinkedList),ArrayList和LinkedList的區別?
ArrayList的特點,內部容器是如何擴充的?
Properties類的特點? 線程安全?
===============================================
平時使用過的框架有哪些? (我提到了Struts2)
請說一下Struts2的初始化?和類的創建?(從源代碼角度出發)
據你了解,除了反射還有什么方式可以動態的創建對象?(我提到了CGLIB…… 我以為他會接著問CGLIB,揪心中……,結果他沒問)
請說一下Struts2 是如何把Action交給Spring托管的?它是單例的還是多例? 你們頁面的表單對象是多例還是單例?
請說一下你們業務層對象是單例還是多例的?
請說一下Struts2源代碼中有哪些設計模式?
======================================================
請說一下,你覺得你最熟悉的技術特點? (我提到了并發編程)
請說一下線程安全出現的原因?
請說一下線程池的中斷策略(4個)? 各有什么特點?
請說一下Tomcat配置不同應用的不同端口如何配置? 如何配置數據源? 如何實現動態部署?
請說一下Java常用的優化?
你了解最新的Servlet規范嗎? 簡單說一下?(我提到了推)
那請你說一下“推”是如何實現的?
線程安全下,StringBuffer與StringBuilder的區別? 它們是如何擴充內部數組容量的? (源代碼)
請說一下Tomcat中的設計模式?(我提到觀察者模式)
是否可以說說Java反射的相關優化機制? (我說我不太清楚…… 他說沒關系 - -!)
請說一些Mysql的常用優化策略?
因為我之前有提到過“推”,他可能對我的知識面比較感興趣,要我說說平時都看些什么書,還了解一些什么其他的技術范疇。
(他首先提到SOA,我說有了解,并且是未來的趨勢,還有提到云計算,我說有過一定了解,但是并未深究)
=====================================================
之后是幾個職業方面的問題?
你覺得你的潛力? 你在團隊中的位置? 你覺得跟團隊中最好的還有哪些差距?你要花多少時間趕上他們?
你對阿里巴巴還有什么疑問嗎? (我很囧的問了,“阿里巴巴的牛人平時都跟你們有互動嗎?-----本意是指培訓,但是話沒說清楚……”,囧了……)
- 你對Java的集合框架了解嗎? 能否說說常用的類?

-
說說Hashtable與HashMap的區別(源代碼級別)
1.最明顯的區別在于Hashtable 是同步的(每個方法都是synchronized),而HashMap則不是.
2.HashMap繼承至AbstractMap,Hashtable繼承至Dictionary ,前者為Map的骨干, 其內部已經實現了Map所需 要做的大部分工作, 它的子類只需要實現它的少量方法即可具有Map的多項特性。而后者內部都為抽象方法,需要 它的實現類一一作自己的實現,且該類已過時
3.兩者檢測是否含有key時,hash算法不一致,HashMap內部需要將key的hash碼重新計算一邊再檢測,而 Hashtable則直接利用key本身的hash碼來做驗證。
HashMap:
Hashtable:
- int hash = key.hashCode();
4.兩者初始化容量大小不一致,HashMap內部為 16*0.75 , Hashtable 為 11*0.75
HashMap:
- static final int DEFAULT_INITIAL_CAPACITY = 16;
- static final float DEFAULT_LOAD_FACTOR = 0.75f;
- public HashMap() {
- this.loadFactor = DEFAULT_LOAD_FACTOR;
- threshold=(int)(DEFAULT_INITIAL_CAPACITY*DEFAULT_LOAD_FACTOR);
- table = new Entry[DEFAULT_INITIAL_CAPACITY];
- init();
- }
- ………………………………
Hashtable:
- public Hashtable() {
- this(11, 0.75f);
- }
- -----
- public Hashtable(int initialCapacity, float loadFactor) {
- ..........
- this.loadFactor = loadFactor;
- table = new Entry[initialCapacity];
- threshold = (int)(initialCapacity * loadFactor);
- }
其實后續的區別應該還有很多, 這里先列出4點。
- 平時除了ArrayList和LinkedList外,還用過的List有哪些?
ArrayList和LinkedList的區別?
ArrayList和LinkedList的區別?
事實上,我用過的List主要就是這2個, 另外用過Vector.
ArrayList和LinkedList的區別:
- 毫無疑問,第一點就是兩者的內部數據結構不同, ArrayList內部元素容器是一個Object的數組,
而LinkedList內部實際上一個鏈表的數據結構,其有一個內部類來表示鏈表.
- (ArrayList)
- private transient Object[] elementData;
- ………………………………………………………………………………
- (LinkedList)
- private transient Entry<E> header = new Entry<E>(null, null, null);/鏈表頭
- //內部鏈表類.
- private static class Entry<E> {
- E element; //數據元素
- Entry<E> next; // 前驅
- Entry<E> previous;//后驅
- Entry(E element, Entry<E> next, Entry<E> previous) {
- this.element = element;
- this.next = next;
- this.previous = previous;
- }
- }
- 兩者的父類不同,也就決定了兩者的存儲形式不同。 ArrayList繼承于 AbstractList,而LinkedList繼承于AbstractSequentialList. 兩者都實現了List的骨干結構,只是前者的訪問形式趨向于 “隨機訪問”數據存儲(如數組),后者趨向于 “連續訪問”數據存儲(如鏈接列表)
- public class ArrayList<E> extends AbstractList<E>
- ---------------------------------------------------------------------------------------
- public class LinkedList<E> extends AbstractSequentialList<E>
- 再有就是兩者的效率問題, ArrayList基于數組實現,所以毫無疑問可以直接用下標來索引,其索引數據快,插入元素設計到數組元素移動,或者數組擴充,所以插入元素要慢。LinkedList基于鏈表結構,插入元素只需要改變插入元素的前后項的指向即可,故插入數據要快,而索引元素需要向前向后遍歷,所以索引元素要慢。
- ArrayList的特點,內部容器是如何擴充的?
- public void ensureCapacity(int minCapacity) {
- modCount++;
- int oldCapacity = elementData.length;
- if (minCapacity > oldCapacity) {
- Object oldData[] = elementData;
- //這里擴充的大小為原大小的大概 60%
- int newCapacity = (oldCapacity * 3) / 2 + 1;
- if (newCapacity < minCapacity)
- newCapacity = minCapacity;
- //創建一個指定大小的新數組來覆蓋原數組
- elementData = Arrays.copyOf(elementData, newCapacity);
- }
- }
- Properties類的特點? 線程安全嗎?
Properties 繼承于Hashtable,,所以它是線程安全的. 其特點是: 它表示的是一個持久的屬性集,它可以保存在流中或者從流中加載,屬性列表的每一個鍵和它所對應的值都是一個“字符串” 其中,常用的方法是load()方法,從流中加載屬性:
- <SPAN style="FONT-WEIGHT: normal">public synchronized void load(InputStream inStream) throws IOException {
- // 將輸入流轉換成LineReader
- load0(new LineReader(inStream));
- }
-
- private void load0(LineReader lr) throws IOException {
- char[] convtBuf = new char[1024];
- int limit;
- int keyLen;
- int valueStart;
- char c;
- boolean hasSep;
- boolean precedingBackslash;
- // 一行一行處理
- while ((limit = lr.readLine()) >= 0) {
- c = 0;
- keyLen = 0;
- valueStart = limit;
- hasSep = false;
- precedingBackslash = false;
- // 下面用2個循環來處理key,value
- while (keyLen < limit) {
- c = lr.lineBuf[keyLen];
- // need check if escaped.
- if ((c == '=' || c == ':') && !precedingBackslash) {
- valueStart = keyLen + 1;
- hasSep = true;
- break;
- } else if ((c == ' ' || c == '\t' || c == '\f')
- && !precedingBackslash) {
- valueStart = keyLen + 1;
- break;
- }
- if (c == '\\') {
- precedingBackslash = !precedingBackslash;
- } else {
- precedingBackslash = false;
- }
- keyLen++;
- }
-
- while (valueStart < limit) {
- c = lr.lineBuf[valueStart];
- if (c != ' ' && c != '\t' && c != '\f') {
- if (!hasSep && (c == '=' || c == ':')) {
- hasSep = true;
- } else {
- break;
- }
- }
- valueStart++;
- }
-
- String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
- String value = loadConvert(lr.lineBuf, valueStart, limit
- - valueStart, convtBuf);
- // 存入內部容器中,這里用的是Hashtable 內部的方法.
- put(key, value);
- }
- }</SPAN>
LineReader類,是Properties內部的類:
- <SPAN style="FONT-WEIGHT: normal">class LineReader {
- public LineReader(InputStream inStream) {
- this.inStream = inStream;
- inByteBuf = new byte[8192];
- }
-
- public LineReader(Reader reader) {
- this.reader = reader;
- inCharBuf = new char[8192];
- }
-
- byte[] inByteBuf;
- char[] inCharBuf;
- char[] lineBuf = new char[1024];
- int inLimit = 0;
- int inOff = 0;
- InputStream inStream;
- Reader reader;
-
- /**
- * 讀取一行
- *
- * @return
- * @throws IOException
- */
- int readLine() throws IOException {
- int len = 0;
- char c = 0;
- boolean skipWhiteSpace = true;// 空白
- boolean isCommentLine = false;// 注釋
- boolean isNewLine = true;// 是否新行.
- boolean appendedLineBegin = false;// 加 至行開始
- boolean precedingBackslash = false;// 反斜杠
- boolean skipLF = false;
- while (true) {
- if (inOff >= inLimit) {
- // 從輸入流中讀取一定數量的字節并將其存儲在緩沖區數組inCharBuf/inByteBuf中,這里區分字節流和字符流
- inLimit = (inStream == null) ? reader.read(inCharBuf)
- : inStream.read(inByteBuf);
- inOff = 0;
- // 讀取到的為空.
- if (inLimit <= 0) {
- if (len == 0 || isCommentLine) {
- return -1;
- }
- return len;
- }
- }
- if (inStream != null) {
- // 由于是字節流,需要使用ISO8859-1來解碼
- c = (char) (0xff & inByteBuf[inOff++]);
- } else {
- c = inCharBuf[inOff++];
- }
-
- if (skipLF) {
- skipLF = false;
- if (c == '\n') {
- continue;
- }
- }
- if (skipWhiteSpace) {
- if (c == ' ' || c == '\t' || c == '\f') {
- continue;
- }
- if (!appendedLineBegin && (c == '\r' || c == '\n')) {
- continue;
- }
- skipWhiteSpace = false;
- appendedLineBegin = false;
- }
- if (isNewLine) {
- isNewLine = false;
- if (c == '#' || c == '!') {
- // 注釋行,忽略.
- isCommentLine = true;
- continue;
- }
- }
- // 讀取真正的屬性內容
- if (c != '\n' && c != '\r') {
- // 這里類似于ArrayList內部的容量擴充,使用字符數組來保存讀取的內容.
- lineBuf[len++] = c;
- if (len == lineBuf.length) {
- int newLength = lineBuf.length * 2;
- if (newLength < 0) {
- newLength = Integer.MAX_VALUE;
- }
- char[] buf = new char[newLength];
- System.arraycopy(lineBuf, 0, buf, 0, lineBuf.length);
- lineBuf = buf;
- }
- if (c == '\\') {
- precedingBackslash = !precedingBackslash;
- } else {
- precedingBackslash = false;
- }
- } else {
- // reached EOL 文件結束
- if (isCommentLine || len == 0) {
- isCommentLine = false;
- isNewLine = true;
- skipWhiteSpace = true;
- len = 0;
- continue;
- }
- if (inOff >= inLimit) {
- inLimit = (inStream == null) ? reader.read(inCharBuf)
- : inStream.read(inByteBuf);
- inOff = 0;
- if (inLimit <= 0) {
- return len;
- }
- }
- if (precedingBackslash) {
- len -= 1;
- skipWhiteSpace = true;
- appendedLineBegin = true;
- precedingBackslash = false;
- if (c == '\r') {
- skipLF = true;
- }
- } else {
- return len;
- }
- }
- }
- }
- } </SPAN>
這里特別的是,實際上,Properties從流中加載屬性集合,是通過將流中的字符或者字節分成一行行來處理的。
- 請說一下Struts2的初始化?和類的創建?(從源代碼角度出發)
(我當時回答這個問題的思路我想應該對了, 我說是通過反射加配置文件來做的)
由于這個問題研究起來可以另外寫一篇專門的模塊,這里只列出相對簡單的流程,后續會希望有時間整理出具體的細節: 首先,Struts2是基于Xwork框架的,如果你有仔細看過Xwork的文檔,你會發現,它的初始化過程基于以下幾個類: Configuring XWork2 centers around the following classes:- 1. ConfigurationManager 2. ConfigurationProvider 3. Configuration 而在ConfigurationProvider的實現類XmlConfigurationProvider 的內部,你可以看到下面的代碼
- <SPAN style="FONT-WEIGHT: normal"> public XmlConfigurationProvider() {
- this("xwork.xml", true);
- }</SPAN>
同樣的,Struts2的初始化也是這樣的一個類,只不過它繼承于Xwork原有的類,并針對Struts2做了一些特別的定制。
- <SPAN style="FONT-WEIGHT: normal">public class StrutsXmlConfigurationProvider
- extends XmlConfigurationProvider {
- public StrutsXmlConfigurationProvider(boolean errorIfMissing)
- {
- this("struts.xml", errorIfMissing, null);
- }
- …… </SPAN>
如果你要查看這個類在哪里調用了,你會追蹤到Dispatch的類,
記得嗎? 我們使用Struts2,第一步就是在Web.xml中配置一個過濾器 FilterDispatcher,
沒錯,在web容器初始化過濾器的時候, 同時也會初始化Dispatch..
FilterDispatch.init():
- <SPAN style="FONT-WEIGHT: normal">public void init(FilterConfig filterConfig)
- throws ServletException {
- try {
- this.filterConfig = filterConfig;
- initLogging();
- dispatcher = createDispatcher(filterConfig);
- dispatcher.init();////初始化Dispatcher.
- dispatcher.getContainer().inject(this);
- staticResourceLoader.setHostConfig(new FilterHostConfig(filterConfig));
- } finally {
- ActionContext.setContext(null);
- }
- } </SPAN>
Dispatch.init():
- <SPAN style="FONT-WEIGHT: normal">//這里是加載配置文件, 真正初始化Struts2的Action實例還沒開始,
- public void init() {
- if (configurationManager == null) {
- configurationManager =
- new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
- }
- init_DefaultProperties(); // [1]
- init_TraditionalXmlConfigurations(); // [2]
- init_LegacyStrutsProperties(); // [3]
- init_CustomConfigurationProviders(); // [5]
- init_FilterInitParameters() ; // [6]
- init_AliasStandardObjects() ; // [7]
- Container container = init_PreloadConfiguration();
- container.inject(this);
- init_CheckConfigurationReloading(container);
- init_CheckWebLogicWorkaround(container);
- if (!dispatcherListeners.isEmpty()) {
- for (DispatcherListener l : dispatcherListeners) {
- l.dispatcherInitialized(this);
- }
- }
- } </SPAN>
到初始化Action類的時候, 你需要去FilterDispatcher的doFilter方法去看代碼, 你會發現:
- <SPAN style="FONT-WEIGHT: normal">public void doFilter(ServletRequest req, ServletResponse res,
- FilterChain chain) throws IOException, ServletException {
- ……
- dispatcher.serviceAction(request, response, servletContext, mapping);</SPAN>
再追蹤到Dispatcher類,看到這個方法:
- <SPAN style="FONT-WEIGHT: normal"> public void serviceAction(HttpServletRequest request,
- HttpServletResponse response, ServletContext context,
- ActionMapping mapping) throws ServletException {
- ……
- ActionProxy proxy =config.getContainer().getInstance(
- ActionProxyFactory.class).
- createActionProxy(namespace,
- name,
- method,
- extraContext,
- true, false);
-
- ……</SPAN>
這行代碼已經明確的告訴你了, 它的作用就是創建ActionProxy,而我們想要知道的是,
他是如何創建的;
而上面代碼中的config,實際上是Xwork中的.Configuration, 如果你打開Xwork源代碼,你會發現,他其實是一個接口, 真正做處理的,這里是
com.opensymphony.xwork2.config.impl.DefaultConfiguration類, 通過它的getContainer()方法,獲取到一個Container類型的實例,而Container也是一個接口, 其實現類是:
- <SPAN style="FONT-WEIGHT: normal">public synchronized void load(InputStream inStream) throws IOException {
- // 將輸入流轉換成LineReader
- load0(new LineReader(inStream));
- }
- private void load0(LineReader lr) throws IOException {
- char[] convtBuf = new char[1024];
- int limit;
- int keyLen;
- int valueStart;
- char c;
- boolean hasSep;
- boolean precedingBackslash;
- // 一行一行處理
- while ((limit = lr.readLine()) >= 0) {
- c = 0;
- keyLen = 0;
- valueStart = limit;
- hasSep = false;
- precedingBackslash = false;
- // 下面用2個循環來處理key,value
- while (keyLen < limit) {
- c = lr.lineBuf[keyLen];
- // need check if escaped.
- if ((c == '=' || c == ':') && !precedingBackslash) {
- valueStart = keyLen + 1;
- hasSep = true;
- break;
- } else if ((c == ' ' || c == '\t' || c == '\f')
- && !precedingBackslash) {
- valueStart = keyLen + 1;
- break;
- }
- if (c == '\\') {
- precedingBackslash = !precedingBackslash;
- } else {
- precedingBackslash = false;
- }
- keyLen++;
- }
- while (valueStart < limit) {
- c = lr.lineBuf[valueStart];
- if (c != ' ' && c != '\t' && c != '\f') {
- if (!hasSep && (c == '=' || c == ':')) {
- hasSep = true;
- } else {
- break;
- }
- }
- valueStart++;
- }
- String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
- String value = loadConvert(lr.lineBuf, valueStart, limit
- - valueStart, convtBuf);
- // 存入內部容器中,這里用的是Hashtable 內部的方法.
- put(key, value);
- }
- }</SPAN>
- <SPAN style="FONT-WEIGHT: normal">class LineReader {
- public LineReader(InputStream inStream) {
- this.inStream = inStream;
- inByteBuf = new byte[8192];
- }
- public LineReader(Reader reader) {
- this.reader = reader;
- inCharBuf = new char[8192];
- }
- byte[] inByteBuf;
- char[] inCharBuf;
- char[] lineBuf = new char[1024];
- int inLimit = 0;
- int inOff = 0;
- InputStream inStream;
- Reader reader;
- /**
- * 讀取一行
- *
- * @return
- * @throws IOException
- */
- int readLine() throws IOException {
- int len = 0;
- char c = 0;
- boolean skipWhiteSpace = true;// 空白
- boolean isCommentLine = false;// 注釋
- boolean isNewLine = true;// 是否新行.
- boolean appendedLineBegin = false;// 加 至行開始
- boolean precedingBackslash = false;// 反斜杠
- boolean skipLF = false;
- while (true) {
- if (inOff >= inLimit) {
- // 從輸入流中讀取一定數量的字節并將其存儲在緩沖區數組inCharBuf/inByteBuf中,這里區分字節流和字符流
- inLimit = (inStream == null) ? reader.read(inCharBuf)
- : inStream.read(inByteBuf);
- inOff = 0;
- // 讀取到的為空.
- if (inLimit <= 0) {
- if (len == 0 || isCommentLine) {
- return -1;
- }
- return len;
- }
- }
- if (inStream != null) {
- // 由于是字節流,需要使用ISO8859-1來解碼
- c = (char) (0xff & inByteBuf[inOff++]);
- } else {
- c = inCharBuf[inOff++];
- }
- if (skipLF) {
- skipLF = false;
- if (c == '\n') {
- continue;
- }
- }
- if (skipWhiteSpace) {
- if (c == ' ' || c == '\t' || c == '\f') {
- continue;
- }
- if (!appendedLineBegin && (c == '\r' || c == '\n')) {
- continue;
- }
- skipWhiteSpace = false;
- appendedLineBegin = false;
- }
- if (isNewLine) {
- isNewLine = false;
- if (c == '#' || c == '!') {
- // 注釋行,忽略.
- isCommentLine = true;
- continue;
- }
- }
- // 讀取真正的屬性內容
- if (c != '\n' && c != '\r') {
- // 這里類似于ArrayList內部的容量擴充,使用字符數組來保存讀取的內容.
- lineBuf[len++] = c;
- if (len == lineBuf.length) {
- int newLength = lineBuf.length * 2;
- if (newLength < 0) {
- newLength = Integer.MAX_VALUE;
- }
- char[] buf = new char[newLength];
- System.arraycopy(lineBuf, 0, buf, 0, lineBuf.length);
- lineBuf = buf;
- }
- if (c == '\\') {
- precedingBackslash = !precedingBackslash;
- } else {
- precedingBackslash = false;
- }
- } else {
- // reached EOL 文件結束
- if (isCommentLine || len == 0) {
- isCommentLine = false;
- isNewLine = true;
- skipWhiteSpace = true;
- len = 0;
- continue;
- }
- if (inOff >= inLimit) {
- inLimit = (inStream == null) ? reader.read(inCharBuf)
- : inStream.read(inByteBuf);
- inOff = 0;
- if (inLimit <= 0) {
- return len;
- }
- }
- if (precedingBackslash) {
- len -= 1;
- skipWhiteSpace = true;
- appendedLineBegin = true;
- precedingBackslash = false;
- if (c == '\r') {
- skipLF = true;
- }
- } else {
- return len;
- }
- }
- }
- }
- } </SPAN>
- 請說一下Struts2的初始化?和類的創建?(從源代碼角度出發)
- <SPAN style="FONT-WEIGHT: normal"> public XmlConfigurationProvider() {
- this("xwork.xml", true);
- }</SPAN>
- <SPAN style="FONT-WEIGHT: normal">public class StrutsXmlConfigurationProvider
- extends XmlConfigurationProvider {
- public StrutsXmlConfigurationProvider(boolean errorIfMissing)
- {
- this("struts.xml", errorIfMissing, null);
- }
- …… </SPAN>
- <SPAN style="FONT-WEIGHT: normal">public void init(FilterConfig filterConfig)
- throws ServletException {
- try {
- this.filterConfig = filterConfig;
- initLogging();
- dispatcher = createDispatcher(filterConfig);
- dispatcher.init();////初始化Dispatcher.
- dispatcher.getContainer().inject(this);
- staticResourceLoader.setHostConfig(new FilterHostConfig(filterConfig));
- } finally {
- ActionContext.setContext(null);
- }
- } </SPAN>
- <SPAN style="FONT-WEIGHT: normal">//這里是加載配置文件, 真正初始化Struts2的Action實例還沒開始,
- public void init() {
- if (configurationManager == null) {
- configurationManager =
- new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
- }
- init_DefaultProperties(); // [1]
- init_TraditionalXmlConfigurations(); // [2]
- init_LegacyStrutsProperties(); // [3]
- init_CustomConfigurationProviders(); // [5]
- init_FilterInitParameters() ; // [6]
- init_AliasStandardObjects() ; // [7]
- Container container = init_PreloadConfiguration();
- container.inject(this);
- init_CheckConfigurationReloading(container);
- init_CheckWebLogicWorkaround(container);
- if (!dispatcherListeners.isEmpty()) {
- for (DispatcherListener l : dispatcherListeners) {
- l.dispatcherInitialized(this);
- }
- }
- } </SPAN>
- <SPAN style="FONT-WEIGHT: normal">public void doFilter(ServletRequest req, ServletResponse res,
- FilterChain chain) throws IOException, ServletException {
- ……
- dispatcher.serviceAction(request, response, servletContext, mapping);</SPAN>
- <SPAN style="FONT-WEIGHT: normal"> public void serviceAction(HttpServletRequest request,
- HttpServletResponse response, ServletContext context,
- ActionMapping mapping) throws ServletException {
- ……
- ActionProxy proxy =config.getContainer().getInstance(
- ActionProxyFactory.class).
- createActionProxy(namespace,
- name,
- method,
- extraContext,
- true, false);
- ……</SPAN>
com.opensymphony.xwork2.inject.ContainerImpl 他的getInstance(Class clazz):
public <T> T getInstance(final Class<T> type) {
- return callInContext(new ContextualCallable<T>() {
- public T call(InternalContext context) {
- return getInstance(type, context);
- }
- });
返回的是你傳入的對象,而在這里就是:ActionProxyFactory(也是接口,真正返回的是com.opensymphony.xwork2.DefaultActionProxyFactory)
- public ActionProxy createActionProxy(ActionInvocation inv, String namespace, String actionName, String methodName,
- boolean executeResult, boolean cleanupContext) {
- DefaultActionProxy proxy = new DefaultActionProxy(inv,
- namespace, actionName, methodName, executeResult, cleanupContext);
- container.inject(proxy);
- proxy.prepare();
- return proxy;
- }
- protected void prepare() {
- ……
- invocation.init(this);
- ……
- }
OK, 我們進去看看,這里發生了什么?
- public void init(ActionProxy proxy) {
- ……
- createAction(contextMap);
- ……
- }
- protected void createAction(Map<String, Object> contextMap) {
- // load action
- String timerKey = "actionCreate: " + proxy.getActionName();
- ……
- action =
- objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);
- ……
- public Object buildAction(String actionName, String namespace, ActionConfig config, Map<String, Object> extraContext)
- throws Exception {
- return buildBean(config.getClassName(), extraContext);
- }
- public Object buildBean(String className, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
- Class clazz = getClassInstance(className);//根據Action的名字,進行初始化
- Object obj = buildBean(clazz, extraContext);
- //利用反射來做實例初始化.
- if (injectInternal) {
- injectInternalBeans(obj);
- }
- return obj;
- }
- public Class getClassInstance(String className) throws ClassNotFoundException {
- if (ccl != null) {
- return ccl.loadClass(className);
- }
- return
- ClassLoaderUtil.loadClass(className, this.getClass());
- }
- public Object buildBean(Class clazz, Map<String, Object> extraContext) throws Exception {
- return clazz.newInstance();
- }