Arganzheng's Blog

stay hungry, stay foolish

Spring与web MVC的整合——Spring的应用上下文管理

问题1 如何让web容器加载你的web MVC框架 对于基于servlet的web容器来说,遵循的是servlet规范,入口配置文件是web.xml。这类web容器会在启动的时候会而且仅会加载如下三种类型的对象: servlet context listener filter 而且有一定的加载和销毁顺序! Loading Servlets, Context Listeners, and Filters Servlets, Context Listeners, and Filters are loaded and destroyed in the following order: Order of loading: ...

创建可执行的jar包

今天打算将Swing桌面程序打包成一个可以执行的jar包发布,方便用户使用。 可执行的jar包其实原理很简单,就是在jar包的manifest文件中用Main-Class标明你的应用程序入口: Main-Class: classname。其中classname是类的全路径名,不包含.class后缀,跟Spring的配置一样。 然后你就可以直接鼠标双击该jar包,或者在命令行中执行如下命令启动该jar了: java -jar JAR-name 在ant脚本中这通过一个简单的脚本就可以完成了: <project name="me.arganzheng.study.util" basedir="." default="usage"> ...

JUnit与Spring的整合——JUnit的TestCase如何自动注入Spring容器托管的对象

问题 在Java中,一般使用JUnit作为单元测试框架,测试的对象一般是Service和DAO,也可能是RemoteService和Controller。所有这些测试对象基本都是Spring托管的,不会直接new出来。而每个TestCase类却是由JUnit创建的。如何在每个TestCase实例中注入这些依赖呢? 预期效果 我们希望能够达到这样的效果: package me.arganzheng.study; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** ...

Quartz与Spring的整合-使用Spring的FactoryBean实现动态Properties

问题 每个Quartz调度器实例都需要有一个标识,这是在配置org.springframework.scheduling.quartz.SchedulerFactoryBean的时候配置进去的。例如: <?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans h...

Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象

问题 Quartz中的job是由Quartz框架动态创建的(配置该job的classname,通过反射创建),而job一般会依赖到配置在spring中的bean,怎样获取或者更好的自动注入这些依赖bean呢? 预期效果 我们希望达到这样的效果: /** * * 取消超时未支付订单的任务。 * * @author arganzheng */ public class CancelUnpaidOrderTask implements Job { @Autowired private AppOrderService orderService; @Override public void execute(JobExec...

Linux下如何备份旧文件

将30天前的文件打包压缩备份,然后删除原文件 今天王岩发了一个shell脚本让大家review一下: mmddnew=`date "+%Y%m%d"` basedir="/data/tomcatlog/api/reconciliationFiles/" echo ${mddnew} cd ${basedir} echo "压缩" find ./ -maxdepth 1 -type f -mtime +30 | xargs tar czvf ./${mmddnew}.tar echo "移动压缩包" mv ./${mmddnew}.tar ./bak/ echo "删除旧的文件" find ./ -maxdepth 1 -type f -mtime +30 -ex...

maven的resources插件

maven的resources插件职责是将项目的资源文件拷贝到目标目录。maven将资源划分为两类:main resources 和 test resources。因此有如下三个相应的goal: 1. resources:resources command: mvn resources:resources bound by default to the process-resources life-cycle phase copies the resources for the main source code to the main output directory the resources for your main source cod...

Spring事务配置

原理 Spring是基于proxy方式的事务实现机制。如果你的target实现了某个接口,那么Spring可以根据这个接口动态生成一个proxy,在你的service方法前后进行如下事务管理: tx.begin(); try{ target.service(); }catch(Exception e){ tx.rollback(); } tx.commit(); 而动态代理的生成方式,Spring支持两种: 如果target有实现接口,那么使用JDK动态代理 否则使用CGLIB Proxy Creates a JDK proxy when proxy interfaces are given, and a CGLI...

如何不刷新页面上传文件

场景: 一个表单页面:有很多字段,其中有一个是上传图片字段,如何提交这个表单页面。 解决方案 方案1. iframe + hidden field 为了不刷新页面,一般来说要不就是对文件标签使用iframe和单独的form来提交。提交到iframe指定的action处理完成后,将文件上传路径回写到父页面的某个字段。需要在iframe中区分是否已经上传了文件。 如:http://apitest.buy.qq.com/apitools/imgUpload.xhtml?imgName=pic <html> <head> <meta name="description" content="腾讯电商" />...

如何往HttpServletRequest中塞请求参数

需求 假如用户没有传递cooperatorId,那么默认等于uin。 常规做法是在interceptor或者filter或者action中判断如果没有cooperatorId参数,则设置到request的parameterMap中: String cooperatorId = request.getParameter("cooperatorId"); if(cooperatorId == null){ request.getParameterMap().put("cooperatorId",request.getParameter("uin")) ; } 问题 public interface ServletRequest { ...

×