面試題總結《2》

1.如何把一個字符串轉成數字java

第一位3  if("3".equals("3")) {value = 3}
第二位 if("2".equals(str)){value = 2*10 + value}

2 單利,如何確保一個類是單利spring

public class Singleton {  
    private static Singleton uniqueInstance = null;  
   
    private Singleton() {  
       // Exists only to defeat instantiation.  
    }  
   
    public static Singleton getInstance() {  
       if (uniqueInstance == null) {  
           uniqueInstance = new Singleton();  
       }  
       return uniqueInstance;  
    }  
    // Other methods...  
}

可是以上代碼並無考慮到單例模式在併發狀況下的場景數組

單例對象的初始化同步併發

public class GlobalConfig {  
    private static GlobalConfig instance = null;  
    private Vector properties = null;  
    private GlobalConfig() {  
      //Load configuration information from DB or file  
      //Set values for properties  
    }  
    private static synchronized void syncInit() {  
      if (instance == null) {  
        instance = new GlobalConfig();  
      }  
    }  
    public static GlobalConfig getInstance() {  
      if (instance == null) {  
        syncInit();  
      }  
      return instance;  
    }  
    public Vector getProperties() {  
      return properties;  
    }  
  }

單例對象的屬性更新同步app

public class GlobalConfig {  
 private static GlobalConfig instance;  
 private Vector properties = null;  
 private boolean isUpdating = false;  
 private int readCount = 0;  
 private GlobalConfig() {  
   //Load configuration information from DB or file  
      //Set values for properties  
 }  
 private static synchronized void syncInit() {  
  if (instance == null) {  
   instance = new GlobalConfig();  
  }  
 }  
 public static GlobalConfig getInstance() {  
  if (instance==null) {  
   syncInit();  
  }  
  return instance;  
 }  
 public synchronized void update(String p_data) {  
  syncUpdateIn();  
  //Update properties  
 }  
 private synchronized void syncUpdateIn() {  
  while (readCount > 0) {  
   try {  
    wait();  
   } catch (Exception e) {  
   }  
  }  
 }  
 private synchronized void syncReadIn() {  
  readCount++;  
 }  
 private synchronized void syncReadOut() {  
  readCount--;  
  notifyAll();  
 }  
 public Vector getProperties() {  
  syncReadIn();  
  //Process data  
  syncReadOut();  
  return properties;  
 }  
  }


3.有個n整數,存儲在數組array中,使其前面各數順序向後移動m個位置,最後的m個數變成最前面的m個數,並輸出spa

取模函code

4.spring如何在類裏面得到applicatoncontextorm

  1.建立一個類並讓其實現orgntext.ApplicationContextAware接口來讓Spring在啓動的時候爲咱們注入ApplicationContext對象.
  示例代碼:
  import org.springframework.beans.BeansException;
  import orgntext.ApplicationContext;
  import orgntext.ApplicationContextAware;
  public class MyApplicationContextUtil implements ApplicationContextAware {
  private static ApplicationContext context;
  //聲明一個靜態變量保存
  public void setApplicationContext(ApplicationContext contex) throws BeansException {
  ntext=contex;
  }
  public static ApplicationContext getContext(){
  return context;
  }
  }
  2.在applicationContext.xml文件中配置此bean,以便讓Spring啓動時自動爲咱們注入ApplicationContext對象.
  例:
  <!-- 這個bean主要是爲了獲得ApplicationContext 因此它不須要其它屬性-->
  <bean class="org.ing.springutil.MyApplicationContextUtil"></bean>
  3.有了這個ApplicationContext以後咱們就能夠調用其getBean("beanName")方法來獲得由Spring 管理全部對象.

5 spring如何解析配置文件,spring如何處理請求的。xml

相關文章
相關標籤/搜索