복붙노트

[SPRING] Spring 3.1의 기본 프로파일

SPRING

Spring 3.1의 기본 프로파일

내 응용 프로그램에는 @Profile ( "prod") 및 @Profile ( "demo")으로 주석 처리 된 bean이 있습니다. 첫 번째 것은 추측 할 수 있듯이 프로덕션 DB에 연결되는 빈에 사용되며 두 번째 것은 일부 가짜 DB (HashMap 또는 기타)를 사용하는 빈에 주석을 달아 개발 속도를 향상시킵니다.

내가 갖고 싶은 것은 디폴트 프로파일 ( "prod")이다.이 프로파일은 "something-else"에 의해 오버라이드되지 않으면 항상 사용된다.

완벽한 web.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.profiles.active = "prod"를 설정하는 것은 옵션이 아닙니다.

해결법

  1. ==============================

    1.내 경험으로는

    내 경험으로는

    @Profile("default")
    

    bean은 다른 프로파일이 식별되지 않는 경우에만 컨텍스트에 추가됩니다. 다른 프로필을 전달하면 (예 : -Dspring.profiles.active = "demo"이면이 프로필은 무시됩니다.

  2. ==============================

    2.web.xml에 프로덕션 환경을 기본 프로파일로 정의하십시오.

    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
    
  3. ==============================

    3.PROD 프로파일을 제거하고 @Profile ( "! demo")을 사용하는 것도 고려해 볼 수 있습니다.

    PROD 프로파일을 제거하고 @Profile ( "! demo")을 사용하는 것도 고려해 볼 수 있습니다.

  4. ==============================

    4.동일한 문제가 있지만 WebApplicationInitializer를 사용하여 프로그래밍 방식으로 ServletContext (Servlet 3.0 이상)를 구성합니다. 그래서 나는 다음과 같이한다.

    동일한 문제가 있지만 WebApplicationInitializer를 사용하여 프로그래밍 방식으로 ServletContext (Servlet 3.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));
        }
    }
    
  5. ==============================

    5.기본 제작 프로필 설정 정보는 이미 @andih 게시 됨

    기본 제작 프로필 설정 정보는 이미 @andih 게시 됨

    Maven Jetty Plugin의 기본 프로파일을 설정하는 가장 쉬운 방법은 아래의 요소를 Plugin 설정에 포함시키는 것이다 :

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
            <systemProperties>
                <systemProperty>
                    <name>spring.profiles.active</name>
                    <value>demo</value>
                </systemProperty>
            </systemProperties>
        </configuration>
    </plugin>
    
  6. ==============================

    6.Spring은 활성화되어있는 프로파일을 결정할 때 두 개의 분리 된 특성을 제공합니다 :

    Spring은 활성화되어있는 프로파일을 결정할 때 두 개의 분리 된 특성을 제공합니다 :

    spring.profiles.active가 설정되면 그 값에 따라 활성화 된 프로파일이 결정됩니다. 그러나 spring.profiles.active가 설정되어 있지 않으면 Spring은 spring.profiles.default를 찾습니다.

    spring.profiles.active 및 spring.profiles.default가 설정되어 있지 않으면 활성 프로파일이없고 프로파일에없는 것으로 정의 된 bean 만 작성됩니다. 프로파일을 지정하지 않은 모든 bean은 다음에 속합니다. 기본 프로필.

  7. ==============================

    7.web.xml을 필터링 된 리소스로 설정하고 maven 프로필 설정에서이 값을 maven으로 채울 수 있습니다.

    web.xml을 필터링 된 리소스로 설정하고 maven 프로필 설정에서이 값을 maven으로 채울 수 있습니다.

    pom에서 모든 자원을 필터링하십시오 ($ {} 표시가없는 경우이 작업을 수행 할 수 있음)

    <webResources>
        <resource>
            <directory>src/main/webapp</directory>
            <filtering>true</filtering>
        </resource>
    </webResources>
    

    web.xml에 넣는다.

    <context-param>
         <param-name>spring.profiles.active</param-name>
         <param-value>${spring.prfile}</param-value>
    </context-param>
    

    POM에서 메이븐 프로파일 만들기

    <profiles>
        <profile>
            <id>DEFAULT</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profile>prod</spring.profile>
            </properties>
        <profile>
        <profile>
            <id>DEMO</id>
            <properties>
                <spring.profile>demo</spring.profile>
            </properties>
        <profile>
    </profiles>
    

    이제 사용할 수 있습니다.

    mvn jetty:run -P DEMO
    

    또는 maven 명령을 사용하여 간단히 -P DEMO

  8. from https://stackoverflow.com/questions/10041410/default-profile-in-spring-3-1 by cc-by-sa and MIT license