[Solved] Spring introduces the properties variable Error: org.springframework.beans.TypeMismatchException…

Configure the data source through the properties configuration file. The code is as follows:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="3" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:config/db.properties</value>
                <!-- Note the path to write, the above write method to add classpath, path before can not have /, the following write method properties file to be in the WEB-INF directory, path before to have/
                <value>/WEB-INF/test.properties</value> -->
            </list>
        </property>
    </bean>
    
    <bean id="dataSource" name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${db.jdbc.driverClassName}" />
        <property name="url" value="${db.jdbc.url}" />
        <property name="username" value="${db.jdbc.username}" />
        <property name="password" value="${db.jdbc.password}" />
        <property name="initialSize" value="${db.initialSize}"/>
        <property name="maxActive" value="${db.maxActive}"/>
        <property name="maxWait" value="${db.maxWait}" />
        <property name="poolPreparedStatements" value="true" />
        <property name="defaultAutoCommit" value="true" />
        <property name="maxIdle" value="${db.maxIdle}" />
        <property name="minIdle" value="${db.minIdle}" />
        <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="select 1 from dual"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="false"/>
        <property name="filters" value="stat"/>
    </bean>

Project startup error:

org.springframework.beans.TypeMismatchException…

java.sql.SQLException: ${db.jdbc.driverClassName}

After consulting the data, it is found that this configuration is caused by:

<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.xiaomu.**.dao" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

Reason:

Mappercannerconfigurer has customized the initialization interface of the bean. During its initialization, the placeholder has not been initialized, resulting in an error when the properties value has not been injected

Solution:

Mapperscannerconfigurer has two options when injecting sqlsessionfactory. One is to directly inject sqlsessionfactory objects during spring configuration, and the other is to inject beans through names

So < property name=”sqlSessionFactory” ref=”sqlSessionFactory” /> Change to < property name=”sqlSessionFactoryBeanName” value=”mybatisSqlSessionFactory” /> Note that after the change, the original sqlsessionfactory name cannot be used for the value value, because it is injected through the name, so using sqlsessionfactory will still report an error

Note: sqlsessionfactorybeanname is a non mandatory label and can be configured without a single data source

Description:

Mappercannerconfigurer is similar to the mappers tag in that it scans mappers to determine the mapping relationship. The mapper configuration form is:

<mappers>
    <!-- sql file configuration way one: specify the file path location, sql.xml file namespace can be directly with dao name, note that at this time sql.xml file to be in the resource directory -->
    <! -- <mapper resource="mybatis/sqlmap/user/brokerinfoMapper.xml"/> -->
    <! -- sql file configuration way two: sql file and dao interface in the same directory, only need to configure the sql.xml file and the interface package path , but the sql.xml file and the interface path are in the same directory.
But the namespace of sql.xml should be the full path of the dao interface-->
    <!-- <package name="com.test.web.dao"/> -->
</mappers>

Similar Posts: