Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

直接使用实例化Model对象, 报 NoSuchMethodException QueryBuilderConfig.<init>() #35

Open
windqyoung opened this issue May 18, 2023 · 1 comment

Comments

@windqyoung
Copy link

可以提供以下信息
bug类:

1 .使用的代码库的方式 ? (源码编译 / maven依赖 / 源码拷贝 ..)

repositories {
    mavenCentral()
    maven {
        url 'https://jitpack.io'
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'com.mysql:mysql-connector-j'
    implementation 'com.github.gaarason.database-all:database-spring-boot-starter:4.8.0'
}

2 .使用的代码库的方式版本 ? ( tag版本 / maven依赖版本 / commit hash ..)

3 .java版本
java 17

4 .框架以及版本 ( eg : spring boot 3.0.0 )
无框架

5 .数据库以及版本 ( eg : mysql 8.0.0 )
mysql8.0.27
6 .问题简述 ( 有必要的话, 可以贴下相关调用代码以及定义代码 )
以下是报错代码


public class Person {
    Integer id;
    String name;
    String firstname;
    String lastname;
    String addr;
}


public class PersonModel extends Model<Person, Integer> {
    @Override
    public GaarasonDataSource getGaarasonDataSource() {
        GaarasonDataSource build = GaarasonDataSourceBuilder.build(new Sdata2Application().dataSource());

        // 注释掉这行 就会报错 
//        new MysqlAutoconfiguration().init(build.getContainer());
        return build;
    }

    public static void main(String[] args) {
        var generalModel = new PersonModel();
        var record = generalModel.newQuery()
                .first();
        System.out.println("record = " + record);
    }
}

7 .预期效果

Exception in thread "main" gaarason.database.exception.ObjectNewInstanceException: Error instantiating object[interface gaarason.database.config.QueryBuilderConfig] with message : Error instantiating object[interface gaarason.database.config.QueryBuilderConfig]
	at gaarason.database.provider.ContainerProvider.getBeansInside(ContainerProvider.java:144)
	at gaarason.database.provider.ContainerProvider.getBeans(ContainerProvider.java:86)
	at gaarason.database.connection.GaarasonDataSourceWrapper.getQueryBuilder(GaarasonDataSourceWrapper.java:329)
	at gaarason.database.connection.GaarasonDataSourceWrapper.getQueryBuilder(GaarasonDataSourceWrapper.java:316)
	at gaarason.database.eloquent.ModelOfQuery.theBuilder(ModelOfQuery.java:50)
	at gaarason.database.eloquent.ModelOfQuery.newQuery(ModelOfQuery.java:55)
	at com.example.sdata2.PersonModel.main(PersonModel.java:23)
Caused by: gaarason.database.exception.ObjectNewInstanceException: Error instantiating object[interface gaarason.database.config.QueryBuilderConfig]
	at gaarason.database.util.ClassUtils.newInstance(ClassUtils.java:56)
	at gaarason.database.provider.ContainerProvider.lambda$defaultNewInstance$1(ContainerProvider.java:160)
	at gaarason.database.provider.ContainerProvider.getBeansInside(ContainerProvider.java:136)
	... 6 more
Caused by: java.lang.NoSuchMethodException: gaarason.database.config.QueryBuilderConfig.<init>()
	at java.base/java.lang.Class.getConstructor0(Class.java:3585)
	at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2754)
	at gaarason.database.util.ClassUtils.newInstance(ClassUtils.java:52)
	... 8 more

Caused by: gaarason.database.exception.ObjectNewInstanceException: Error instantiating object[interface gaarason.database.config.QueryBuilderConfig]

Caused by: java.lang.NoSuchMethodException: gaarason.database.config.QueryBuilderConfig.<init>()

建议类:

感觉像是直接实例化Model对象的时候, 没有把 MysqlQueryBuilderConfig 这个类给注册到container中.

@xutengx
Copy link
Member

xutengx commented Jun 6, 2023

你好

所指出的问题, 应该是不同java版本的加载机制有差异, 可以参考 文档 中的解决办法, 进行简单配置.

根据反馈的代码, 这边修改了下, 你可以作为参考

package com.temp;

import com.alibaba.druid.pool.DruidDataSource;
import gaarason.database.connection.GaarasonDataSourceBuilder;
import gaarason.database.contract.connection.GaarasonDataSource;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.eloquent.Model;

public class Application {

    public static void main(String[] arg) {
        PersonModel generalModel = new PersonModel();
        Record<Person, Integer> record = generalModel.newQuery()
            .first();
        System.out.println("record = " + record);
    }

    static {
        // 设置包扫描配置
        System.setProperty("gaarason.database.scan.packages", "com.temp");
    }

    private static final GaarasonDataSource dataSource = mysqlDataSource();

    private static GaarasonDataSource mysqlDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(
            "jdbc:mysql://mysql.local/test_master_0?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=true&autoReconnect=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
        druidDataSource.setDbType("com.alibaba.druid.pool.DruidDataSource");
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        druidDataSource.setInitialSize(5);
        druidDataSource.setMinIdle(5);
        druidDataSource.setMaxActive(10);
        druidDataSource.setMaxWait(60000);

        // 在此初始化 GaarasonDataSource,  可以是任意实现的 DataSource
        // 保存在内静态属性上, 以保证仅初始化一次
        GaarasonDataSource gaarasonDataSource = GaarasonDataSourceBuilder.build(druidDataSource);
        return gaarasonDataSource;
    }

    public class Person {
        Integer id;
        String name;
        String firstname;
        String lastname;
        String addr;
    }

    public static class PersonModel extends Model<Person, Integer> {

        @Override
        public GaarasonDataSource getGaarasonDataSource() {
//            GaarasonDataSource build = GaarasonDataSourceBuilder.build(new Sdata2Application().dataSource());
            // 简单的获取已经完成初始化的 GaarasonDataSource
            return dataSource;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants