1

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.

  • can I do "Model A"?
  • If yes, what needs to happen?

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.

will
  • 4,799
  • 8
  • 54
  • 90
  • It looks like you are using the groovy `Singleton` annotation instead of javax `Singleton` annotation... – Sascha Frinken Aug 18 '18 at 08:49
  • For Model A, several things are unclear. In `MyApp` you have a `main` method, but it isn't `static` so you won't be able to run it as a real `main`. Assuming that is mistake in your example, the next problem is that you are trying to reference an instance field from a `static` method, which is not allowed and nonsensical. Separate from all of that, you are using `@Inject` in a class which doesn't look like it is a bean, so it won't be subjected to DI. – Jeff Scott Brown Aug 21 '18 at 20:19
  • @SaschaFrinken ... Nope, `import javax.inject.Inject;` and `import javax.inject.Singleton;` – will Aug 23 '18 at 01:42
  • @JeffScottBrown ... I will change the name of the method. It is NOT a static. Key method is `runApp( String... args )` – will Aug 23 '18 at 01:43
  • @JeffScottBrown ... Thanks for the comment about "beans". Now that I have had time to test a few things and read _more_ I am _imagining_ that some expansion / explanation for of the remark: " ` – will Aug 23 '18 at 13:42
  • (_continued_...) _of the_ remark: "_`you are using @Inject in a class which doesn't look like it is a bean, so it won't be subjected to DI`_"; Is the information the question seeks. The referenced: `Engine` example (_but_) is a direct quote from the Micronaut documentation on: **[3.1 Defining beans](https://docs.micronaut.io/latest/guide/index.html#beans)** (latest). The project describes `@Singleton` _is-a_ bean. This is an example of an _assumed_ understanding commonly seen in documents -- Your insight will probably unlock a solution. Insights welcome. – will Aug 23 '18 at 13:57
  • Beans are only auto injected into other beans. In your case there will be a `Vehicle` bean but there will not be a `MyApp` bean. In `MyApp` you have a `Vehicle` property that is marked with `@Inject`. `@Inject` is only relevant in beans that are subjected to dependency injection. Only beans are subjected to dependency injection. You don't have a `MyApp` bean, so no `MyApp` will be subjected to dependency injection. – Jeff Scott Brown Aug 23 '18 at 15:26
  • @JeffScottBrown ... Many thanks.I knew something was missing. So an answer to the question _at-hand_ then would promote `MyApp` to a `Bean` I guess. I am asked the wrong question. The examples I've seen so far seem to `.run( MyApp.class )` or just `run()`. Is there a method then to ensure that the "`runApp(...){ ... }`" code gets executed(?) and also How? – will Aug 24 '18 at 03:15
  • "Is there a method then to ensure that the "runApp(...){ ... }" code gets executed(?) and also How?" - There are a lot of ways to make sure it gets run. At what point do you want it to run? – Jeff Scott Brown Aug 24 '18 at 14:13
  • The question asked what's necessary to let the `runApp(..)` access the `@Inject vehicle` in "**Model A**". I added a `static Main()` and some labels. The aim is for label-C to execute `runApp()` and `start` the injected `vehicle`. (per example: [Defining beans](https://docs.micronaut.io/latest/guide/index.html#beans)). It is clear your input that something is needed in/before the declaration at label-A and perhaps some set-up as indicated at label-B. In my ignorance, this is the most simple example I could come-up with at the time. – will Aug 27 '18 at 13:16

0 Answers0