dependency-injection

Dagger 2.0 Constructor injection and Singleton

天大地大妈咪最大 提交于 2020-01-02 06:13:09
问题 is it possible to use Dagger 2.0 Constructor injection and singleton at one time. I don't find the answer in the documentation. Example: @Singleton public class MyClass { private final OtherClass member; @Inject public MyClass(OtherClass member){ this.member = member; } } Constructor injection does work for sure. But is it guaranteed that MyClass is created as a singleton if I write @Singleton on the class? Thank you 回答1: Yes. Since Dagger 2 generates the source code for you, it is easy to

How to pass parameter to injected class from another class in CDI?

China☆狼群 提交于 2020-01-02 05:26:06
问题 I am new to CDI, tried to find solution for this question, but, couln't found any. Question is Suppose I have one class which is being injected(A) from, where some value(toPass) is getting injected, now I want to pass this same value(toPass) to class B, which is getting injected from class A. public class A { String toPass = "abcd"; // This value is not hardcoded @Inject private B b; } public class B { private String toPass; public B(String toPass) { toPass = toPass; } } Can anybody please

Dependency Injection in ASP.NET Core

限于喜欢 提交于 2020-01-02 05:21:26
问题 In Autofac you can register your dependencies with RegisterAssemblyTypes so you will be able todo something like this, is there a way to do the somthing similar in the build in DI for .net Core builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data")) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces() .InstancePerLifetimeScope(); This is what i am trying to register LeadService.cs public class LeadService : ILeadService { private readonly ILeadTransDetailRepository

ASP.NET core call async init on singleton service

删除回忆录丶 提交于 2020-01-02 04:59:31
问题 I have a service that asynchronously reads some content from a file in a method called InitAsync public class MyService : IService { private readonly IDependency injectedDependency; public MyService(IDependency injectedDependency) { this.injectedDependency = injectedDependency; } public async Task InitAsync() { // async loading from file. } } Now this service is injected into my controller. public class MyController : Controller { private readonly IService service; public MyController

Android Dagger 2 POJO field Inject null

…衆ロ難τιáo~ 提交于 2020-01-02 04:07:15
问题 Just started using Dagger 2 today and I'm a bit confused on how exactly I need to set everything up. I'm trying to inject a POJO, but it's always null. First, some code: App.java private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent .builder() .appModule(new AppModule(this)) .build(); } public AppComponent component() { return appComponent; } AppModule.java @Module public class AppModule { private Application app; public

What is Spring's Minimum Dependencies for Dependency Injection?

拜拜、爱过 提交于 2020-01-02 04:04:06
问题 What are the minimum dependencies required to just use Spring's dependency injection (core framework only)? I'm using Spring for a standalone application, and I'd like to minimize the number of dependencies that I have to ship with the application. I suppose I could systematically remove a Jar and see if the application breaks, but it would be much better if someone had a definitive answer. Oh, and I'm using Spring 2.5. 回答1: Check out the readme.txt that comes with the download of Spring 2.5.

pass “HardCoded” Constructor Arg Class<T> to bean via Spring Config

落爺英雄遲暮 提交于 2020-01-02 03:44:17
问题 I have a generic type that I am injecting into a service. Because of the way generics are implemented in Java, I need to have a constructor arg (or property setter) that holds the Class information of the generic type parameter. My question is -- Can I, via property injection or specifying a constructor arg, pass in an instance of Class with spring? I DO know the type of T before run time so I know specifically what the Type parameter will be. I was thinking it would look something like this:

Simple Injector conditional injection

不打扰是莪最后的温柔 提交于 2020-01-02 02:51:28
问题 Lets say I have two Controllers: ControllerA and ControllerB. Both of those controllers accept as parameter IFooInterface. Now I have 2 implementation of IFooInterface, FooA and FooB. I want to inject FooA in ControllerA and FooB in ControllerB. This was easily achieved in Ninject, but I'm moving to Simple Injector due to better performance. So how can I do this in Simple Injector? Please note that ControllerA and ControllerB resides in different assemblies and are loaded dynamically. Thanks

Ninject, passing constructor argument to the kernel

自作多情 提交于 2020-01-02 02:31:32
问题 Here is my problem: I want to pass in one of the values to the constructor every time I request an instance form the kernel. I written some code below to illustrate the problem. The test is not failing so I guess that this works, but it does look pretty ugly. Is there a better, cleaner way to accomplish this with Ninject? Or should I rethink my design? All suggestions are appreciated. [TestFixture] public class Sandbox { [Test] public void Run_Forrest_Run() { using (var kernel = new

DI container, factory, or new for ephemeral objects?

北慕城南 提交于 2020-01-02 02:00:33
问题 When dealing with objects that require data known only at runtime, such as a username and password, where should object instantiation happen: by using new, in a factory, or in a DI container? For example, I could just new an object once I have the data: UserCredentials creds = new UserCredentials(dialog.getUsername(), dialog.getPassword()); Or, I could use a factory: UserCredentials creds = CredentialsFactory.create(dialog.getUsername(), dialog.getPassword()); Or, I could use a provider