Spring boot的autowired问题?报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Spring boot的autowired问题?报错

爱吃鱼的程序员 2020-06-07 22:40:51 40

想自己做一个Spring boot的项目,结果遇到问题了,请教各位大神解答。。

 

@Autowired
DataSource dataSource;

正常运行

改成

private final DataSource dataSource;

@Autowired
public MyBatisConfig(DataSource dataSource) {
    this.dataSource = dataSource;
}

报错如下

Error creating bean with name 'embeddedServletContainerCustomizerBeanPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBatisConfig' defined in file [E:\springboot-mybatis\target\classes\cn\no7player\common\MyBatisConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.no7player.common.MyBatisConfig$$EnhancerBySpringCGLIB$$737d81d7]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.no7player.common.MyBatisConfig$$EnhancerBySpringCGLIB$$737d81d7.<init>()

 

完整代码如下

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {


    private final DataSource dataSource;

    @Autowired
    public MyBatisConfig(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
XML Java 数据格式 Spring
分享到
取消 提交回答
全部回答(1)
  • 爱吃鱼的程序员
    2020-06-07 22:41:09

    Nodefaultconstructorfound;nestedexceptionisjava.lang.NoSuchMethodException:cn.no7player.common.MyBatisConfig$$EnhancerBySpringCGLIB$$737d81d7.<init>()

     

    这么大的提示,视而不见?

    别这样。

    一楼6666!没有默认的构造方法。

    回初中好好学学英语就看懂了

    楼上已经说的很明白了,下回记得多看看,别摸到就问

    为什么不用 mybatisspringbootstarter

    1楼真相了

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题