Java中Web程序修改配置文件不重啟服務器的方法

見:http://blog.sina.com.cn/s/blog_69398ed9010191jg.html

另:http://ekisstherain.iteye.com/blog/1701463



jrebel 、JavaRebel是什么,見另一博客:jrebel/JavaRebel



開發環境

1.??JDK

2.??MyEclipse

3.??Tomcat

4.??Struts2

5.??JavaRebel/JRebel

?

第一步:修改struts.properties來實現熱加載Struts2的配置文件


(注:開發環境是在Struts2下)

在src目錄下新建一個文件struts.properties,打開編輯,加入以下語句

#Whether Struts is in development mode or not

struts.devMode=true

#Whether the localization messages should automatically be reloaded

struts.i18n.reload=true

#Whether to reload the XML configuration or not

struts.configuration.xml.reload=true

上面的語句分別為是否為struts開發模式、是否國際化信息自動加載、是否加載xml配置(true,false),修改后重啟服務器后,就能體現效果。在我們修改Struts2的配置信息的時候就不需再重啟服務器了,而且Struts2的配置還包括include標簽(防止配置文件膨脹),例如下面的配置:

?

????"struts-back.xml">

????"struts-custom.xml">

上面的struts.properties會對每個配置文件都產生作用。

對于上面的測試,我實際上發現:上面的配置在純Struts2開發中,是可以實現對配置文件和國際化文件熱加載功能的,但我在SSH開發中并不能實現功能,而只能實現對國際化的熱加載,不能對配置文件的熱加載。

?

第二步:解決類的熱加載

?

方法一:修改MyEclipse 中的Tomcat配置,加入JRebel的應用

既然要使用JRebel,那就要我們下載JRebel,到JRebel的官網上面下載最新的JRebel包,需要說明的是JRebel并非免費的產品,提供30天的試用期限,不過網上可以搜索到破解版的喲。

下載后解壓,就可以看到jrebel.jar了。

使用配置?

Jrebel的使用一般都依賴于服務器,就那tomcat來說,主要是配置JDK的Optional Java VM arguments(打開Myeclipse中的windows->Preferences->MyEclipse …->Servers->Tomcat->

選擇配置的Tomcat,單擊,展開JDK選項),加入下面的語句

-noverify -javaagent:D:\Jars\jrebel-2.1a\jrebel.jar

說明一下,“-noverify -javaagent:”是固定的,后面加上的是jrebel.jar在你電腦上面的物理路徑,請修改為你的電腦配置,不然不會成功。

這一步是為了解決Java類文件熱加載的問題,實際上,有一個更簡單的方法吧,并不需要使用JRebel包,而是我們進行適當的配置

?

方法二:

自動加載修改后的項目不需重啟服務器(只對Java文件,對配置文件不可以)

在WebRoot下的META-INF文件夾中新建一個名為context.xml文件,在里面寫

"true">

注意大小寫

方法二的思想是源自張孝祥老師的講解,不需要修改tomcat的配置,減少tomcat的啟動時間,最重要的是,能夠完成一樣的作用——對Java類進行熱加載,但是有的時候,特別是在SSH開發中,會出現異常,建議不要使用這種方法,而是使用第一種方法使用JRebel。

?

這兩步下來, 可以在新加入類以及Struts配置修改后, 完全無需重啟或者重新發布即可立即測試! 在JAR包暴多的情況下, 可以讓我們不再等待10到20秒了。

?

第三步:測試是否有效


測試類的熱加載(具體的測試請大家完成,我僅貼出我的測試結果)

我在一個已知的登錄系統中,加入一個自己留的后臺用戶,不重啟服務器登錄

if("TestJRebel".equals(operator.getOperatorName()))

???????{

???????????System.out.println("通過自己留后臺用戶TestJRebel登錄,大家不要這樣哦。");

???????????return?true;

???????}




另附:jrebel熱部署配置


1. 配置tomcat服務器:修改tomcat的部署配置為:從不自動發布,禁用Web模塊的自動從新裝入為:禁用


2. 在tomcat運行配置上添加vm參數:-noverify -javaagent:D:\RUNTIME\jrebel\jrebel.jar -Dmyproject.root=D:/project/operamasks/workspaces/xxxx -Drebel.disable_update=true
?? 其中:-javaagent的值表示jrebel的jar包路徑
?? -Dmyproject.root的值表示當前的需要熱部署的工程路徑


3. 在工程的src源文件夾下添加名稱為:rebel.xml的配置文件,內容格式如下:

?

Xml代碼??收藏代碼
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <application?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns="http://www.zeroturnaround.com"?xsi:schemaLocation="http://www.zeroturnaround.com?http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">??
  3. ????<classpath>??
  4. ????????<dir?name="${myproject.root}/bin">??
  5. ????????</dir>??
  6. ????????<dir?name="${myproject.root}/web/WebContent/WEB-INF/classes">??
  7. ????????</dir>??
  8. ????</classpath>??
  9. ????<web>??
  10. ????????<link?target="/">??
  11. ????????????<dir?name="${myproject.root}/web/WebContent">??
  12. ????????????</dir>??
  13. ????????</link>??
  14. ????</web>??
  15. </application>??

?

?

其中:myproject.root就是tomcat vm參數中指定的工程路徑,你也可以使用絕對路徑
這個文件定義的就是jrebel要監控的具體文件夾,即class文件和資源文件(比如:jsp等)



4.最后,啟動tomcat服務器的成功提示:

?

Fri Oct 19 10:34:29 CST 2012 com.zeroturnaround.javarebel.hD#new V( false )
Fri Oct 19 10:34:29 CST 2012 com.zeroturnaround.javarebel.bH#public boolean a(byte abyte0[])

#############################################################

?JRebel 3.0-M1 (200910151623)
?(c) Copyright ZeroTurnaround, Ltd, 2007-2009. All rights reserved.

?A rough estimate: Over the last 5 days JRebel?
?prevented the need for at least 36 redeploys/restarts.
?Using industry standard build and redeploy times,?
?JRebel saved you between 1 and 2 hours.

?This product is licensed to? Java Hack Organization
?for unlimited number of developer seats on site.

#############################################################


JRebel: A newer version '5.0.1' is available for download?
JRebel: from?http://www.zeroturnaround.com/download.

2012-10-19 10:34:29 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\RUNTIME\java\jdk1.6.0_11\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/RUNTIME/java/jdk1.6.0_11/bin/../jre/bin/client;D:/RUNTIME/java/jdk1.6.0_11/bin/../jre/bin;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;D:\RUNTIME\java\jdk1.6.0_11\bin;D:\RUNTIME\java\jdk1.6.0_11\jre\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;D:\RUNTIME\MySQL\MySQL Server 7.0\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\MacType;C:\PROGRA~1\DISKEE~1\DISKEE~1\;d:\Program Files (x86)\DBank\ClickUp;C:\Program Files (x86)\DBank\ClickUp
2012-10-19 10:34:29 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:XXX' did not find a matching property.
2012-10-19 10:34:29 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2012-10-19 10:34:29 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 324 ms
2012-10-19 10:34:29 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2012-10-19 10:34:29 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.35
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\bin' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent' will be monitored for changes.

=============================== [JRebel Spring Framework Plugin] ===============================
Plugins are contributed by third party and can cause compatibility problems.
If you have any troubles set -Drebel.spring_plugin=false to disable it.
------------------------------------------------------------------------------------------
Description: Supports adding new beans and adding new bean dependencies using
annotations or XML. Singletons will be reconfigured after the change. It also
supports adding or changing Spring MVC controllers or handlers.
=============================== [/JRebel Spring Framework Plugin] ==============================


=============================== [JRebel AspectJ Plugin DISABLED] ==============================
You can enable AspectJ Plugin by setting -Drebel.aspectj_plugin=true.
------------------------------------------------------------------------------------------
Description: Allows the AspectJ load-time weaving (javaagent launched by aj5 or
-javaagent:aspectjweaver.jar) to be used with JavaRebel. Note that AspectJ
weaver will still show "java.lang.Exception: AspectJ5 does not weave hotswapped
class" in the beginning, but this can be safely ignored.
=============================== [/JRebel AspectJ Plugin DISABLED] =============================

2012-10-19 10:34:30 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-dao.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-service.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\bin\applicationContext-service-log.xml'.
...

?

?

到此為止,你成功配置jrebel了。


值得注意的是:
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\bin' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent' will be monitored for changes.

JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-dao.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-service.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\bin\applicationContext-service-log.xml'.


表示在運行期間jrebel會監控這些位置的class文件和資源文件的變化情況!






本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/450935.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/450935.shtml
英文地址,請注明出處:http://en.pswp.cn/news/450935.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ffmpeg-0.6.3 移植到 windows 開源代碼

ffmpeg-0.6.3開源編碼解碼庫&#xff0c;從linux下移植到windows vs2005&#xff0c;全部開源。 需要 Intel C Compile 和 開源的SDL庫支持&#xff0c;由于 Intel C Compile支持C99語法&#xff0c;所以源代碼改動很小很小。 主要的修改 1&#xff1a;添加了linux中有而wind…

一起嘮嘮分布式鎖

&#xff08;1&#xff09;分布式鎖和分布式事務的區別 1.分布式鎖是在集群環境下&#xff0c;用來控制不同機器對全局共享資源的訪問。 2.分布式事務是在集群環境下&#xff0c;用來保證全局事務的一致性&#xff0c;保證多個數據庫的數據整體上能正確的從一個一致性狀態轉到…

luogu2577/bzoj1899 午餐 (貪心+dp)

首先&#xff0c;應該盡量讓吃飯慢的排在前面&#xff0c;先按這個排個序 然后再來決定每個人到底去哪邊 設f[i][j]是做到了第i個人&#xff0c;然后1號窗口目前的總排隊時間是j&#xff0c;目前的最大總時間 有這個i和j的話&#xff0c;再預處理出前i個人的排隊總時間sum[i]&a…

wpf中xps文檔合并功能實現

原文:wpf中xps文檔合并功能實現跟著上一篇的xps文檔套打的文章&#xff0c;近期一直在研究xps打印技術&#xff0c;其中用戶提到了一個需求&#xff0c;要求能夠多頁面進行打印&#xff0c;我的想法是&#xff0c;先生成xps文件&#xff0c;然后將文件讀取出來以后&#xff0c;…

DCT(離散余弦變換(DiscreteCosineTransform))

離散余弦變換&#xff08;Discrete Cosine Transform&#xff0c;簡稱DCT變換&#xff09;是一種與傅立葉變換緊密相關的數學運算。在傅立葉級數展開式中&#xff0c;如果被展開的函數是實偶函數&#xff0c;那么其傅立葉級數中只包含余弦項&#xff0c;再將其離散化可導出余弦…

從源碼看ConcurrentHashMap

簡介 ConcurrentHashMap是線程安全的HashMap實現&#xff0c;這里主要研究JDK8后的ConcurrentHashMap&#xff0c;下面是ConcurrentHashMap的簡單結構&#xff1a; ConcurrentHashMap基于HashMap的基本邏輯&#xff0c;通過CAS synchronized 來保證并發安全性。ConcurrentHas…

代碼重構的方法

見&#xff1a;http://blog.csdn.net/u011889786/article/details/51865344 見&#xff1a;http://blog.csdn.net/weiky626/article/details/1602691 一.提取子函數 說白了就是一個大函數里&#xff0c;可以根據不同功能分成幾個小函數&#xff0c;因為說不定&#xff0c;其他…

android 去掉標題欄、狀態欄、橫屏

// 去掉標題欄supportRequestWindowFeature(Window.FEATURE_NO_TITLE);// 全屏、隱藏狀態欄getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 橫屏setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION…

Spring Boot 整合Mybatis (一)

2019獨角獸企業重金招聘Python工程師標準>>> 新建spring-boot項目&#xff0c;相關依賴 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><de…

x264 的 cache詳解

在這里和下一級別的分析中有必要先講一下這個h->mb.cache&#xff08;沒法講&#xff0c;就是cache!&#xff09;。 x264_macroblock_cache_load將參考幀中某位置的&#xff08;重建后&#xff09;數據保存進cache&#xff0c;供參考和反復使用。 x264_macroblock_cache_s…

同步/異步阻塞/非阻塞

平時開發中經常會聽大家說到什么同步阻塞、異步非阻塞等等名詞&#xff0c;這里我談下自己對這兩個名詞的理解&#xff0c;僅僅是個人觀點&#xff0c;并不一定正確。 1.阻塞/非阻塞 我認為判定阻塞還是非阻塞&#xff0c;取決于線程所做的操作是否需要將線程掛起等待。 舉個…

Repeater的使用

1.頁面代碼 如果要分頁&#xff0c;那么頁面開頭必須寫&#xff08;<% Register Src"~/Controls/Page.ascx" TagName"Page" TagPrefix"uc1" %>&#xff09; 并且分頁&#xff0c;頁腳<uc1:Page ID"Page2" runat"server&…

springboot 整合 mongodb實現 批量更新數據

現需求&#xff1a;需要批量將1000個數據先查詢在更新到mongodb&#xff08;如果查詢不到數據&#xff0c;則添加數據&#xff09; 1&#xff1a;工具類BathUpdateOptions 1 import org.springframework.data.mongodb.core.query.Query;2 import org.springframework.data.mong…

【開題報告】基于微信小程序的校園資訊平臺的設計與實現

1.選題背景與意義 隨著移動互聯網的快速發展&#xff0c;微信成為了人們日常生活中不可或缺的工具之一。在校園生活中&#xff0c;學生們對于校園資訊的獲取和交流需求也越來越高。然而&#xff0c;傳統的校園資訊發布方式存在信息不及時、傳播范圍有限等問題&#xff0c;無法…

三種Cache寫入方式原理簡介

三種Cache寫入方式原理簡介 在386以上檔次的微機中&#xff0c;為了提高系統效率&#xff0c;普遍采用Cache&#xff08;高速緩沖存儲器&#xff09;&#xff0c;現在的系統甚至可以擁有多級Cache。Cache實際上是位于CPU與DRAM主存儲器之間少量超高速的靜態存儲器&#xff08;S…

Minor GC和Full GC

我們在日常開發中可能經常會聽大家談論GC&#xff0c;但是其實很多人對GC的種類其實并不是很了解&#xff0c;接下來我們簡單介紹下Minor GC和Full GC及他們的區別。 MinorGC&#xff1a; 也可以叫作新生代GC&#xff0c;指的是發生在新生代的垃圾收集動作。因為新生代中對象大…

linux安裝軟件的幾種方法

見&#xff1a;http://blog.csdn.net/u010509774/article/details/50593231 一、rpm包安裝方式步驟&#xff1a; 1、找到相應的軟件包&#xff0c;比如soft.version.rpm&#xff0c;下載到本機某個目錄&#xff1b; 2、打開一個終端&#xff0c;su -成root用戶&#xff1b; …

Android NDK MediaCodec在ijkplayer中的實踐

https://www.jianshu.com/p/41d3147a5e07 從API 21&#xff08;Android 5.0&#xff09;開始Android提供C層的NDK MediaCodec的接口。 Java MediaCodec是對NDK MediaCodec的封裝&#xff0c;ijkplayer硬解通路一直使用的是Java MediaCodec接Surface的方式。 本文的主要內容是&a…

leetcode-49-字母異位詞分組(神奇的哈希)

題目描述&#xff1a; 給定一個字符串數組&#xff0c;將字母異位詞組合在一起。字母異位詞指字母相同&#xff0c;但排列不同的字符串。 示例: 輸入: ["eat", "tea", "tan", "ate", "nat", "bat"], 輸出: [[&quo…

【精心總結】java內存模型和多線程必會知識

內存模型 &#xff08;1&#xff09;java內存模型到底是個啥子東西&#xff1f; java內存模型是java虛擬機規范定義的一種特定模型&#xff0c;用以屏蔽不同硬件和操作系統的內存訪問差異&#xff0c;讓java在不同平臺中能達到一致的內存訪問效果&#xff0c;是在特定的協議下…