dependency-injection

How to pass constructor parameter while using spring auto wiring?

拟墨画扇 提交于 2020-01-24 09:58:29
问题 Our Project is using spring DI/IoC, so i am using autowiring to inject beans. The program needs to pass parameters to an object during it's instantiation. And the parameters are know at run time (not at compile time). How to achive this while using autowiring. Sample code is as below. Interface - IMessage package com.example.demo.services; public interface IMessage { String message(String name); } Implementations - SayHelloService package com.example.demo.services; import org.springframework

<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method

杀马特。学长 韩版系。学妹 提交于 2020-01-24 09:20:13
问题 I am trying to create a viewmodel module like in this example but I am having this error error: java.util.Map,javax.inject.Provider> cannot be provided without an @Provides-annotated method. I followed all the example, here are my codes ViewModelFactory class @Singleton public class ViewModelFactory implements ViewModelProvider.Factory { private final Map<Class<? extends ViewModel>, Provider<ViewModel>> mCreators; @Inject ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>>

How can i register a type with its interfaces as singleton using simple injector?

泄露秘密 提交于 2020-01-24 09:07:13
问题 I have models like below: public interface IUserLookupService { Guid[] GetDirectChilds(Guid userId); Guid[] GetAllChilds(Guid userId); Guid GetDepartment(Guid userId); } public interface ICanRefreshCache{ void Refresh(); } public class XHandler : IEventHandler<UserNameChanged> { ... } public class YHandler : IEventHandler<TeamCreated> { ... } public class CachedUserLookupService : IUserLookupService, ICanRefreshCache, IEventHandler<UserNameChanged> { private Func<ISession> _sessionFactory;

How can i register a type with its interfaces as singleton using simple injector?

こ雲淡風輕ζ 提交于 2020-01-24 09:05:47
问题 I have models like below: public interface IUserLookupService { Guid[] GetDirectChilds(Guid userId); Guid[] GetAllChilds(Guid userId); Guid GetDepartment(Guid userId); } public interface ICanRefreshCache{ void Refresh(); } public class XHandler : IEventHandler<UserNameChanged> { ... } public class YHandler : IEventHandler<TeamCreated> { ... } public class CachedUserLookupService : IUserLookupService, ICanRefreshCache, IEventHandler<UserNameChanged> { private Func<ISession> _sessionFactory;

Where is the composition root in a Windows Phone app?

↘锁芯ラ 提交于 2020-01-24 07:45:45
问题 I'm new to WP 7. For purposes of dependency injection, I want to adhere to practices acquired doing WinForms apps. I therefore want to build my app graph at the composition root. What part of a regular WP 7 app source code can be viewed as the composition root? 回答1: The pattern I prefer, as recommended by the Patterns & Practices team, is: Add a ViewModelLocator class to your resources App.xaml with an x:Key="ViewModelLocator" , and add to that class a property for each ViewModel type and

Should i inject Objects needed for execution of an algorithm? Should i inject everything?

百般思念 提交于 2020-01-24 05:10:29
问题 Maybe i missed it in the documentation but i'm wondering how i should handle "helper Objects"? code example: public Path dijkstra(Node startNode, Node endNode) { Set<Node> nodesToInspect = new HashSet<Node>(); // should this Object be injected? Path path = new Path(); // and this one? while (!nodesToInspect.isEmpty()) { // some logic like: path.add(currentNode); } return path; } Should i inject everything or should i say at some point that the algorithm "knows" best what it needs? Should i

Spring inject list of generic interface implementations in kotlin

☆樱花仙子☆ 提交于 2020-01-24 04:46:09
问题 Disclaimer: New to Kotlin, may be a easy to solve issue or misunderstood basics. I am trying to inject a List of a specific interface's Spring implementations, in a regular java class this have been easy like this: @Autowired List<IMyClass> myClassList; But in Kotlin doing the following gives me a error @Autowired lateinit private var myClassList: List<IMyClass<Any>> // No beans of '? extends IMyClass<Object>' or 'List<? extends IMyClass<Object>>' types found Doing it like this: @Autowired

How to inject a dependency when testing an Android activity without a third-party framework?

主宰稳场 提交于 2020-01-24 02:58:45
问题 I want to test an Android activity CommentActivity that normally constructs and uses an instance of CommentsDataSource (both are classes that I wrote). public class CommentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { : CommentsDataSource = new CommentsDataSource(..); : } : } I'm willing to create MockCommentsDataSource myself and would like to avoid using a third-party mocking framework. (Why? Because I'm a teaching trying to reduce the amount of

Dagger singleton vs kotlin object

℡╲_俬逩灬. 提交于 2020-01-24 02:12:04
问题 To define a singleton, should I use kotlin object declaration or to make an ordinary kotlin class and inject it using dagger? In my opinion the first option is definitely easier but there may be a reason to use dagger in this situation that I'm not aware of. Option 1 (notice object keyword): object SomeUtil { // object state (properties) fun someFunction(number: Long) { // ... } } Option 2 (notice class keyword): class SomeUtil { // object state (properties) fun someFunction(number: Long) { /

How to inject service in JSF managed bean without using Spring IOC

痞子三分冷 提交于 2020-01-24 00:37:12
问题 Typically if I have to inject a service in Spring I use <bean id="mycontroller" class="com.MyController"> <property name="myService" ref="myService" /> and <bean id="myService" class="com.MyService"></bean> How to do the same when using JSF? I dont want to use two IOC containers for the beans and rather keep it in faces context itself. I have seen links such as JSF 2 inject Spring bean/service with @ManagedProperty and no xml and A problem about injecting spring bean into jsf bean . They talk