当前位置: 首页 > 知识库问答 >
问题:

Spring 3.1中的默认配置文件

陶修洁
2023-03-14

在我的应用程序中,我有用@Profile("prod")@Profile("demo")注释的bean。第一个,你可以猜到:),用于连接到生产数据库的bean,第二个注释使用一些假数据库(HashMap或其他任何东西)的bean,以使开发更快。

我想要的是默认配置文件("prod"),如果它不被"某物"覆盖,它将始终使用。

在我的网站上有一个完美的选择。xml

<context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>prod</param-value>
</context-param>

然后用-Dspring.profiles.active="demo"覆盖它,这样我就可以:

mvn jetty:run -Dspring.profiles.active="demo". 

但可悲的是,这是行不通的。你知道我怎么能做到吗?设置-Dspring。简介。active=“prod”在我的所有环境中都不是一个选项。

共有3个答案

梁昊天
2023-03-14

我也有同样的问题,但我使用WebApplicationInitializer以编程方式配置ServletContext(Servlet3.0)。因此,我做了以下工作:

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        // Create the 'root' Spring application context
        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        // Default active profiles can be overridden by the environment variable 'SPRING_PROFILES_ACTIVE'
        rootContext.getEnvironment().setDefaultProfiles("prod");
        rootContext.register(AppConfig.class);

        // Manage the lifecycle of the root application context
        sc.addListener(new ContextLoaderListener(rootContext));
    }
}
龙才俊
2023-03-14

我的经验是使用

@Profile("default")

只有当没有其他配置文件被标识时,bean才会被添加到上下文中。如果传入不同的配置文件,例如-Dspring.profiles.active="demo",则忽略此配置文件。

时恩
2023-03-14

将生产环境定义为web中的默认配置文件。xml

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>prod</param-value>
</context-param>

如果您想使用不同的配置文件,请将其作为系统属性传递

mvn -Dspring.profiles.active="demo" jetty:run
 类似资料:
  • 在我的应用程序中,我有用和注释的bean。第一个,你可以猜到:),用于连接到生产数据库的bean,第二个注释使用一些假数据库(或其他任何东西)的bean,以使开发更快。 我想要的是默认配置文件(),如果它不被"某物"覆盖,它将始终使用。 在我的: 然后用覆盖它,这样我就可以: 但可悲的是,这是行不通的。你知道我怎么能做到吗?设置在我的所有环境中都不是一个选项。

  • 主要内容:默认配置文件,示例通常情况下,Spring Boot 在启动时会将 resources 目录下的 application.properties 或 apllication.yml 作为其默认配置文件,我们可以在该配置文件中对项目进行配置,但这并不意味着 Spring Boot 项目中只能存在一个 application.properties 或 application.yml。 默认配置文件 Spring Boot

  • 与Jquery Mobile自动初始化共同协作 Working with Jquery Mobile's Auto-initialization 不像其他的Jq项目,比如jq和jq ui,Jquery Mobile会在加载到增强特性时马上应用它(远早于document.ready事件发生时)。这些特性会基于Jquery Mobile的默认配置应用,是针对默认的情形设计的,他可能符合你的需求,也可能

  • 上一小节讲到,DispatcherServlet维护了一个列表,其中保存了其所依赖的所有bean的默认实现。这个列表保存在包org.springframework.web.servlet下的DispatcherServlet.properties文件中。 这些特殊的bean都有一些基本的默认行为。或早或晚,你可能需要对它们提供的一些默认配置进行定制。比如说,通常你需要配置InternalResou

  • 如果未设置,我希望默认活动配置文件为。 Spring-boot版本=1.3.5。发行版

  • 问题内容: 我正在通过映射配置文件配置Hibernate。 如何设置为可为空,默认为null? 问题答案: