0

After I import spring-cloud-context jar into my spring-boot2 project, it failed to parse property placeHolder in my datasource bean "org.apache.tomcat.jdbc.pool.DataSource" config. But the same property placeHolder in other bean config is parsed successfully.

Other datasource bean like "com.alibaba.druid.pool.DruidDataSource" also has the same problem. Which configuration class in spring-cloud-context cause the spring load datasource bean so eagerly?

enter image description here spring config:

<bean id="dataSourceTarget" class="org.apache.tomcat.jdbc.pool.DataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${mysql.url}" />
    <property name="username" value="${mysql.username}" />
    <property name="password" value="${mysql.password}" />

    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="true" />
    <property name="testOnReturn" value="false" />
    <property name="validationInterval" value="60000" />
    <property name="validationQuery" value="select 1 from dual" />
    <property name="maxActive" value="100" />
    <property name="minIdle" value="1" />
</bean>
<bean id="configTest1" class="wzp.rest.service.ConfigTest"
    primary="true">
    <property name="password" value="${mysql.password}" />
</bean>

pom.xml:

    <!-- feign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.4.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-archaius</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-ribbon</artifactId>
            </exclusion>
            <exclusion>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-hystrix</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
    </dependency>

1 Answer 1

0

I used MapperFactoryBean of mybatis to load mapper, and MapperFactoryBean load sqlSessionFactory and datasource bean earlier than PropertySourcesPlaceholderConfigurer changing the placeholder.

When I use org.mybatis.spring.mapper.MapperScannerConfigurer to load mapper, the problem gone.

@ConditionalOnMissingBean makes all factory beans and their dependent beans initialed early, and their getObjectType function called before placeholders in their definition was parsed. Then spring cloud config client in spring-cloud-context jar makes normal PropertySourcesPlaceholderConfigurer can not parse and update placeholders of these beans. This behavior can be forbade by setting for whom do not use cloud config:

spring.cloud.config.enabled=false
spring.cloud.refresh.enabled=false
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.