Shi*_*nha 7 java spring tomcat spring-mvc spring-boot
我有以下目录结构/配置文件:
src/main/resource/config:
application.yml
application-dev.yml
application-sit.yml
Run Code Online (Sandbox Code Playgroud)
请注意根据" Bootiful Configuration " https://spring.io/blog/2015/01/13/configuring-it-all-out-or-12-factor-app-style-configuration-with-spring:
Spring Boot将默认读取src/main/resources/application.properties中的属性.如果配置文件处于活动状态,它还将根据配置文件名称自动读取配置文件,例如src/main/resources/application-foo.properties,其中foo是当前配置文件.如果Snake YML库在类路径上,那么它也会自动加载YML文件.
如果我--spring.profiles.active=dev在eclipse运行配置中设置为程序arg并且使用它作为我的主要方法,那么蛇YML jar在类路径中,永远按预期工作:
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
// Check if the selected profile has been set as argument.
// if not the development profile will be added
addDefaultProfile(app, source);
app.run(args);
}
/**
* Set a default profile if it has not been set
*/
private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) {
if (!source.containsProperty("spring.profiles.active")) {
app.setAdditionalProfiles(Constants.SPRING_PROFILE_DEVELOPMENT);
}
}
Run Code Online (Sandbox Code Playgroud)
(请注意,上面的主要方法参考来自我的代码中使用的以下类:https://github.com/jarias/generator-jhipster-ember/blob/master/app/templates/src/main/java/package/ _Application.java)
一切都按照预期的方式运行spring.profile.active = dev.这意味着: application.yml(默认加载)和application-dev.yml(活动配置文件)属性文件都被加载并排除 application-sit.yml,因为sit不是活动配置文件.
这个嵌入式容器非常适合开发测试.但是,我希望通过生成战争并将其部署到独立的Tomcat8服务器来将其发布到生产环境中.
为此,我创建了WebApplicationInitializer的实现,Tomcat8服务器需要该实现在独立服务器上自动检测,引导和启动spring应用程序.
@Configuration
public class WebAppInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
部署战争后,我收到以下错误,我尝试启动独立服务器并收到以下错误:
引起:org.springframework.beans.factory.enter code hereBeanCreationException:无法自动装配字段:private java.lang.String com.titlefeed.config.db.DbConfigJPA.databaseUrl; 嵌套异常是java.lang.IllegalArgumentException:无法解析字符串值"$ {spring.data.postgres.uri}"中的占位符spring.data.postgres.uri'
这意味着Tomcat服务器/春心不是加载应用程序,dev.yml,因为它包含的属性:spring.data.postgres.uri
所以我尝试了以下两种解决方案
-Dspring.profiles.active=dev到tomcat/bin/catalina.sh中的JAVA_OPTSspring.profiles.active=dev到tomcat/conf/catalina.properties他们都没有工作.如何让独立的tomcat服务器加载与spring.profiles.active属性关联的yml文件.
它适用于从eclipse启动的嵌入式springboot服务器,但不适用于独立服务器?
EDIT1:M.Deinum - 在下面实现了您建议的解决方案,但仍然出现以下错误:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.postgres.uri' in string value "${spring.data.postgres.uri}
似乎-Dspring.profiles.active = dev没有得到设置.
@Configuration
public class WebAppInit extends SpringBootServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext(
ServletContext servletContext) {
log.info("Properly INITALIZE spring CONTEXT");
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
return super.createRootApplicationContext(servletContext);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2 ACV: - 在启动脚本中添加"--spring.profiles.active = dev"作为JAVA_OPTS变量的一部分:tomcat/bin/catalina.sh不是一个可行的选项
例如:
JAVA_OPTS="$JAVA_OPTS --spring.profiles.active=dev ...etc
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
无法识别的选项: - spring.profiles.active = dev错误:无法创建Java虚拟机."
编辑3: 修改application.yml以包含以下属性
spring:
profiles:
active: dev
Run Code Online (Sandbox Code Playgroud)
重新部署战争.去了爆炸的tomcat目录位置以确保属性存在webapps/feedserver/WEB-INF/classes/config/application.yml
问题仍然存在.
编辑4:在tomcat展开的webdir下添加了application.properties:webapps/feedserver/WEB-INF/classes/application.properties:
spring.profiles.active=dev
spring.data.postgres.uri=jdbc:postgresql://localhost:5432/feedserver
Run Code Online (Sandbox Code Playgroud)
重新启动tomcat并且问题仍然存在.
它似乎没有拿起application.properties或application.yml
编辑5使用推荐的方法启动外部容器的spring boot服务器:
@Configuration
public class WebAppInit extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑6:
我将-Dspring.profiles.active = dev添加到了start命令args:
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java -Djava.util.logging.config.file=/Users/shivamsinha/Desktop/Programming/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dlog4j.rootLevel=ERROR -Dlog4j.rootAppender=console -DENV=dev -Dlog4j.configuration=/WEB-INF/classes/properties/log4j.properties -DTOMCAT_DIR=WEB-INF/classes/ -Djava.endorsed.dirs=/Users/shivamsinha/Desktop/Programming/tomcat/endorsed -classpath /Users/shivamsinha/Desktop/Programming/tomcat/bin/bootstrap.jar:/Users/shivamsinha/Desktop/Programming/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/Users/shivamsinha/Desktop/Programming/tomcat -Dcatalina.home=/Users/shivamsinha/Desktop/Programming/tomcat -Djava.io.tmpdir=/Users/shivamsinha/Desktop/Programming/tomcat/temp org.apache.catalina.startup.Bootstrap -Dspring.profiles.active=dev start
Run Code Online (Sandbox Code Playgroud)
但是我在日志中得到以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.titlefeed.config.db.DbConfigJPA.databaseUrl; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.postgres.uri' in string value "${spring.data.postgres.uri}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 68 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.postgres.uri' in string value "${spring.data.postgres.uri}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 70 more
02-Sep-2015 03:15:40.472 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive /Users/shivamsinha/Desktop/Programming/tomcat/webapps/feedserver-1.0.0.war
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/feedserver-1.0.0]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:728)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:917)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1701)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)
信用:@M。Deinum
有两种方法可以将spring概要文件args传递到Tomcat 8中。
1.将其设置为环境变量
Tomcat允许您在启动过程中设置CATALINA_HOME/setenv.sh或CATALINA_BASE/setenv.sh调用的环境配置。
setenv.sh:
export SPRING_PROFILES_ACTIVE=dev
Run Code Online (Sandbox Code Playgroud)
您可能还想创建一个src/main/resources/banner.txt包含以下内容的代码:
active profiles :: ${spring.profiles.active}
Run Code Online (Sandbox Code Playgroud)
它在您的IDE中不起作用(它会从jar / war的MANIFEST.MF文件中读取,如果您正常编译则不会获得该文件),但是在您关心的环境中它非常方便-一切都取决于您当地环境!
2.将其添加到执行类之前的启动脚本/命令中
我修改了CATALINA_HOME/catalina.sh添加一个声明的变量:
SPRING_PROFILE="dev"
并在所有相关的执行中将其添加到脚本中:
eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
-Dspring.profiles.active="\"$SPRING_PROFILE\"" \
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
Run Code Online (Sandbox Code Playgroud)
显然,这不是推荐的方法。但这有效!如果您确实有确切的复制步骤,建议的方法可以随时发布答案,如果可行,我会接受。
| 归档时间: |
|
| 查看次数: |
9603 次 |
| 最近记录: |