Femtocontainer — The IoC container built into the JDK

FROM:http://www.javarants.com/2005/02/16/femtocontainer-the-ioc-container-built-into-the-jdk/

 

was published on February 16th, 2005 and is listed in  Java ,  Technology

There are many many IoC containers for Java out there that claim to be the smallest. Well, today I am going to show you one that cannot be beat.
There have been an endless series of tiny IoC containers released into the Java community including  picocontainer ,  nanocontainer ,  spring framework ,microspring , etc, etc. I’m going to take the all time record with this one though. First some background.

JavaBeans were created as Java’s answer to the COM component model. There were properties, events, and methods. They were serializable so you could store them in little .ser files. They had design and runtime variations, GUI and non-GUI versions, and all manner of icons, customizers, etc. They appeared to be designed for integration with IDEs so you could bring up a palette of components and reuse them in your applications. During the time between when JavaBeans was released and when EJB was released a lot of developers, including myself (see  Jumping Beans ), created remoteable versions that could be used between VMs and without GUI representations. This was all quickly stamped out because now there was a JavaBean solution for inter-VM execution — but it didn’t look much like JavaBeans. In fact, it was its own component model that only shared the name and virtually none of the attributes. It felt a lot like annoying Java and JavaScript naming problem. During this same period of Java history a little used specification was released, known as the JavaBeans Glasgow specification. This got virtually no attention but it tried to create a  services and lifecycle system for JavaBeans , but because of the interest in EJB and the poorly written specification, it has not gotten much attention. This is an interesting aside, but because there was no IoC framework yet, it would have been difficult to use anyway.

Java version 1.3 didn’t introduce much on this side so we will skip ahead to the focus of this entry. In Java 1.4 Sun included a much needed encoding and decoding framework for “JavaBeans”. I put JavaBeans in quotations because it is actually much more powerful than that. It will decode and encode virtually any Java class you wish because of how flexible it is. Essentially, it lets you write Java code in an XML file… much like the Spring IoC Framework. Let’s look at a simple configuration using the Spring Framework and then I will show you the equivalent in the JDK:

This is a relatively straight-forward configuration file that specifies two beans, one contains the other and each of them contains a complicated property. So what is this mysterious IoC framework that we are going to use in the JDK? It is none other than  java.beans.XMLDecoder . When JavaSoft realized that serializing beans was a nasty business because of versioning issues they created this class and its brother the  XMLEncoder   to take its place. Instead of little “pickled” (nice name Sun) JavaBeans stored in .ser files you have .xml files with something that is vaguely human readable, at least as much as that Spring stuff is. So what does the translated system look like? Here we go:

Interestingly, the format for this file is slightly more powerful in some ways by being infinitely flexible, but it is missing some of the abstraction features like templates that the Spring framework has… at least I think so. I am going to be experimenting with it some more in coming days since I think I’d rather use something built into the JDK than add a ton of new frameworks. Here is the code for loading the config file:

Amazingly, Sun even has some  good documentation on the femtocontainer . I would love to hear from people what other major features within the spring xml file would be nice to see implemented using this system rather than Spring or other containers. Next I am going to be looking at how modern Java users could reuse BeanContext for all their other container needs…

### 初始化Prism框架中的IoC容器 在Prism框架中,初始化依赖注入容器(IoC Container)可以通过两种主要方式实现:使用Unity或Managed Extensibility Framework (MEF)[^2]。 #### 使用Unity作为IoC容器 当选择Unity作为依赖注入容器时,可以利用`UnityBootstrapper`类来完成大部分必要的配置工作。下面是一个简单的例子展示如何设置Unity IoC容器: ```csharp public class MyUnityBootstrapper : UnityBootstrapper { protected override DependencyObject CreateShell() { return new Shell(); } protected override void InitializeShell() { base.InitializeShell(); App.Current.MainWindow = (Window)this.Shell; App.Current.MainWindow.Show(); } } ``` 这段代码定义了一个继承自`UnityBootstrapper`的新类,并重写了两个方法——`CreateShell()`用于创建应用程序的主要窗口,而`InitializeShell()`则负责显示这个窗口[^2]。 #### 使用MEF作为IoC容器 如果偏好于采用MEF,则应考虑从`MefBootstrapper`派生自己的引导程序类。这里给出了一种基本模式来进行此类操作: ```csharp public class MyMefBootstrapper : MefBootstrapper { protected override DependencyObject CreateShell() { return new Shell(); } protected override void InitializeShell() { base.InitializeShell(); App.Current.MainWindow = (Window)this.Shell; App.Current.MainWindow.Show(); } protected override CompositionContainer GetContainer() { var catalog = new AggregateCatalog( AssemblySource.Instance.Select(a => new AssemblyCatalog(a)).OfType<ComposablePartCatalog>()); return new CompositionContainer(catalog); } } ``` 此示例展示了怎样通过覆盖`GetContainer()`方法来自定义组成目录以及返回一个新的组合容器实例。 无论是哪种情况,在启动应用之前都需要调用相应的bootstrapper对象上的`Run()`方法以执行整个初始化过程[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值