订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
java

IOC XML配置例子

上一篇日记中介绍了直接使用IOC容器的例子,但是如果觉得那样的方式不是很便利,那么下面将介绍基于XML配置型的例子.在该IOC容器中,定义了POJO的描述元素,如构造,属性等,还定义了IOC环境下的IOC 值对象,只不可以使用XML的方式来描述它们.下面给出一个简单的例子说明它们的使用


在上篇代码基础之上,添加一个新的工厂类(factory bean),产生POJO实例.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* Copyright (c) jmin Organization. All rights reserved.
 */
package org.jmin.j2ee.ioc.test;
 
/**
 * A object factory to create instances for man class
 * @author Chris Liao
 */
public class ManFactory {
 
  /**
   * Factory method to create a man
   */
  public Man create(String name,int sex){
  	return new Man(name,sex);
  }
}


好了,我们给出这个应用的XML(POJO.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0"?>
<service id="POJO">
        <components>
 
                <component id="man1" type="bean">
                        <class>org.jmin.j2ee.ioc.test.Man</class>
                        <singleton>false</singleton>
                        <destroy-method>destroy</destroy-method>
                        <constructor>
                                <constructor-arg value="Chris"/>
                                <constructor-arg value="28"/>
                        </constructor>
                        <property name="age" value="28"/>
                        <invocation>
                                <method-name>sayHello</method-name>
                                <method-param-values>
                                        <method-param-value value="China"/>
                                </method-param-values>
                        </invocation>
                        <interception>
                                <method-name>sayHello</method-name>
                                <method-param-types>
                                        <method-param-type>java.lang.String</method-param-type>
                                </method-param-types>
                                <interceptor class="org.jmin.j2ee.ioc.test.ManInterceptor"/>
                        </interception>
                </component>
 
                <component id="man2" type="bean">
                        <class>org.jmin.j2ee.ioc.test.Man</class>
                        <factory-method>create</factory-method>
                        <singleton>false</singleton>
                        <constructor>
                                <constructor-arg value="Chris"/>
                                <constructor-arg value="28"/>
                        </constructor>
                        <property name="age" value="28"/>
                        <invocation>
                                <method-name>sayHello</method-name>
                                <method-param-values>
                                        <method-param-value value="China"/>
                                </method-param-values>
                        </invocation>
                </component>
 
                <component id="man3" type="bean">
                		<class>org.jmin.j2ee.ioc.test.Man</class>
                        <factory-bean>factory</factory-bean>
                        <factory-method>create</factory-method>
                        <singleton>false</singleton>
                        <constructor>
                                <constructor-arg value="Chris"/>
                                <constructor-arg value="28"/>
                        </constructor>
                        <property name="age" value="28"/>
                        <invocation>
                                <method-name>sayHello</method-name>
                                <method-param-values>
                                        <method-param-value value="China"/>
                                </method-param-values>
                        </invocation>
                </component>
 
                <component id="factory" type="bean">
                        <class>org.jmin.j2ee.ioc.test.ManFactory</class>
                </component>
        </components>
</service>




好了,接下来只用如何通过Context使用这个XML文件了,在Framework中,定义了三种context
ClassPathAppContext, FilePathAppContext , URLAppContext. 在目前例子中使用最后者,那是因为我们已经假定上面的POJO.xml文件是和这些类定义在一起.下面代码将获取三个POJO实例.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 /*
* Copyright (c) jmin Organization. All rights reserved.
 */
package org.jmin.j2ee.ioc.test;
 
import java.net.URL;
 
import org.jmin.j2ee.ioc.context.URLAppContext;
 
/**
 * XML IOC tester
 *
 * @author Chris Liao
 */
 
public class ManXMLIocTest {
 
  /**
   * main method
   */
  public static void main(String args[]) throws Exception {
 
    /**
     * Get resource URL
     */
    URL fileURL = (ManXMLIocTest.class).getResource("POJO.xml");
 
    /**
     * Create a IOC URL context
     */
    URLAppContext context = new URLAppContext(fileURL);
 
    /**
     * print a ioc bean with ID: man1
     */
    System.out.println("bean1: " + context.getBean("man1"));
 
    /**
     * print a ioc bean with ID: man2
     */
    System.out.println("bean2: " + context.getBean("man2"));
 
    /**
     * print a ioc bean with ID: man3
     */
    System.out.println("bean3: " + context.getBean("man3"));
  }
}
 




最后编译运行它们:

before

Hello,China

returning: null

fianlly: null

bean1: org.jmin.j2ee.ioc.test.Man@9f671b

Hello,China

bean2: org.jmin.j2ee.ioc.test.Man@12a3793

Hello,China

bean3: org.jmin.j2ee.ioc.test.Man@a6d51e

destroy



最新文档
下载该IOC工具

任何问题请联系 jmin520@21cn.com

 

平均得分
(0 次评分)





文章来自: 本站原创
标签:
评论: 4 | 查看次数: 520
  • 共有 4 条评论
游客 [2008-08-27 13:29:42]
游客 [2008-06-05 21:23:15]
支持。。。
游客 [2008-05-12 21:39:29]
可能大家没看清楚,你需要明确告诉大家,这个是一个框架,才会得到更多人的注意,不然人家还以为你学习某个IOC容器呢.
游客 [2008-05-12 21:30:07]
我也来凑凑热闹! 支持!
  • 共有 4 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启