微信開發準備(三)--框架以及工具的基本使用
轉自:http://www.cuiyongzhi.com/post/35.html
在前面兩篇中我們從基本的專案建立到框架搭建,將專案已經搭建成功,並將基本的配置項也都已經配置完成,那麼這裡我們就進入到對框架的熟悉和一個工具generator的使用!
(一)專案部分配置檔案的初始化
我們在前面框架中層在web.xml檔案中配置了一個啟動Servlet初始化檔案,這裡做的就是在專案中需要用到某些配置檔案的時候,我們在這個時候對配置檔案中的值初始化到公共Properties中,以方便後面的呼叫,基本程式碼實現如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package
com.cuiyongzhi.web.start;
import
javax.servlet.ServletConfig;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet; /**
*
* ClassName: InterfaceUrlIntiServlet
*
* @Description: 專案檔案初始化
* @author dapengniao
* @date 2015/10/13
*/
public
class
InterfaceUrlIntiServlet
extends
HttpServlet {
private
static
final
long
serialVersionUID = 1L;
@Override
public
void
init(ServletConfig config)
throws
ServletException {
InterfaceUrlInti.init();
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package
com.cuiyongzhi.web.start;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Properties;
import
com.cuiyongzhi.web.util.GlobalConstants;
/**
*
* ClassName: InterfaceUrlInti
* @Description: 專案啟動配置檔案初始化
* @author dapengniao
* @date 2015/10/13
*/
public
class
InterfaceUrlInti {
public
synchronized
static
void
init(){
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Properties props =
new
Properties();
if
(GlobalConstants.interfaceUrlProperties==
null
){
GlobalConstants.interfaceUrlProperties =
new
Properties();
}
InputStream in =
null
;
try
{
in = cl.getResourceAsStream(
"interface_url.properties"
);
props.load(in);
for
(Object key : props.keySet()){
GlobalConstants.interfaceUrlProperties.put(key, props.get(key));
}
props =
new
Properties();
in = cl.getResourceAsStream(
"wechat.properties"
);
props.load(in);
for
(Object key : props.keySet()){
GlobalConstants.interfaceUrlProperties.put(key, props.get(key));
}
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
if
(in!=
null
){
try
{
in.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
return
;
}
}
|
在這裡我初始化化了兩個檔案,一個是用來配置在微信開發中經常用的到appid、AppSecret的引數(wechat.properties),另外一個用來初始化我們經常用到的http請求的url地址interface_url.properties!
初始化成功之後我們只需要通過下面的方法即可在專案中任何想用的地方去使用:GlobalConstants.getInterfaceUrl(key),如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package
com.cuiyongzhi.web.util;
import
java.util.Properties;
public
class
GlobalConstants {
public
static
Properties interfaceUrlProperties;
/**
*
* @Description: TODO
* @param @param key
* @param @return
* @author dapengniao
* @date 2015年10月13日 下午4:59:14
*/
public
static
String getInterfaceUrl(String key) {
return
(String) interfaceUrlProperties.get(key);
}
}
|
(二)對日誌檔案的配置說明
在我搭建的開發環境中採用的是log4j日誌記錄的方式,這種方式對普通專案是沒有問題的,後續將有可能升級為logback,首先我們在resources下新建檔案log4j.properties,簡單配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
log4j.rootLogger=DEBUG,Console,File
#ERROR,WARN,INFO,DEBUG 日誌輸出等級依次降低,可以根據自己的需求自己調整輸出等級
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Threshold=DEBUG
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=${catalina.base}/wechatlogs/wechat.log
log4j.appender.File.Threshold=INFO
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.Append=true
log4j.appender.File.ImmediateFlush=true
log4j.appender.File.DatePattern=yyyy-MM-dd'.log'
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
|
有了上面的配置檔案之後我們在web.xml中加入如下的啟動配置即可:
1 2 3 4 5 6 7 |
<
context-param
>
<
param-name
>log4jConfigLocation</
param-name
>
<
param-value
>classpath:log4j.properties</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.springframework.web.util.Log4jConfigListener</
listener-class
>
</
listener
>
|
簡單的使用如下圖所示:
(三)Mybatis工具Generator
在這裡我要推薦一款工具Generator,在專案開發中他給我節省了很多,他的作用是讓我們能很方便生成我們需要的表對應的pojo、mapping、dao的程式碼,而且使用起來非常簡單,由於這篇文章篇幅不短了,所以我開了一篇新的來詳細講解它的使用,地址:http://www.cuiyongzhi.com/?id=36