![](/img/trans.png)
[英]SpringBoot application with multiple application.properties running under Tomcat
[英]Tomcat8 Springboot not reading application.properties
我第一次在個人項目上使用java spring,但似乎無法使application.properties中的任何屬性正常工作。 我已經將其簡化為這個測試用例,它似乎沒有做任何事情:
application.properties
logging.level.root=WARN
AppInitalizer.java:
import javax.servlet.Filter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SpringBootApplication
@ConfigurationProperties
public class AppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
private static final Logger log = LoggerFactory.getLogger(AppInitializer.class);
@Override
protected Class<?>[] getRootConfigClasses() {
log.debug("This is a debug message");
log.error("This is an error message");
return new Class[] {};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {};
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] {};
}
}
部署后記錄:
20:08:26.113 [http-nio-8080-exec-32] DEBUG AppInitializer - This is a debug message
20:08:26.114 [http-nio-8080-exec-32] ERROR AppInitializer - This is an error message
這兩個文件被部署到類路徑根目錄下的同一目錄中。
應該只顯示第二條日志消息,但我無法處理第一條日志消息。
相關的ivy.xml配置:
<!-- Spring Framework -->
<dependency org="org.springframework"
name="spring-webmvc" rev="5.0.9.RELEASE"/>
<!-- Logging -->
<dependency org="log4j" name="log4j" rev="1.2.17"/>
編輯:我切換到slf4j我的記錄器,仍然不走運。
Edit2:添加了依賴性
Edit3:我在瀏覽他的日志時發現:
10:28:22.628 [http-nio-8080-exec-114] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
10:28:22.634 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding PropertySource 'servletConfigInitParams' with lowest search precedence
10:28:22.634 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding PropertySource 'servletContextInitParams' with lowest search precedence
10:28:22.636 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding PropertySource 'jndiProperties' with lowest search precedence
10:28:22.636 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding PropertySource 'systemProperties' with lowest search precedence
10:28:22.636 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding PropertySource 'systemEnvironment' with lowest search precedence
10:28:22.636 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [StubPropertySource@36449077 {name='servletConfigInitParams', properties=java.lang.Object@24214aae}, StubPropertySource@1983551321 {name='servletContextInitParams', properties=java.lang.Object@6e836355}, JndiPropertySource@219519027 {name='jndiProperties', properties=org.springframework.jndi.JndiLocatorDelegate@234fc705}, MapPropertySource@23923818 {name='systemProperties', properties={awt.toolkit=sun.awt.X11.XToolkit, java.specification.version=10, file.encoding.pkg=sun.io, sun.cpu.isalist=, sun.jnu.encoding=UTF-8, java.class.path=/usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jar, java.vm.vendor=Oracle Corporation, sun.arch.data.model=64, java.vendor.url=http://java.oracle.com/, catalina.useNaming=true, user.timezone=America/Los_Angeles, os.name=Linux, java.vm.specification.version=10, sun.java.launcher=SUN_STANDARD, user.country=US, sun.boot.library.path=/usr/lib/jvm/java-11-openjdk-amd64/lib, sun.java.command=org.apache.catalina.startup.Bootstrap start, jdk.debug=release, sun.cpu.endian=little, user.home=/var/lib/tomcat8, user.language=en, java.specification.vendor=Oracle Corporation, java.naming.factory.url.pkgs=org.apache.naming, java.version.date=2018-07-17, java.home=/usr/lib/jvm/java-11-openjdk-amd64, ignore.endorsed.dirs=, file.separator=/, java.vm.compressedOopsMode=32-bit, line.separator=
10:28:22.639 [http-nio-8080-exec-114] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Replacing PropertySource 'servletContextInitParams' with 'servletContextInitParams'
10:28:22.639 [http-nio-8080-exec-114] INFO org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue Jan 15 10:28:22 PST 2019]; root of context hierarchy
所有屬性源似乎都沒有從我擁有application.properties的類路徑中加載。
請通過apache.commons.logging使用slf4j.Logger
使用slf4j的LoggerFactory代替org.apache.commons.logging.LogFactory
:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final Logger log = LoggerFactory.getLogger(this.getClass());
或靜態記錄器:
private static final Logger log = LoggerFactory.getLogger(AppInitializer.class);
Spring還引入了新的@PropertySource批注,作為將屬性源添加到環境的便捷機制。 該注釋將與基於Java的配置和@Configuration注釋一起使用:
@Configuration @PropertySource(“ classpath:foo.properties”)公共類AppInitializer {// ...}
注冊新屬性文件的另一種非常有用的方法是使用占位符,以允許您在運行時動態選擇正確的文件。 例如:
@PropertySource({“ classpath:persistence-$ {envTarget:mysql} .properties”})
您可以在XML中使用,可以通過命名空間元素使Spring訪問新的屬性文件:
foo.properties文件應放在/ src / main / resources下,以便在運行時可在類路徑上使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.