ApplicationContext:獲得spring定義的bean實例
pubilc class Test{
protected static ApplicationContext context;
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
context = new ClassPathXmlApplicationContext("classpath:epolicy-context-task-test.xml"); LinkExpiryDataJob linkExpiryDataJob = (LinkExpiryDataJob) context.getBean("linkExpiryDataJob");
}
}
根據配置文件獲取屬性的屬性值:
public class SpringCfgPropHelper {
private static Properties instance;
private static String classpath = "/epolicy-context.properties";
public void setClasspath(String classpath) {
SpringCfgPropHelper.classpath = classpath;
}
private synchronized static void init() {
//System.out.println("classpath:"+classpath);
Resource resource = new ClassPathResource(classpath);
try {
instance = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String key) {
if(instance==null) {
init();
}
return instance.getProperty(key);
}
}
Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); cal.getTime()
poi設置字體大小(來自poi官網)
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1);
// Create a new font and alter it.
Font font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
style.setFont(font);
HSSFDataFormat format=wb.createDataFormat();
style.setDataFormat(format.getFormat("@"));//設置文本輸出,避免將過長的數字,轉化成科學技術法
// Create a cell and put a value in it.
Cell cell = row.createCell(1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
Map countMap = new HashMap();
// 文件名稱配置的固定目錄+時間+文件名稱
String fileName = localPath + fileTimestamp + "/" + Const.INTERFACE_FILE_END + fileTimestamp + ".CSV";
BufferedReader br;
try {
br = new BufferedReader(new FileReader(fileName));
String csvLine = null;
while ((csvLine = br.readLine()) != null) {
csvLine = csvLine.replaceAll(",", ", "); // 防止截少字段
String[] lineData = csvLine.split(",");
if (lineData != null && lineData.length > 1) {
int count = -1;
try {
count = Integer.valueOf(this.trim(lineData[1]));
countMap.put(this.trim(lineData[0]), count);
} catch (NumberFormatException e) {
} catch (Exception e) {
//e.printStackTrace();
logger.error(e,e.fillInStackTrace());
}
}
}
} catch (FileNotFoundException e1) {
throw new ServiceException("獲取接口文件計數錯誤:", null, e1.fillInStackTrace());
} catch (IOException e) {
throw new ServiceException("獲取接口文件計數錯誤:", null, e.fillInStackTrace());
}
return countMap;
//.csv格式: string, num
//throw Exception後,程序中斷,後面代碼不再執行。
public class ThrowTest {
public static void main(String[] args) throws Exception {
int a=0;
int b=0;
if(a==b){
throw new Exception("做個小測試而已");
}
System.out.println("a= "+a+"; b="+b);
}
}
根據反射機制創建對象
Class cls=interfaceName.class;
InterfaceData obj = null;
obj = (InterfaceData) cls.newInstance();