Intellij IDEA Tomcat设置CLASSPATH 不生效 配置分离实现

在开发JavaWeb应用时,为了实现配置分离,我们有时候需要在CLASSPATH中设置的配置文件的路径。

但是在使用Intellij idea时发现发现设置的CLASSPATH不生效。

尝试过的方法:

1.在intellij idea 项目中的module中添加

2.在Windows --我的电脑--属性--高级系统变量里设置CLASSPATH

都没有效果!

找了许久的原因,偶然发现原来创建Tomcat server时 指定的是 自己的tomcat版本;所以在intellij idea启动时加载的CLASSPATH时,是加载本地的tomcat中的CLASSPATH;而本地的CLASSPATH又是catalina.bat中设置的。所以我们需要更改intellij IDEA使用的本地的Catalina.bat 或者Catania.sh 文件

修改前--intellij idea output:

:\apache\apache-tomcat-7.0.84\bin\catalina.bat run
[2018-03-30 06:58:40,011] Artifact boss:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE:   "C:\Users\mingj\.IntelliJIdea2017.3\system\tomcat\Unnamed_pay_2"
Using CATALINA_HOME:   "D:\apache\apache-tomcat-7.0.84"
Using CATALINA_TMPDIR: "D:\apache\apache-tomcat-7.0.84\temp"
Using JRE_HOME:        "D:\Program Files\Java\jdk1.7.0_80"
Using CLASSPATH:       "D:\apache\apache-tomcat-7.0.84\bin\bootstrap.jar;D:\apache\apache-tomcat-7.0.84\bin\tomcat-juli.jar"
Connected to the target VM, address: '127.0.0.1:57543', transport: 'socket'
2018-03-30 18:58:41 JRebel: Contacting myJRebel server ..

修改后--intellij idea output:

D:\apache\apache-tomcat-7.0.84\bin\catalina.bat run
[2018-03-30 06:58:40,011] Artifact boss:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE:   "C:\Users\mingj\.IntelliJIdea2017.3\system\tomcat\Unnamed_pay_2"
Using CATALINA_HOME:   "D:\apache\apache-tomcat-7.0.84"
Using CATALINA_TMPDIR: "D:\apache\apache-tomcat-7.0.84\temp"
Using JRE_HOME:        "D:\Program Files\Java\jdk1.7.0_80"
Using CLASSPATH:       "D:\apache\apache-tomcat-7.0.84\bin\bootstrap.jar ;d:\pay_conf/;D:\apache\apache-tomcat-7.0.84\bin\tomcat-juli.jar"
Connected to the target VM, address: '127.0.0.1:57543', transport: 'socket'
2018-03-30 18:58:41 JRebel: Contacting myJRebel server ..

观察catalina.bat,发现catalina设置CLASSPATH时会先找setenv.bat(属于用户自定义属性文件) 然后再找setclasspath.bat(属于java environment属性文件),我们直接在这两者的后面(加粗标红处)添加我们定义的属性可以了。

set CLASSPATH=


rem Get standard environment variables
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone


rem Get standard Java environment variables
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
echo This file is needed to run this program
goto end
:okSetclasspath
call "%CATALINA_HOME%\bin\setclasspath.bat" %1
if errorlevel 1 goto end


rem Add on extra jar file to CLASSPATH
rem Note that there are no quotes as we do not want to introduce random
rem quotes into the CLASSPATH
if "%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"

rem 设置自定义属性文件
set "CLASSPATH=%CLASSPATH% ;d:\pay_conf\ "


if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir

这样,如果在系统中有通过class.getClassLoader().getResourceAsStream(resource)直接就能读取到配置文件了。

当使用spring加载配置文件时需要注意的是:我们通常使用PropertyPlaceholderConfigurer加载配置文件,这个地方需要设置 来指引spring 去系统的环境变量里面寻找相同名称的配置文件,才能找得到。

具体配置如下:

	
		
		
		
			
				classpath:system.properties
				classpath:redis.properties
			
		
	

bingo!

你可能感兴趣的:(apache,tomcat,spring,服务器,intellij,idea,Intellij,IDEA)