will
will

Reputation: 5081

Simple Java field injection with Micronaut what's the missing step(s)?

I am not sure yet if I'm on the wrong track or not. There is an example on the Micronaut Getting Started page for a V8 Engine and injecting a Vehicle.

With that example in mind. What is the most straightforward way to implement "Model A" with Micronaut using Java? If there's no direct approach, what is the closest hands-off method with Micronaut?

My simple vision of vanilla field injection stepping-on with such an example is as so (using Java), I'm labelling it "Model A" ...

Model A

  import io.micronaut.context.*

  public class MyApp {                        // (A)

      @Inject 
      Vehicle vehicle;


      public void runApp( String... args ){

         println( vehicle.start() )
      }

      public static main( String... args ){

          //   whatever set-up and steps need   // (B)
          //   for auto-inject / auto-wiring.

          MyApp body = new MyApp( args  );

          body.runApp();                       // (C)
      }
  }

Where the annotation processor uses provides an instance of the @Singleton Vehicle in this example. Or creates a new instance in the case of non-singleton-s.

Either way the result of the process would be that I don't need to write code to 'instantiate' the code or find a factory to do so explicitly.

The example itself goes on to demonstrate the method I'll label "Model B" (using Groovy)...

Model B

  import io.micronaut.context.*
    ...
  Vehicle vehicle = BeanContext.run().getBean(Vehicle)

  println( vehicle.start() )

Which in fact is MORE typing than just writing:

  Vehicle vehicle = new Vehicle();

      //    OR

  Vehicle vehicle = Vehicle.getInstance();

With some libraries you need to initialise the scopes or context, I see that. The question boils donw to what must I do to inject Vehicle as shown in my code.

I made a @Singleton and tried to @Inject the field. The reference is NULL. I then made a @Providerand set a break point. That isn't called.

I've scanned lots of examples doing great things. I'd love to get into those fancy things too. Right now I'm in the basement looking for a way up to the ground floor. Many thanks for you guidance.

Upvotes: 1

Views: 2591

Answers (0)

Related Questions