欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dwr3.0纯注解(纯Java Code配置)配置与应用浅析一之零配置文件化

程序员文章站 2024-03-09 16:17:59
//annotation configuration dwr servletprivate void initializedwrservlet(servletcon...
//annotation configuration dwr servletprivate void initializedwrservlet(servletcontext container) 
{dwrservlet dwrservlet = new dwrservlet();
servletregistration.dynamic dynamic = container.addservlet("dwr-invoker", dwrservlet    );
dynamic.setloadonstartup(2);
dynamic.setinitparameter("debug", "true");
dynamic.setinitparameter("pollandcometenabled", "true");
dynamic.setinitparameter("activereverseajaxenabled", "true");
dynamic.setinitparameter("maxwaitafterwrite", "1800000");
dynamic.setinitparameter("disconnectedtime", "60000");
dynamic.setinitparameter("classes", dwrannotationclasssconfiguration());
dynamic.addmapping("/dwr/*");}
// configure dwr annotation classes,comma separatedprivate string dwrannotationclasssconfiguration() {
stringbuffer config = new stringbuffer();
config.append("com.g360.bean.reconsole.opcost.annualopcostrequestcontroller");
config.append(",");config.append("com.g360.bean.security.sessionexpiredparam");
return config.tostring();}

上面我的配置是基于注解版的dwr servlet配置(将web.xml里面的servlet配置转换为了纯java代码配置),当然也包括前端和后端直接交互的类(

com.g360.bean.reconsole.opcost.annualopcostrequestcontroller,com.g360.bean.security.sessionexpiredparam

)也是使用注解配置的(dwr注解配置,包括@remoteproxy,@remotemethod),这对于有些注解配置基础的同学来说可能比较容易看懂,对于初次接触的有什么不懂的可以给我留言哦;

com.g360.bean.security.sessionexpiredparam的注解配置如下所示:

@component("sessionexpiredparam")
@remoteproxy(creator = springcreator.class, scope = scriptscope.application, name = "remote", creatorparams = @param(name = "beanname", value = "sessionexpiredparam"))
 public class sessionexpiredparam{ 
private static final logger logger = loggerfactory.getlogger(sessionexpiredparam.class); 
public boolean issessionexpire (string sessionid) { 
return null == constantcacheservice.getcacheconstant(sessionid); }
 @remotemethod public void setenabledwrupdate(string sessionid) {
  scriptsession scriptsession = webcontextfactory.get().getscriptsession(); 
 string jsessionid = scriptsession.gethttpsessionid(); 
string dwrsessionid = scriptsession.getid(); 
logger.info(string.format("set jsessionid = [%s],dwrsession = [%s] push enabled",jsessionid,dwrsessionid)); 
constantcacheservice.putcacheconstant(constants.push_id+jsessionid, dwrsessionid); 
} }

这就是相当于dwr.xml配置文件中如下所示:

<!doctype dwr public "-//getahead limited//dtd direct web remoting 3.0//en" "http://getahead.org/dwr/dwr30.dtd"><dwr> 
<allow> <create creator="new" javascript="remote" scope="application">  
 <param name="class" value="com.g360.bean.security.sessionexpiredparam"/> 
 </create> </allow></dwr>

对于com.g360.bean.reconsole.opcost.annualopcostrequestcontroller来说道理一样,这里就不多做说明了;

对于使用dwrservlet来说要使用纯注解配置(不使用dwr.xml)一定要注意在初始化参数时一定要配置 classes这个参数,而且这个参数是这个注解类的全路径名,有多个的话就使用逗号分隔,这个是特别重要的哦,因为dwr3.0好像还不支持包扫描,不知道是需要什么配置才能实现还是怎么的,如果有哪位仁兄正好看见,也正好知道的话请留言麻烦告诉一下,感激不尽啊!欢迎大家有什么不明白的地方或者有什么不对的地方,请随时留言;