property-injection

How to inject dependency property using Ioc Unity

百般思念 提交于 2019-12-09 15:06:50
问题 I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { return "MethodA1() " +serviceB.MethodB1(); } } public class ServiceB : IServiceB { public string MethodB1() { return "MethodB1() "; } } I use Unity for IoC, my registration looks like this: container.RegisterType<IServiceA, ServiceA>(); container.RegisterType<IServiceB,

Spring @Autowired and @Value on property not working

。_饼干妹妹 提交于 2019-12-05 07:44:56
I would like to use @Value on a property but I always get 0 (on int). But on a constructor parameter it works. Example: @Component public class FtpServer { @Value("${ftp.port}") private int port; public FtpServer(@Value("${ftp.port}") int port) { System.out.println(port); // 21, loaded from the application.properties. System.out.println(this.port); // 0??? } } The object is spring managed, else the constructor parameter wouldn't work. Does anyone know what causes this weird behavior? Field injection is done after objects are constructed since obviously the container cannot set a property of

How can I get Castle Windsor to automatically inject a property?

萝らか妹 提交于 2019-12-04 19:53:30
问题 I have a property on my classes for logging service. private ILogger logger = NullLogger.Instance; public ILogger Logger { get { return logger; } set { logger = value; } } And I have this in my component registration: container.AddFacility<LoggingFacility>(x => new LoggingFacility(LoggerImplementation.Log4net)); However, Windsor doesn't seem to inject the Logger - am I missing something? 回答1: The lambda parameter for AddFacility is actually a creation callback (it gets called when the

How to inject dependency property using Ioc Unity

筅森魡賤 提交于 2019-12-04 01:47:17
I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { return "MethodA1() " +serviceB.MethodB1(); } } public class ServiceB : IServiceB { public string MethodB1() { return "MethodB1() "; } } I use Unity for IoC, my registration looks like this: container.RegisterType<IServiceA, ServiceA>(); container.RegisterType<IServiceB, ServiceB>(); When I resolve a ServiceA instance, serviceB will be null . How can I resolve this? You have at

How can I get Castle Windsor to automatically inject a property?

元气小坏坏 提交于 2019-12-03 14:14:23
I have a property on my classes for logging service. private ILogger logger = NullLogger.Instance; public ILogger Logger { get { return logger; } set { logger = value; } } And I have this in my component registration: container.AddFacility<LoggingFacility>(x => new LoggingFacility(LoggerImplementation.Log4net)); However, Windsor doesn't seem to inject the Logger - am I missing something? The lambda parameter for AddFacility is actually a creation callback (it gets called when the facility is created), not a factory. Use this instead: container.AddFacility("logging", new LoggingFacility

How to inject dependency name as a constructor parameter

谁都会走 提交于 2019-11-30 21:34:35
Using Autofac, I can register a class to resolve against an interface using property injection, using the following code: builder.RegisterType<Log4NetAdapter>() .As<ILogger>() .PropertiesAutowired() .InstancePerDependency(); However, my Log4NetAdapter class has a constructor parameter that requires the name of the calling class. This way, I can log events based upon the name of the calling class. public class Log4NetAdapter : ILogger { private readonly ILog _logger; public Log4NetAdapter(string logName) { _logger = LogManager.GetLogger(logName); } ... } How can I inject the name (i.e. typeof

How to inject dependency name as a constructor parameter

这一生的挚爱 提交于 2019-11-30 17:07:45
问题 Using Autofac, I can register a class to resolve against an interface using property injection, using the following code: builder.RegisterType<Log4NetAdapter>() .As<ILogger>() .PropertiesAutowired() .InstancePerDependency(); However, my Log4NetAdapter class has a constructor parameter that requires the name of the calling class. This way, I can log events based upon the name of the calling class. public class Log4NetAdapter : ILogger { private readonly ILog _logger; public Log4NetAdapter

Windsor Container: How to specify a public property should not be filled by the container?

风流意气都作罢 提交于 2019-11-30 11:59:06
When Instantiating a class, Windsor by default treats all public properties of the class as optional dependencies and tries to satisfy them. In my case, this creates a rather complicated circular dependency which causes my application to hang. How can I explicitly tell Castle Windsor that it should not be trying to satisfy a public property? I assume there must be an attribute to that extent. I can't find it however so please let me know the appropriate namespace/assembly. If there is any way to do this without attributes (such as Xml Configuration or configuration via code) that would be