返回

提问 添加收藏

基于java注解的代码,Spring使用MapperScannerConfigurer无法读取*.properties文件

本页包含1张图片,默认未加载,显示所有图片

TGYD 发布于 2019年09月11日 (共有8个回帖)

@温安适 大神你好,请教一个问题啊:

基于java注解的代码,我用了selSessionFactoryBeanName属性,Spring使用MapperScannerConfigurer仍然无法读取*.properties

 

源码如下:

package com.wjt.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @Time 2019/9/10/2:42
 * @Author jintao.wang
 * @Description
 */
@Slf4j
@Configuration
@EnableTransactionManagement
@PropertySource(value = "classpath:dbconfig/jdbc.properties", encoding = "UTF-8")
public class DBConfig {

    @Value(value = "${jdbc_driver}")
    private String driverName;

    @Value(value = "${jdbc_url}")
    private String url;

    @Value(value = "${jdbc_user}")
    private String userName;

    @Value(value = "${jdbc_password}")
    private String password;

    @Value(value = "${jdbc_maxActive}")
    private int maxActive;
    @Value(value = "${jdbc_minIdle}")
    private int minIdle;

    @Value(value = "classpath:dbconfig/mybatis_config.xml")
    private Resource configLocation;

    @Value(value = "classpath:mapper/*.xml")
    private Resource[] mapperLocations;

    /*   public static final PathMatchingResourcePatternResolver RESOLVER = new PathMatchingResourcePatternResolver();*/

    @Bean
    public DataSource dataSource() {
        log.info("driverName={};url={};userName={};password={};", driverName, url, userName, password);
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName(driverName);
        dataSource.setUrl(url);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(minIdle);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        log.info("driverName={};url={};userName={};password={};dataSource={};", driverName, url, userName, password, dataSource);
        return dataSource;
    }


    @Bean(name = {"sqlSessionFactoryBean"})
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DataSource dataSource) {

/*        Resource configLocation = new ClassPathResource("dbconfig/mybatis_config.xml");
        Resource[] mapperLocations = {new ClassPathResource("mapper/ArticleMapper.xml")};*/

        log.info("configLocation={};mapperLocations={};", configLocation, mapperLocations);
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfigLocation(configLocation);

        sqlSessionFactoryBean.setMapperLocations(mapperLocations);
        log.info("sqlSessionFactoryBean={};", sqlSessionFactoryBean);
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        log.info("driverName={};url={};userName={};password={};mapperLocations={};", driverName, url, userName, password, mapperLocations);
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.wjt.dao");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        mapperScannerConfigurer.setAnnotationClass(Repository.class);
        //mapperScannerConfigurer.setProcessPropertyPlaceHolders(true);
        log.info("mapperScannerConfigurer={};", mapperScannerConfigurer);
        return mapperScannerConfigurer;
    }


    @Bean
    public PlatformTransactionManager transactionManager(@Autowired DataSource dataSource) {
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        log.info("transactionManager={};", transactionManager);
        return transactionManager;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        log.info("propertySourcesPlaceholderConfigurer={};", propertySourcesPlaceholderConfigurer);
        return propertySourcesPlaceholderConfigurer;
    }

/*    @Bean
    public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer=new PropertyPlaceholderConfigurer();
        log.info("propertyPlaceholderConfigurer={};",propertyPlaceholderConfigurer);
        return propertyPlaceholderConfigurer;
    }*/

}

jdbc.properties配置文件如下:

jdbc_driver=com.mysql.cj.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/ssm_train?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
jdbc_user=root
jdbc_password=linux2014
jdbc_maxActive=10
jdbc_minIdle=1
jdbc_driver_class_path=C:/Users/17460/.m2/repository/mysql/mysql-connector-java/6.0.6/mysql-connector-java-6.0.6.jar

 

测试用例代码如下:

package com.wjt.config;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import javax.sql.DataSource;

import static org.junit.Assert.*;

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DBConfig.class})
public class DBConfigTest {
    @Resource
    private DataSource dataSource;

    @Test
    public void db() {
        log.info("dataSource={};", dataSource);
    }
}

 

 

执行测试用例的结果如下:

点击加载图片

这时无法读取jdbc.properties文件内的内容。

 

但是删除DBConfig.mapperScannerConfigurer()这个方法后就可以成功读取了。

请大神帮忙分析一下原因和解决办法吧!先行谢过了。

 

 

 

 

文明上网,理性发言
客户端 Android iPhone WP7