In developing javaweb applications, we sometimes need the path of the configuration file to be set in classpath in order to achieve configuration separation.
However, when using IntelliJ idea, it is found that the classpath of the discovery settings does not take effect.
Tried the method:
1. Add in module in IntelliJ idea Project
2. Set classpath in Windows--My computer--attributes--Advanced system variables
has no effect.
For a long time, I stumbled upon the original Tomcat version that was specified when the Tomcat server was created, so when the classpath loaded at IntelliJ idea started, it was loading local tomcat Classpath , and the local classpath is set in Catalina.bat. So we need to change the local catalina.bat or catania.sh file that IntelliJ idea uses.
Prior to modification--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.
After modification--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: \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.
Observe Catalina.bat, found that Catalina settings Classpath will first find SetEnv.bat (belong to user-defined properties file) and then find Setclasspath.bat (belongs to the Java Environment Property file), We add the attributes we defined directly to the back of the two (bold red).
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 called" %catalina_home%\bin\setenv.bat ": Setenvdone rem Get standard Java environment variables if exist"%catalina_home%\bin\se Tclasspath.bat "Goto Oksetclasspath Echo Cannot find"%catalina_home%\bin\setclasspath.bat "The echo this file are needed to Ru n This program goto End:oksetclasspath called "%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 don't want to introduce random REM quotes into the CLASSPATH if "%classpath%" = "goto Emptyclasspath set" classpath=%classpath%; ": Emptyclasspath Set" Classpath=%classpa Th%%catalina_home%\bin\bootstrap.jar "REM Settings custom properties fileSet "classpath=%classpath%;d: \pay_conf\"If not "%catalina_tmpdir%" = "" goto gottmpdir Set "Catalina_tmpdir=%catalina_base%\temp": Gottmpdir
This allows the configuration file to be read directly in the system via Class.getclassloader (). getResourceAsStream (Resource).
When you use spring to load a configuration file, you need to be aware that we typically use Propertyplaceholderconfigurer to load the configuration file, which needs to be set up to guide spring to the system's environment variables to find a configuration file with the same name.
<property name= "Systempropertiesmodename" value= "System_properties_mode_override"/>
The specific configuration is as follows:
<bean id= "Propertyconfigurer"
class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
<property name=" Ignoreresourcenotfound "value=" true "/> <property name=" systempropertiesmodename "value="
SYSTEM_ Properties_mode_override "/>
<property name=" Locations ">
<list>
<value> classpath:system.properties</value>
<value>classpath:redis.properties</value>
</ list>
</property>
</bean>
Bingo!