avaje / avaje-inject

Dependency injection via APT (source code generation) ala "Server-Side Dagger DI"
https://avaje.io/inject
Apache License 2.0
227 stars 21 forks source link

Adds `@External` annotation #553

Closed SentryMan closed 4 months ago

SentryMan commented 4 months ago

Adds a similar annotation to @Nullable such that compile time validations can be disabled for beans not managed by Inject, but will still fail at runtime if the bean is not present. An example use case is beans provided by Jooby

rbygrave commented 4 months ago

renames the processor

Any reason for that?

SentryMan commented 4 months ago

Any reason for that?

Aesthetics

SentryMan commented 4 months ago

hmm wait a sec

SentryMan commented 4 months ago

Okay, I'm good with this

rbygrave commented 4 months ago

I'm pretty sure this does not support partial compile.

Edit: Seems like it isn't needed - all good !!

ponyjoy commented 2 months ago

@SentryMan

@Factory
public class JdbiFactory {
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/myapp?serverTimeZone=Asia/Shanghai";
    private static final String DB_USER = "demo";
    private static final String DB_PASSWORD = "demo";
    private static Jdbi JDBI = null;

    @Bean
    public DataSource getDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(JDBC_URL);
        config.setUsername(DB_USER);
        config.setPassword(DB_PASSWORD);
        DataSource ds = new com.zaxxer.hikari.HikariDataSource(config);
        return ds;
    }

    @Bean
    public Jdbi getJdbi(DataSource ds) {
        JDBI = Jdbi.create(ds);
        JDBI.installPlugin(new org.jdbi.v3.sqlobject.SqlObjectPlugin());
        return JDBI;
    }
}

public class JdbiTodoService implements TodoService {

    final Jdbi jdbi;

    //  @Inject  
   //need to add @External before Jdbi parameter to compile 
    public JdbiTodoService(Jdbi jdbi) {
        if (jdbi == null) {
            throw new IllegalArgumentException("jdbi is null");
        }
        this.jdbi = jdbi;
    }
}

When I use version 10.1, why required to use @External to compile when using the constructor injection method with bean returned by the @Bean method? Isn’t the Jdbi object in this code managed by avaje-inject?

SentryMan commented 2 months ago

is it the same in inject 10.0? Also does the JdbiTodoService service exist in a separate module than the factory?

ponyjoy commented 1 month ago

is it the same in inject 10.0? Also does the JdbiTodoService service exist in a separate module than the factory? @SentryMan In 10.0 does not add annotations and compiles successfully.

JdbiTodoService in same project image

SentryMan commented 1 month ago

Is it possible for you to upload an example repo to GitHub so that I can reproduce?

SentryMan commented 1 month ago

Also is it the same on inject 10.2-RC1? If it is fine there then we should be good.

ponyjoy commented 1 month ago

@SentryMan jdbc-demo.zip OK, it's a demo project

SentryMan commented 1 month ago

did you have any luck with 10.2-RC1? Also where are you seeing this error? I'm compiling with @External removed and 10.1 and I don't see the error when I run mvn clean package

ponyjoy commented 1 month ago

did you have any luck with 10.2-RC1? Also where are you seeing this error? I'm compiling with @External removed and 10.1 and I don't see the error when I run mvn clean package

It’s really strange. Without adding @External, the compilation passed with versions 10.0, 10.1, and 10.2-RC1. It must be because I didn’t run mvn clean compile before.

rob-bygrave commented 1 month ago

There were 2 regressions for "Partial Compile" starting in version 10.0-RC5 including 10, 10.1, 10.2-RC*.
The regressions were only fixed in 10.2.

and I don't see the error when I run mvn clean package

You don't see these bugs with a full compile - so mvn clean package should have always worked, but partial compile with any 10.x prior to 10.2 could definitely produce those errors.

I suspect that was what this issue is. You should see no issue with 10.2 with partial compilation now.