2012-02-02 53 views
3

我有这个奇怪的行为,使用Maven <filter>标记和Spring配置。我的理解是Spring配置是Maven的普通XML文件,但我遇到<context:component-scan base-package="com.xyz"/>标记的问题。测试XML文件如下Spring配置和Maven配置文件的奇怪行为

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd" 
    default-autowire="byName"> 

    <!-- Import the DataSource configurations --> 
    <import resource="classpath:spring/MyDataSource.xml"/> 

    <!-- Property File location --> 
    <context:property-placeholder location="${ext.properties.dir}"/> 


    <!--The services are auto-detected POJOs labeled with the @Service annotation.--> 
<context:component-scan base-package="com.xyz"/> 

</beans> 

和Maven的配置文件的配置如下build-dev.properties的

<build> 
    ..... 
    <resources> 
    <resource> 
     <directory>src/main/resources</directory>    
     <filtering>true</filtering> 
    </resource> 
    </resources> 
<filters> 
    <filter>src/main/resources/build/build-${environment}.properties</filter> 
</filters> 
</build> 

<profiles> 
    <profile> 
     <id>uat</id> 
     <activation> 
      <property> 
       <name>env</name> 
       <value>uat</value> 
      </property> 
     </activation> 
     <properties> 
       <environment>uat</environment> 
     </properties> 
    </profile> 
    <profile> 
     <id>prod</id> 
     <activation> 
      <property> 
       <name>env</name> 
       <value>prod</value> 
      </property> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <properties> 
       <environment>prod</environment> 
     </properties> 
    </profile> 
</profiles> 

内容被

ext.properties.dir=file:///C:/Temp/myProp.properties

我的问题是, Maven配置文件过滤不起作用,并且在包装过程中没有更换属性${ext.properties.dir}。它表示工作时,我删除<context:component-scan base-package="com.xyz"/>标签,因此我把它放在需要过滤的属性下面。现在一切正常。我的问题是<context:component-scan base-package="com.xyz"/>有什么问题?

回答

4

我不认为,但高于<context:component-scan base-package="com.xyz"/>评论

<!--The services are auto-detected POJOs labeled with the @Service annotation.--> 

@maven fitlers特殊的意义。

说实话,我有这样的感觉,在弹簧配置文件和maven过滤器之间有许多重叠的语法来将它们一起使用。我的“解决方案”是为弹簧配置使用(尽可能长)两个文件。

  • 一个属性文件,即由使用PropertyPlaceholder配置器来加载属性文件弹簧过滤器
  • 普通Spring配置文件(占位符)操纵。
+0

你击中目标。如果你能告诉我在Maven过滤器中使用了什么'@'会很好。 Maven在看到它时能够理解什么? – 2012-02-02 11:01:50

+0

我明白了,但在我们的案例中,我无法避免它,因为PropertyPlaceholder的位置来自基于环境的属性文件-Linux具有与Windows位置不同的属性文件的不同路径。无论如何,谢谢。 – 2012-02-02 11:09:51

+0

@Aravind答:behinde属性文件,它总是相同的文件(具有不同的内容),他们在类路径中的相同位置,所以胜利与无关紧要。 – Ralph 2012-02-02 11:38:02