Change the name of the injected service in grails

It is impossible to find information about this, if you have, indicate me the desired topic / message / link!

I have a service, let's say it's called "SomeServiceWithAReallyLongNameICannotChange". Of course, the usual way to use services is to allow graals to enter them using descriptive or typed notation:

class SomeClass{ //... def someServiceWithAReallyLongNameICannotChange //... } 

- or -

 class SomeClass{ //... SomeServiceWithAReallyLongNameICannotChange someServiceWithAReallyLongNameICannotChange //... } 

What I would like to do is rename the service to something shorter, but only where I use it, since I cannot change the name of the actual service. I tried using the β€œhow” record, as you did with the import, and I tried changing the name in the typed declaration, but none of these things work. Is it possible?

I tried something like this:

 class SomeClass{ //... def someServiceWithAReallyLongNameICannotChange as SS //and I tried def SomeServiceWithAReallyLongNameICannotChange SS //no joy //... } 

Thanks for your help!

+4
source share
4 answers

The solution is to use autwire by type. By default, grails uses autwire by name, and therefore you must declare a service with the same name as bean.

Here is an example

 class FooController { boolean byName = false //set autowire by type SomeReallyLongService service } 
  • You must define a boolean variable byName and set it to false
  • You cannot use def, but you must use the actual type of service when declaring a service
  • It will enable autowire by type for the entire controller, so all other dependencies will also be automatically printed by type

It is explained here

Update: You can even use Autwired annotation with Qualifier.

Example:

 class MyController { @Autowired @Qualifier("myServiceWithDifferntName") def someService } 
+6
source

You can create a new bean through resources.groovy

 beans = { ss(SomeServiceWithAReallyLongNameICannotChange) } 

Then you can enter it usually:

 class SomeClass { //... def ss //... } 
+3
source

Another option, if you do not want to use autowire by type through the entire controller and do not want to add a custom bean for some reason, then add a simple method for your controllers where you need services, for example:

 def someServiceWithAReallyLongNameICannotChange def getSS() { someServiceWithAReallyLongNameICannotChange } 

You can then refer to the service using ss.someMethod() anywhere in this controller.

However, this still requires adding a piece of code for each controller that you use this service, or you will have inconsistent naming of the service.

(Personally, I think the doelleri method is the best, as it allows you to be consistent in naming without changing individual classes.)

+1
source

I tried and did not work for me on Grails 3, did it by doing

 import org.springframework.beans.factory.annotation.Autowired ... @Autowired PaymentStrategyService paymentStrategySvc 
0
source

Source: https://habr.com/ru/post/1395656/


All Articles