依赖注入怎么写( 三 )


上面是依赖注入的所有内容 。
3.如何编写一个简单的依赖注入容器//容器和示例的完整代码如下 public interface ILogWriter { void Write(string text); } public class MyLogWriter : ILogWriter { public void Write(string str) { Console.WriteLine(str); } } public interface ILogger { void Log(string message); } public class MyLogger : ILogger { ILogWriter _writer; public MyLogger(ILogWriter writer) { _writer = writer; } public void Log(string message) { _writer.Write("[ Log ] " + message); } } static void Main(string[] args) { var container = new Container(); container.Register(); container.Register(); var logger = container.Resolve(); logger.Log("asdasdas"); } public class Container { private IDictionary, IList>> Factories { get; set; } public Container() { Factories = new Dictionary, IList>>(); } private Func WrapFactory(Func originalFactory, bool singleton) { if (!singleton) return originalFactory; object value = http://www.xuexi88.com/zhishi/null; return () => { if (value =http://www.xuexi88.com/zhishi/= null) value = originalFactory(); return value; }; } private Func BuildFactory(Type type) { // 获取类型的构造函数 var constructor = type.GetConstructors().FirstOrDefault(); // 生成构造函数中的每个参数的表达式 var argumentExpressions = new List(); foreach (var parameter in constructor.GetParameters()) { var parameterType = parameter.ParameterType; if (parameterType.IsGenericType && parameterType.() == typeof(IEnumerable<>)) { // 等于调用this.ResolveMany(); argumentExpressions.Add(Expression.Call( Expression.Constant(this), "ResolveMany", parameterType.GetGenericArguments(), Expression.Constant(null, typeof(string)))); } else { // 等于调用this.Resolve(); argumentExpressions.Add(Expression.Call( Expression.Constant(this), "Resolve", new [] { parameterType }, Expression.Constant(null, typeof(string)))); } } // 构建new表达式并编译到委托 var newExpression = Expression.New(constructor, argumentExpressions); return Expression.Lambda>(newExpression).Compile(); } public void Register(string serviceKey = null, bool singleton = false) where TImplementation : TService { var key = Tuple.Create(typeof(TService), serviceKey); IList> factories; if (!Factories.TryGetValue(key, out factories)) { factories = new List>(); Factories[key] = factories; } var factory = BuildFactory(typeof(TImplementation)); WrapFactory(factory, singleton); factories.Add(factory); } public TService Resolve(string serviceKey = null) { var key = Tuple.Create(typeof(TService), serviceKey); var factory = Factories[key].Single(); return (TService)factory(); } public IEnumerable ResolveMany(string serviceKey = null) { var key = Tuple.Create(typeof(TService), serviceKey); IList> factories; if (!Factories.TryGetValue(key, out factories)) { yield break; } foreach (var factory in factories) { yield return (TService)factory(); } } } 。
4.多个spring配置文件依赖注入要怎么写主要有两种方式:
1、在一个配置文件中使用import标签导入其他配置文件 , 即
applicationContext.xml中部分代码如下:
2、在web.xml中配置Spring配置文件处导入多个配置文件 , 即可
a、导入多个配置文件
web.xml部分代码如下:
contextConfigLocation
applicationContext-core.xml,
applicationContext-dao.xml,
applicationContext-service.xml,
applicationContext-action.xml
b、使用*通配符导入多个配置文件
web.xml部分代码如下:
contextConfigLocation
applicationContext-*.xml
5.什么是依赖注入依赖注入是spring框架中的解耦的一种策略 , 称为DI或IOC(控制反转) , 主要有set方式(提供set和get方法)和constractor(构造方法)方式 , 它使得类与类之间以配置文件的形式组织在一起 , 而不是硬编码的方式 , 例如classA 中用到了classB如果写代码的话是new 一个classB , 而用依赖注入的方式则是在applicationContext.xml里面写两个