dependency-injection

How do I use custom model binder that supports dependency injection in ASP.NET Core?

人盡茶涼 提交于 2020-01-03 13:39:15
问题 I am trying to use a custom model binder in MVC that I want resolved from my IoC container. The issue I am having is that I can't access my container while I am adding the MVC service, because my container isn't built yet (and I need to add MVC before building my container). Feels like a chicken/egg issue, and I am sure I am missing a simple solution. Example: services.AddMvc().AddMvcOptions(options => { options.ModelBinders.Add(serviceProvider.Resolve<CustomModelBinder>()); }); My custom

Depedency injection: injecting partially-initialized objects

拜拜、爱过 提交于 2020-01-03 10:42:06
问题 This question is about Unity Container but I guess it is applicable to any dependency container. I have two classes with circular dependencies: class FirstClass { [Dependency] public SecondClass Second { get; set; } } class SecondClass { public readonly FirstClass First; public SecondClass(FirstClass first) { First = first; } } Technically it's possible to instantiate and correctly inject dependencies for both of them if treat them as singletons: var firstObj = new FirstClass(); var secondObj

How to create a Spring bean from a static inner class constructor?

百般思念 提交于 2020-01-03 08:55:10
问题 I am trying to use the Spring Framework IoC Container to create an instance of class ThreadPoolExecutor.CallerRunsPolicy. In Java, I'd do it this way... import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; ... RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); But when I try to do the equivalent in Spring, it throws a CannotLoadBeanClassException . <beans> <bean class="java.util.concurrent

How to inject validator in Symfony

最后都变了- 提交于 2020-01-03 07:32:14
问题 Can somebody show me how I would inject validator into a regular class using dependency injection. In my controller I have : use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Form; class FormController extends Controller { public function indexAction() { $form = new Form(); $email = $request->request->get('email'); $valid = $form->isValid($email); } } and I want to use this custom class - but I need it got have access to validator. class Form { public function isValid($value) { //

How do you inject jdbiFactory DAOs into a Dropwizard Command?

有些话、适合烂在心里 提交于 2020-01-03 07:29:44
问题 I'm starting to work with Dropwizard and I'm trying to create a Command that requires to use the database. If someone is wondering why I'd want to do that, I can provide good reasons, but this is not the point of my question anyway. It's about dependency inversion and Service initialization and run phases in Dropwizard. Dropwizard encourages to use its DbiFactory to build DBI instances but in order to get one, you need an Environment instance and/or the database configuration: public class

Injecting Androidx Fragments using Dagger 2

隐身守侯 提交于 2020-01-03 07:06:08
问题 I want to inject my Androidx fragments using dagger 2. In my activity I have: public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject Reposetory reposetory; @Inject DispatchingAndroidInjector<androidx.fragment.app.Fragment> dispatchingAndroidInjector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public AndroidInjector<androidx.fragment.app

Call some methods from interface without override all the methods in JAVA

混江龙づ霸主 提交于 2020-01-03 07:05:49
问题 Friends, I have an issue in Java: I'd like to implement one structure but I'm facing some difficulty in doing it, can anyone help me. interface samp1{ method1() method2() method3() } interface samp2{ method4() method5() } class Samp implements samp1,samp2 { // this class needs only method1 from interface samp1 and method 4 from interface samp2 // I don't want to override all the methods from interface } can anyone propose some solutions for this? Is there any design pattern available for this

Castle Windsor DI installer: dependency factory method has nested dependency on ApiController property

你说的曾经没有我的故事 提交于 2020-01-03 05:18:08
问题 I am trying to implement DI with Castle Windsor. Currently I have a controller with overloaded constructors like this (this is an antipattern as described here: https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=97): public class MyController : ApiController { protected IStorageService StorageService; protected MyController() { StorageService = StorageServiceFactory.CreateStorageService(User.Identity as ClaimsIdentity); } protected MyController(IStorageService storageService) {

How to inject String property in a spring bean

梦想与她 提交于 2020-01-03 04:51:23
问题 We inject simple property with configuration as shown below : <bean id="myService" class="com.aaa.bbb.ccc.MyServiceImpl"> <property name="myProp" value=""/> </bean> How would you do it with annotations? 回答1: Using the @Value annotation. It supports SpEL as well, which means you can load a properties file and have @Value("${someConfigurationProperty}") 来源: https://stackoverflow.com/questions/6630965/how-to-inject-string-property-in-a-spring-bean

EJB injection into Restful service (NullPointerException)

柔情痞子 提交于 2020-01-03 04:14:10
问题 I' have a problem with injecting StoreBean into Restful service. I have reduecd code little bit to get a point. Here is the code(DataStoreBean): @Local(DataStore.class) @Stateless public class DataStoreBean implements DataStore { public String get() { return "works"; } } and here is code for RestEndpoint: @RequestScoped @Path("/DataStore") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class DataStoreRestEndpoint { @EJB DataStoreBean dataStore; @GET @Path("