Skip to content

Could not find any AvajeModule instances to wire #702

Closed
@petroniuchacz

Description

@petroniuchacz

Hi, I can successfully run the maven project that uses avaje from IDE, but after building a fat jar, I get the error:

1. No beans have been defined.
2. The avaje-inject-generator depedency was not available during compilation
3. Perhaps using Gradle and a misconfigured IDE? Refer to https://avaje.io/inject#gradle
Okt. 18, 2024 11:36:03 AM io.avaje.inject.spi.DBeanScope start
INFORMATION: Wired beans in 36ms
Exception in thread "main" java.util.NoSuchElementException: No bean found for type: interface javax.sql.DataSource name: null
        at io.avaje.inject.spi.DBeanScope.getByType(DBeanScope.java:113)
        at io.avaje.inject.spi.DBeanScope.get(DBeanScope.java:94)
        at com.example.MyApp.initDb(GisdbimporterApplication.java:50)
        at de.prehcmservices.gisdbimporter.GisdbimporterApplication.runWithArgs(GisdbimporterApplication.java:35)
        at java.base/java.util.Optional.ifPresent(Optional.java:178)
        at com.example.MyApp.main(GisdbimporterApplication.java:27)

pom file;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	...
	
	<properties>
		<java.version>22</java.version>
		<avaje-inject.version>10.4</avaje-inject.version>
	</properties>
	<dependencies>

		<!--mysql-->

		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<version>9.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.zaxxer</groupId>
			<artifactId>HikariCP</artifactId>
			<version>5.1.0</version>
		</dependency>

		<!--utils-->

		<dependency>
			<groupId>io.avaje</groupId>
			<artifactId>avaje-inject</artifactId>
			<version>${avaje-inject.version}</version>
		</dependency>
		<dependency>
			<groupId>io.avaje</groupId>
			<artifactId>avaje-inject-generator</artifactId>
			<version>${avaje-inject.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>22</source>
					<target>22</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.example.MyApp</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

DataSource is defined like:

package de.prehcmservices.gisdbimporter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zaxxer.hikari.HikariDataSource;
import de.prehcmservices.gisdbimporter.models.CmdArgs;
import io.avaje.inject.Bean;
import io.avaje.inject.Factory;

import javax.sql.DataSource;
import java.util.Arrays;

import static java.util.concurrent.TimeUnit.MINUTES;


@Factory
public class Configuration{
    @Bean
    public DataSource dataSource(CmdArgs args) {
        final var dataSource = new HikariDataSource();
        dataSource.setDriverClassName(com.mysql.cj.jdbc.Driver.class.getName());
        if (args != null) {
            dataSource.setJdbcUrl(args.dbUrl());
            dataSource.setUsername(args.dbUserName());
            dataSource.setPassword(args.dbPassword());
        }
        dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        dataSource.addDataSourceProperty("prepStmtCacheSize", "250");
        dataSource.addDataSourceProperty("cachePrepStmts", "true");
        dataSource.setMaxLifetime(MINUTES.toMillis(1));
        dataSource.setAutoCommit(false);
        dataSource.setMaximumPoolSize(26);
        return dataSource;
    }
}

Failing method:

	public static void initDb(BeanScope beanScope) {
		final var initiator = new DbInitiator(beanScope.get(DataSource.class), beanScope.get(CmdArgs.class));
		initiator.initDb();
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions