복붙노트

[SPRING] Spring bean 파일에서 maven profile id 값을 사용하려면 어떻게해야합니까?

SPRING

Spring bean 파일에서 maven profile id 값을 사용하려면 어떻게해야합니까?

mvn -P dev

프로필 dev를 사용하여 프로젝트를 빌드한다면, 스프링 빈에서 dev.properties를 사용하고 싶습니다. 가능한가? 그렇다면 프로필 이름을 어떻게 얻을 수 있습니까?

<bean id="xyz" class="abc.xyz">
    <property name="propertyFile" value="${maven_profile_id}.properties" />
</bean>

미리 감사드립니다.

해결법

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

    1.Maven 프로파일을 사용하여 'profile'속성을 빌드에 추가 할 수 있습니다.

    Maven 프로파일을 사용하여 'profile'속성을 빌드에 추가 할 수 있습니다.

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile>dev</profile>
            </properties>
        </profile>
    </profiles>
    

    그런 다음 시스템 속성을 사용하여 응용 프로그램에 값을 전달하십시오. 여기에 surefire 예제가 있습니다.

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <systemPropertyVariables>
                <profile>${profile}</profile>
            </systemPropertyVariables>
        </configuration>
    </plugin>
    

    마지막으로 이것은 응용 프로그램에서 참조 할 수 있습니다.

    <bean id="xyz" class="abc.xyz">
        <property name="propertyFile" value="${profile}.properties" />
    </bean>
    

    또는 Spring 3.1 이상을 사용하는 경우 XML 프로필 기능이 사용자의 요구를 충족시킬 수 있습니다 (잔인 할 수도 있음).

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

    2.빌드시에 사용하고있는 프로파일을 지정하는 Maven의 자원 필터링을 사용하여 채울 등록 정보 파일을 작성하십시오.

    빌드시에 사용하고있는 프로파일을 지정하는 Maven의 자원 필터링을 사용하여 채울 등록 정보 파일을 작성하십시오.

    build.properties

    activatedProfile=${profileId}
    

    pom.xml (전체 디렉토리를 필터링 할 필요없이 필요에 따라 사용자 정의)

     <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        <resources>
     </build>   
    

    각기 다른 프로필 아래에 profileId (또는 원하는 이름) 속성을 추가하십시오.

     <profile>
         <id>dev</id>
         <properties>
            <profileId>dev</profileId>
         </properties>
     </profile>
     <profile>
         <id>qa</id>
         <properties>
            <profileId>qa</profileId>
         </properties>
     </profile>
    

    그런 다음 $ {activatedProfile} .properties를 빈에 대한 값으로 사용할 수 있습니다

    <bean id="xyz" class="abc.xyz">
        <property name="propertyFile" value="${activatedProfile}.properties" />
    </bean>
    
  3. from https://stackoverflow.com/questions/12581151/how-can-i-use-maven-profile-id-value-in-spring-bean-files by cc-by-sa and MIT license