上面谈了几个类的封装,这次我们讲讲使用的方式。
在实际过程中,我们怎么就能说明我们少了代码的编写呢?
例如我们需要一个类实现某个表的操作,我们只需要继承与我们的Base类就能实现了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NOAS.PublicOpinionMonitor.Access.Common; using NOAS.PublicOpinionMonitor.ENTITY; namespace NOAS.PublicOpinionMonitor.Access { public class ProxyDAL : AccessBase<t_po_monitor_proxy> { public ProxyDAL() : base(strTableName:"t_po_monitor_proxy",ColumsName:@" [monitorid] ,[monitor_name] ,[tkwid] ,[monitor_stardate] ,[monitor_enddate] ,[monitor_datetype] ,[monitor_interval] ,[data_begindate] ,[data_enddate] ,[exec_datetime] ,[monito_days] ,[updatedate] ,[status]") { } public List<t_po_monitor_proxy> getAllMonitorProxy() { return getListByWhere(string.Empty); } } }我坚持的东西是,自己的事情自己做。所以一般在做底层的架构涉的时候,每个类只能操作自己对应的表。这样让我们的代码也会更加的清晰。
在业务层的使用过程中,就更加简单了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NOAS.PublicOpinionMonitor.BIZ { public class ProxyBLL { public int addMonitorProxy(ENTITY.t_po_monitor_proxy proxyEntity) { new NOAS.PublicOpinionMonitor.Access.ProxyDAL().addEntity(proxyEntity); return proxyEntity.mproxyid; } public int updateMonitorProxy(ENTITY.t_po_monitor_proxy proxyEntity) { new NOAS.PublicOpinionMonitor.Access.ProxyDAL().updateEntity(proxyEntity); return proxyEntity.mproxyid; } } }
从这里也让我更加的喜欢去设计和架构东西。架构让程序编程更美。多想,多做,多看。让我们的代码变得更加清晰。
坚决不做码农,我们是攻城师。
/**
* 发送GET请求
*
* @param url url地址
* @return JSONObject
*/
public static String getAjaxDataFromURLForSMS(String url) {
long begin = System.currentTimeMillis();
LOG.info("request:" + url);
String result = null;
BufferedReader rd = null;
try {
URL u = new URL(/blog_article/url/index.html);
InputStream is = u.openStream();
rd = new BufferedReader(new InputStreamReader(is, "utf-8"));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
long end = System.currentTimeMillis();
LOG.info("response[" + (end - begin) + "]:" + sb.toString());
result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (rd != null) {
try {
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
*/
public static Document getXMLDataFromURL(/blog_article/String url/index.html) {
long begin = System.currentTimeMillis();
LOG.info("request:" + url);
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = null;
try {
URL u = new URL(/blog_article/url/index.html);
InputStream in = u.openStream();
doc = saxBuilder.build(in);
} catch (Exception e) {
e.printStackTrace();
}
if (LOG.isInfoEnabled()) {
long end = System.currentTimeMillis();
XMLOutputter outp = new XMLOutputter();
LOG.info("response[" + (end - begin) + "]:" + outp.outputString(doc));
}
return doc;
}
/**
* 发送GET请求*
* @param url
* url地址
* @return JSONObject
*/
public static JSONObject getJSONDataFromURL(/blog_article/String url/index.html) {
long begin = System.currentTimeMillis();
LOG.info("request:" + url);
JSONObject jsonObj = null;
BufferedReader rd = null;
try {
URL u = new URL(/blog_article/url/index.html);
InputStream is = u.openStream();
rd = new BufferedReader(new InputStreamReader(is, "utf-8"));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
long end = System.currentTimeMillis();
LOG.info("response[" + (end - begin) + "]:" + sb.toString());
jsonObj = new JSONObject(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (rd != null) {
try {
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return jsonObj;
}
/**
* 发送POST请求
*
* @param url
* url地址 params 参数
* @return JSONObject
*/
public static JSONObject getJSONDataFromURL(/blog_article/String url, String params/index.html) {
long begin = System.currentTimeMillis();
LOG.info("request:" + url);
JSONObject jsonObj = null;
HttpURLConnection huc = null;
BufferedReader in = null;
InputStreamReader isr = null;
InputStream is = null;
StringBuffer sb = new StringBuffer(333);
try {
URL u = new URL(/blog_article/url/index.html);
huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("POST");
huc.setDoOutput(true);
huc.getOutputStream().write(params.getBytes("utf-8"));
huc.getOutputStream().flush();
huc.getOutputStream().close();
is = huc.getInputStream();
isr = new InputStreamReader(is, "utf-8");
in = new BufferedReader(isr);
String line = null;
while ((line = i
场景描述:
有时我们需要在应用启动时,加载某些不常变的数据到缓存中,避免每次请求时查询数据库或其它数据源,以提高性能(准备连接,建立连接,关闭连接,减少数据读取的IO数).如果应用中刚好用到spring框架,正好可以用spring的机制实现这个功能
解决方案:
1:写一个类,实现BeanPostProcessor接口,这个接口有两个方法。
(1):postProcessBeforeInitialization方法,在spring中定义的bean初始化前调用这个方法
(2):postProcessAfterInitialization方法,在spring中定义的bean初始化后调用这个方法
例子代码:
public class CacheBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object obj, String arg1) throws BeansException { try { if(obj instanceof ColumnService) { ((ColumnService)obj).getColumnList();//加载栏目数据 }else if(obj instanceof TradeServiceImpl){ ((TradeServiceImpl)obj).getTradeList();//加载行业数据 } } catch (Exception e) { e.printStackTrace(); } return obj; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub return arg0; } }
2:在spring的xml文件中,添加
<!-- 初始缓存数据,初始化spring中定义的bean的前后都会用此实现类 -->
<bean id="cacheBeanPostProcessor" class="com.guagua.cache.CacheBeanPostProcessor"/>
3:在加载数据后,数据存在什么地方,由你来定,我这个例子是放到了一个“静态变量中”
@Service("columnService") public class ColumnServiceImpl implements ColumnService { public static List<Column> columnList = new ArrayList<Column>(); @Autowired private ColumnDao columnDao; public void setColumnDao(ColumnDao columnDao) { this.columnDao = columnDao; } @Override public List<Column> getColumnList() throws Exception { columnList = columnDao.getColumnList(); return columnList; } }
结束语:
如有疑问,请提出异议建意。