Plexus——Spring之外的IoC容器

来源:互联网 发布:中科院算法答案 编辑:程序博客网 时间:2024/05/19 12:39

Plexus是什么?它是一个IoC容器,由codehaus在管理的一个开源项目。和Spring框架不同,它并不是一个完整的,拥有各种组件的大型框架,仅仅是一个纯粹的IoC容器。本文讲解Plexus的初步使用方法。

Plexus和Maven的开发者是同一群人,可以想见Plexus和Maven的紧密关系了。由于在Maven刚刚诞生的时候,Spring还不 成熟,所以Maven的开发者决定使用自己维护的IoC容器Plexus。而由于Plexus的文档比较烂,根据社区的呼声,下一版本的Maven 3则很可能使用比较成熟的Guice框架来取代Plexus,但更换底层框架毕竟不是一件轻松的事情,所以现阶段学习了解Plexus还是很有必要的。并 且Plexus目前并未停止开发,因为它的未来还未可知。除了Maven以外,WebWork(已经与Struts合并)在底层也采用了Pleuxs。

为了学习使用Plexus,首先我们还是用Maven创建一个干净的java项目:

1mvn archetype:create /
2  -DarchetypeGroupId=org.apache.maven.archetypes /
3  -DgroupId=demo /
4  -DartifactId=plexus-demos

生成项目后,在 pom.xml 加入所需的依赖包:

1<dependencies>
2    <dependency>
3        <groupId>org.codehaus.plexus</groupId>
4        <artifactId>plexus-container-default</artifactId>
5        <version>1.0-alpha-10</version>
6    </dependency>
7</dependencies>

Plexus与Spring的IoC容器在设计模式上几乎是一致的,只是语法和描述方式稍有不同。在Plexus中,有ROLE的概念,相当于Spring中的一个Bean。我们首先来通过接口定义一个role:

01packagedemo.plexus-demos;
02

03publicinterfaceCheese
04{
05    /** The Plexus role identifier. */
06    String ROLE = Cheese.class.getName();
07

08    /**
09     * Slices the cheese for apportioning onto crackers.
10     * @param slices the number of slices
11     */
12    voidslice( intslices );
13

14    /**
15     * Get the description of the aroma of the cheese.
16     * @return the aroma
17     */
18    String getAroma();
19}

然后做一个实现类:

01packagedemo.plexus-demos;
02

03publicclassParmesanCheese
04    implementsCheese
05{
06    publicvoidslice( intslices )
07    {
08        thrownewUnsupportedOperationException( "No can do");
09    }
10

11    publicString getAroma()
12    {
13        return"strong";
14    }
15}

接下来是在xml文件中定义依赖注入关系。这一点和Spring的ApplicationContext是几乎一样的,只不过Plexus的xml描述方式稍有不同。另外,Plexus默认会读取项目的classpath的META-INF/plexus/components.xml,因此我们在项目中创建 src/main/resources/META-INF/maven目录,并将配置文件components.xml放置于此。配置文件的内容如下:

1<component-set>
2  <components>
3    <component>
4      <role>demo.plexus-demos.Cheese</role>
5      <role-hint>parmesan</role-hint>      
6      <implementation>demo.plexus-demos.ParmesanCheese</implementation>
7    </component>
8  </components>
9</component-set>

大功告成,接下来只需要做一个使用Cheese组件的类来试用一下:

01importorg.codehaus.plexus.PlexusContainer;
02importorg.codehaus.plexus.PlexusContainerException;
03

04publicclassApp
05{
06    publicstaticvoidmain(String args[]) throwsException
07    {
08        PlexusContainer container= newDefaultPlexusContainer();
09        container.dispose();
10

11        Cheese cheese = (Cheese) container.lookup( Cheese.ROLE, "parmesan");
12        System.out.println( "Parmesan is "+ cheese.getAroma() );
13    }
14}

Plexus概览

Plexus提供完整的软件栈,用于创建和执行软件项目。基于Plexus容器,应用程序可以利用面向组件的编程方式来构建模块化,容易集成和重复使用的可复用组件。
虽然Plexus跟其他控制反转(IoC)或依赖注入(DI)框架相似,比如Spring框架 ,事实上它更是一个支持如下许多功能的全面的容器:
●组件生命周期
●组件实例化战略
●嵌套容器
●组件配置
●自动布线
●组件依赖关系,并各种依赖注入技术,包括构造函数注入,setter注入和私人领域的注入。
Plexus框架内置了一些比较常用开发组件和工具集,如Jetty,Velocity,Hibernate,国际化等。Plexus可以利用你为其他IoC框架编写的组件,如:Spring、Avalon和Pico,而不需要做任何的修改,就跟在Plexus中使用已经存在的代码一样。
Plexus内置一个应用服务器,可以运行您的应用,这使得构建一个可独立运行并具有像执行服务这类的一般性功能的 发布版本变得相当简单 。然而,通过将Plexus容器嵌入到其他的应用中Plexus应用能够运行在任何环境,包括Java EE应用程序或Web应用程序。
Plexus组件不需要Java编写,而是使用已经存在的针对Jython, JRuby, Beanshell, 和Groovy的组件工厂。
Pleuxs容器在很多框架和项目中被使用,尤其在Maven2.0和WebWork 2.2(也就是strusts2框架)中。
0 0
原创粉丝点击