其他分享
首页 > 其他分享> > Spring配置和Maven配置文件的怪异行为

Spring配置和Maven配置文件的怪异行为

作者:互联网

我在使用Maven< filter>标签和Spring配置.我的理解是,Spring配置是Maven的普通XML文件,但是我遇到了< context:component-scan base-package =“ com.xyz” />问题.标签 .测试XML文件如下

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

build-dev.properties的内容是

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” />有什么问题? ?

解决方法:

我不认为< context:component-scan base-package =“ com.xyz” />但是上面的评论

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

@在maven fitlers中有特殊含义.

老实说,我感觉到Spring配置文件和Maven过滤器在语法上有很多重叠之处,以便一起使用它们.我的“解决方案”是对弹簧配置使用(尽可能长)两个文件.

>一个属性文件,由弹簧过滤器操作
>一个普通的Spring配置文件(带有占位符),该文件使用PropertyPlaceholder配置器来加载属性文件.

标签:maven,maven-3,spring
来源: https://codeday.me/bug/20191101/1985893.html