Injection problem with actions

I am trying to register a custom action class using Table.addAction() method. The action appears properly when used declaratively in the screen descriptor but that approach was not covering all the needs so I switched to the approach of registering it via addAction().

A first try without any particular action content worked fine, the action appears in the screen as expected. However, when I tried to open a different screen in the action code using ScreenBrowsers service, I got null pointer exceptions as this service was not properly injected.

I looked at the code of EditAction class and it was quite similar to mine. I tried replacing my action with EditAction in my calling screen and got a similar error (on the security service, which was not injected either).

I suppose the problem must be in the creation of the action instance. As suggested by the examples in the documentation, I had used

MyAction action = new MyAction

which looks pretty suspicious now that I know that “action” would not be injected with its dependencies.

Is there a better way to instantiate MyAction, which ensures it is also injected with the dependencies?

I did some research and digging in the standard code and indeed, creating the action with new() gives an object which is not injected with its dependencies.

I solved the problem by injecting Actions component in the calling screen and replacing

MyAction action = new MyAction();

by

@Inject
private Actions actions;

MyAction Action = actions.createAction(MyAction.class);

This provided a fully autowired action instance. The only question that remains is whether this is the recommended way to create an action, or is there a better one?

Hi,

Yes, this is an OK solution. CUBA itself uses this factory interface to create standard actions.