论坛首页 Java企业应用论坛

自己动手写一个Spring (Spring 到底是怎么跑起来的)【20090708更新】

浏览 45444 次
该帖已经被评为精华帖
作者 正文
   发表时间:2009-06-30   最后修改:2009-07-01
其实Spring把底层的Aop部分源代码看一下,就能明白其基本框架的作用。
前面的Ioc是基于反射的,这两部分是关键。弄懂后,就可以自己造轮子了。。
0 请登录后投票
   发表时间:2009-07-01  
写的不错,只是看完之后有点云里雾里的
0 请登录后投票
   发表时间:2009-07-01  
mgoann 写道
spring有三种注入方式,其中以接口注入为亮点,楼主应该将接口注入分析一下具体的实现原理!



只听说过方法注入,构造函数注入,接口注入式什么意思?
0 请登录后投票
   发表时间:2009-07-02  
建议想学DI工具的看看MICRO SPRING .很简单的.
0 请登录后投票
   发表时间:2009-07-02  
谢谢分享,学习了,研究下
0 请登录后投票
   发表时间:2009-07-02  
弱弱的说一句,对于读源码的简单分析投精华有点不至于吧.
0 请登录后投票
   发表时间:2009-07-02  
2022228 写道
mgoann 写道
spring有三种注入方式,其中以接口注入为亮点,楼主应该将接口注入分析一下具体的实现原理!



只听说过方法注入,构造函数注入,接口注入式什么意思?



没人鸟我?不知道说错了没
0 请登录后投票
   发表时间:2009-07-02  
2022228 写道
2022228 写道
mgoann 写道
spring有三种注入方式,其中以接口注入为亮点,楼主应该将接口注入分析一下具体的实现原理!



只听说过方法注入,构造函数注入,接口注入式什么意思?



没人鸟我?不知道说错了没

你说的很对,没有错的啊!
0 请登录后投票
   发表时间:2009-07-03  
其实有人写了15行代码实现Ioc的代码,不过是C#的,有兴趣的可以看看。
	public class DemoContainer
{
	public delegate object Creator(DemoContainer container);

	private readonly Dictionary<string, object> configuration 
	= new Dictionary<string, object>();
	private readonly Dictionary<Type, Creator> typeToCreator 
	= new Dictionary<Type, Creator>();

	public Dictionary<string, object> Configuration
	{
		get { return configuration; }
	}

	public void Register<T>(Creator creator)
	{
		typeToCreator.Add(typeof(T),creator);
	}

	public T Create<T>()
	{
		return (T) typeToCreator[typeof (T)](this);
	}

	public T GetConfiguration<T>(string name)
	{
		return (T) configuration[name];
	}
}

	DemoContainer container = new DemoContainer();
//registering dependecies
container.Register<IRepository>(delegate
{
	return new NHibernateRepository();
});
container.Configuration["email.sender.port"] = 1234;
container.Register<IEmailSender>(delegate
{
	return new SmtpEmailSender(container.GetConfiguration<int>("email.sender.port"));
});
container.Register<LoginController>(delegate
{
	return new LoginController(
		container.Create<IRepository>(),
		container.Create<IEmailSender>());
});

//using the container
Console.WriteLine(
	container.Create<LoginController>().EmailSender.Port
	);

0 请登录后投票
   发表时间:2009-07-03  
还是不能理解 spring 的作用
为什么不是这样呢
public static void main(String[] args) {  
       Animal animal = new Animal();
        animal.say();  
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics