Annotation for Auto Creation of Singleton Service
See original GitHub issueSuppose we are doing this in app.component.ts
constructor(assets: AssetsService) {}
The AssetsService
is not actually used. We just rely on the constructor to fetch assets like SVG custom icons etc.
It would be nice not to have to inject it on boot in order to get the constructor to run. Something like:
@AutoConstruct()
export class AssetsService {
constructor(
private matIconRegistry: MatIconRegistry,
private domSanitizer: DomSanitizer) {
{
this.matIconRegistry.addSvgIcon(
"fsalpha",
this.domSanitizer.bypassSecurityTrustResourceUrl(logoURL))
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:21 (8 by maintainers)
Top Results From Across the Web
A Singleton Session Bean Example: counter - The Java EE 6 ...
The counter example demonstrates how to create a singleton session bean. ... Annotating a singleton class with @Lock specifies that all the business...
Read more >Dagger 2 - what is the purpose of a @Singleton annotation class
The best solution according to me is to create a real Singleton , either double checked or of other variants and use this...
Read more >The Difference Between CDI and EJB Singleton - Baeldung
With CDI (Contexts and Dependency Injection), we can easily create singletons using the @Singleton annotation. This annotation is a part of ...
Read more >Extensibility - HK2 - Dependency Injection Kernel
The default Scope for services annotated with @Service is the Singleton scope. Service instances in the Singleton scope are created once and are...
Read more >Java Singleton Design Pattern Best Practices with Examples
Static block initialization implementation is similar to eager initialization, except that instance of the class is created in the static block ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@fireflysemantics that looks like an ordering issue, possibly related to the Webpack 5 update in the CLI (assuming you haven’t changed any other code). Injecting services into a module should still be possible in v12. In any way, this seems unrelated to this issue so please open a new one with a minimum reproduction.
I see this use-case (or a variant of it) in the wild all the time. When I was thinking about this use-case I was leaning in the direction of creating a DI container without bootstrapping an app. Such DI container could have HttpClientModule, your
AssetService
and anything else needed to fetch data before bootstrapping an app. If we had such mechanism we wouldn’t need change anything about DI injection.