constructor-injection

Spring constructor injection of SLF4J logger - how to get injection target class?

不羁的心 提交于 2019-11-30 01:59:58
I'm trying to use Spring to inject a SLF4J logger into a class like so: @Component public class Example { private final Logger logger; @Autowired public Example(final Logger logger) { this.logger = logger; } } I've found the FactoryBean class, which I've implemented. But the problem is that I cannot get any information about the injection target: public class LoggingFactoryBean implements FactoryBean<Logger> { @Override public Class<?> getObjectType() { return Logger.class; } @Override public boolean isSingleton() { return false; } @Override public Logger getObject() throws Exception { return

Constructor injection of a View Model instance used as an Action method parameter

百般思念 提交于 2019-11-29 07:09:19
When a view model is created you can populate the options (e.g. used in a dropdown list) into a setter property of the view model. The problem is that when that view model is later passed as a parameter (by the framework!) into an action method, those property values has not become automagically repopulated, so if you need to redisplay the form because of validation errors, you need to repopulate those options again. One potential solution, which I am asking for specifically in this question, is how to make the MVC framework instantiate the view model with constructor injection, which would

Constructor Injection in C#/Unity?

假装没事ソ 提交于 2019-11-27 19:08:48
I'm using C# with Microsoft's Unity framework. I'm not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity. My problem can be summed up using the following example code: class Train(Person p) { ... } class Bus(Person p) { ... } class Person(string name) { ... } Person dad = new Person("joe"); Person son = new Person("timmy"); When I call the resolve method on Bus how can I be sure that the Person 'son' with the name 'timmy' is injected and when resolving Train how can I be sure that Person 'dad' with then name 'joe' is resolved? I'm

MEF Constructor Injection

落花浮王杯 提交于 2019-11-27 09:50:39
问题 I'm trying to figure out MEF's Constructor Injection attribute. I have no idea how I tell it to load the constructor's parameters. This is the property I'm trying to load [ImportMany(typeof(BUsers))] public IEnumerable<BUsers> LoadBUsers { get; set; } Here is the code I'm using to import the assemblies. try { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly())); catalog.Catalogs.Add(new DirectoryCatalog("DI")); var

Spring overloaded constructor injection

老子叫甜甜 提交于 2019-11-27 07:58:04
问题 This is the code : public class Triangle { private String color; private int height; public Triangle(String color,int height){ this.color = color; this.height = height; } public Triangle(int height ,String color){ this.color = color; this.height = height; } public void draw() { System.out.println("Triangle is drawn , + "color:"+color+" ,height:"+height); } } The Spring config-file is : <bean id="triangle" class="org.tester.Triangle"> <constructor-arg value="20" /> <constructor-arg value="10"

Constructor Injection in C#/Unity?

醉酒当歌 提交于 2019-11-26 19:46:17
问题 I'm using C# with Microsoft's Unity framework. I'm not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity. My problem can be summed up using the following example code: class Train(Person p) { ... } class Bus(Person p) { ... } class Person(string name) { ... } Person dad = new Person("joe"); Person son = new Person("timmy"); When I call the resolve method on Bus how can I be sure that the Person 'son' with the name 'timmy' is

Can I pass constructor parameters to Unity&#39;s Resolve() method?

假装没事ソ 提交于 2019-11-26 15:14:19
I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve<IDataContext>(); var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(context2); RepositoryA and RepositoryB both have a constructor that takes an IDataContext parameter, and I want Unity to