1

I have below project structure

enter image description here

The main project DEMO has a dependency on the Gradle project SERVICE which implements the interface from the PORT project.

Demo build.gradle

dependencies {
    implementation project(':port')
    runtime project(':service')
} 

Service build.gradle

dependencies {
    implementation project(':port')

    implementation("javax.annotation:javax.annotation-api:1.3.2")
    compile "io.micronaut:micronaut-inject:2.4.0"
    annotationProcessor "io.micronaut:micronaut-inject-java:2.4.0"
}

Service class implement IStartUpPort

@Singleton
public class StartUpService implements IStartUpPort {
    @Override
    public void toUpperCase() {
        System.out.println("Something happened");
    }
}

Port build.gradle

dependencies {
    implementation("javax.annotation:javax.annotation-api:1.3.2")
    compile "io.micronaut:micronaut-inject:2.4.0"
    annotationProcessor "io.micronaut:micronaut-inject-java:2.4.0"
}

Port interface

public interface IStartUpPort {
    void toUpperCase();
}

In the main project, I am doing the DI on a service project and facing the exception

@Singleton
public class StartUp implements ApplicationEventListener<StartupEvent> {
    private final IStartUpPort iStartUpPort;

    public StartUp(IStartUpPort iStartUpPort) {
        this.iStartUpPort = iStartUpPort;
    }

    @Override
    public void onApplicationEvent(StartupEvent event) {
        iStartUpPort.toUpperCase();
    }
}

Exception

Message: No bean of type [fete.bird.IStartUpPort] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new StartUp([IStartUpPort iStartUpPort])
    at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1034)
    at com.example.$StartUpDefinition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1943)
    at io.micronaut.context.DefaultBeanContext.addCandidateToList(DefaultBeanContext.java:3091)
    at io.micronaut.context.DefaultBeanContext.getBeanRegistrations(DefaultBeanContext.java:2968)
    at io.micronaut.context.DefaultBeanContext.getBeansOfType(DefaultBeanContext.java:762)
    at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1304)
    at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:246)
    at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:165)
    at io.micronaut.runtime.Micronaut.start(Micronaut.java:71)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:311)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:297)
    at com.example.Application.main(Application.java:8)
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [fete.bird.IStartUpPort] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2367)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1261)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1012)
    ... 12 common frames omitted

This is a Micronuat messenging application repo can be found here https://github.com/anandjaisy/MessengingMultiGradleProject

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • It does not look like there is an implementation of `iStartUpPort` anywhere in your project. Are you expecting to pull that in from some `.jar` library? Something else? – Jeff Scott Brown Mar 16 '21 at 12:08
  • @JeffScottBrown there is an implementation for IStartUpPort in the service project StartUpService class – San Jaisy Mar 16 '21 at 12:31
  • Do you have an implementation of `iStartUpOutPort`? – Jeff Scott Brown Mar 16 '21 at 12:33
  • The question mentions a `IStartUpPort` interface with a `void toUpperCase();` method in it. That doesn't appear to be in the repo. Is that code supposed to be relevant? – Jeff Scott Brown Mar 16 '21 at 12:34
  • `public class StartUpPort implements IStartUpPort` also appears to be missing from the repo. – Jeff Scott Brown Mar 16 '21 at 12:35
  • Its here https://github.com/anandjaisy/MessengingMultiGradleProject/blob/main/service/src/main/java/fete/bird/StartUpService.java – San Jaisy Mar 16 '21 at 12:36
  • That `record` doesn't implement `IStartUpPort` or `IStartUpOutPort`.. – Jeff Scott Brown Mar 16 '21 at 12:37
  • The question has changed a few times. It now indicates that `StartUp` accepts a `IStartUpPort` constructor argument. `IStartUpPort` doesn't seem to exist. Is that supposed to be `IStartUpOutPort`, `IStartUpInPort` or something else? – Jeff Scott Brown Mar 16 '21 at 12:43
  • should be fine now, I made changes on the question – San Jaisy Mar 16 '21 at 12:49
  • You have replaced the repo with whole new code, and it appears to work now. – Jeff Scott Brown Mar 16 '21 at 13:02
  • The code you originally had in your repo is at https://github.com/jeffbrown/MessengingMultiGradleProject. That project contains different code than you have in your repo now. – Jeff Scott Brown Mar 16 '21 at 13:03
  • Yes, but code has some issue, so replace with the new code. It's not working for me, I still get same error – San Jaisy Mar 16 '21 at 13:04
  • "I still get same error" - Are you seeing something like this? https://gist.github.com/jeffbrown/3c419fe2c05985dd2e58fc06391e6986 (nevermind the `cd: "./: No such file or directory` error... that is because your `gradlew` file has some problems, same issue that https://github.com/anandjaisy/MultipleProjectDependency/pull/2 addressed in one of your other repos). – Jeff Scott Brown Mar 16 '21 at 13:13
  • It is surprising to me that you are getting the same error. I know what is wrong in your other project that causes the error, and your new project isn't configured with the same problem. – Jeff Scott Brown Mar 16 '21 at 13:17
  • No, I am not facing the above mention error, I am getting the exception as mention in the equestion – San Jaisy Mar 16 '21 at 13:21
  • "No, I am not facing the above mention error" - I can't reproduce that. The gist that I linked above shows what happens when I check out a clean version of your repo and run it. I am sorry that I can't help. Best of luck. – Jeff Scott Brown Mar 16 '21 at 13:22
  • Can you please clone completely new project and check if you are not facing that issue. – San Jaisy Mar 16 '21 at 13:42
  • "Can you please clone completely new project and check if you are not facing that issue." - Yes. I did. The gist I linked above shows what happens (https://gist.github.com/jeffbrown/3c419fe2c05985dd2e58fc06391e6986). I just now cloned the repo again and I see the same behavior. It appears to work. Your old repo didn't have an implementation for one of the beans, which is why you were getting the "No bean of type" error. The current repo does have the bean, and it appears to work. I am sorry I can't reproduce the behavior you are seeing. – Jeff Scott Brown Mar 16 '21 at 13:46
  • I found the issue for me, on that build task I need to run the jar task, I was doing only buid. Its works for me after running the jar task – San Jaisy Mar 16 '21 at 14:22
  • " Its works for me after running the jar task" - `./gradlew assemble` is commonly used. – Jeff Scott Brown Mar 16 '21 at 14:28
  • ./gradlew assemble is commonly used no idea on this one, can you please elaborate more – San Jaisy Mar 16 '21 at 15:01
  • I don't have anything specific to elaborate around that. Sorry I can't help. – Jeff Scott Brown Mar 16 '21 at 15:02

1 Answers1

1

The code currently in the linked repo is configured such that the error shown in the question will not happen if the app is built and executed properly.

Clone the repository:

$ git clone git@github.com:anandjaisy/MessengingMultiGradleProject.git

Build the service:

$ cd MessengingMultiGradleProject
$ ./gradlew assemble

Run the service:

$ java -jar build/libs/demo-0.1-all.jar 
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v2.4.0)

Something happened
13:33:55.037 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 405ms. Server Running: 0 active message listeners.
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • FYI... If you delete `service/src/main/java/fete/bird/StartUpService.java` from the current repo, you will have basically recreated the problem that existed in a version of the repo that has since been deleted. That will trigger the `No bean of type [fete.bird.IStartUpPort] exists.` error mentioned in the question. – Jeff Scott Brown Mar 16 '21 at 18:47