question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Annotation for Auto Creation of Singleton Service

See original GitHub issue

Suppose 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:closed
  • Created 3 years ago
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, May 20, 2021

@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.

1reaction
pkozlowski-opensourcecommented, Apr 27, 2020

For example for the AssetService included by the original example here we need the HttpClient. If we start the application without having included the HttpClientModule the application fails to start. The compilation fails.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found