IOC XML配置例子
作者:older 日期:2008-05-07 19:05:56
上一篇日记中介绍了直接使用IOC容器的例子,但是如果觉得那样的方式不是很便利,那么下面将介绍基于XML配置型的例子.在该IOC容器中,定义了POJO的描述元素,如构造,属性等,还定义了IOC环境下的IOC 值对象,只不可以使用XML的方式来描述它们.下面给出一个简单的例子说明它们的使用
在上篇代码基础之上,添加一个新的工厂类(factory bean),产生POJO实例.
好了,我们给出这个应用的XML(POJO.xml)
好了,接下来只用如何通过Context使用这个XML文件了,在Framework中,定义了三种context
ClassPathAppContext, FilePathAppContext , URLAppContext. 在目前例子中使用最后者,那是因为我们已经假定上面的POJO.xml文件是和这些类定义在一起.下面代码将获取三个POJO实例.
最后编译运行它们:
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
在上篇代码基础之上,添加一个新的工厂类(factory bean),产生POJO实例.
1 | /*
* 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 |
<?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 |
/*
* 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 | 查看次数: 521
- 共有 4 条评论
- 共有 4 条评论
发表评论
订阅
上一篇
|

文章来自:
标签: 





gtg08y
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
泵
泵
泵
泵
水泵
水泵
水泵
水泵
水泵
齿轮输油泵
齿轮输油泵
环保设备
环保设备
China Travel
China Tours
China Tours
beijing Tours
beijing Tours
beijing Travel
beijing Travel
shanghai Tours
shanghai Tours
shanghai Travel
shanghai Travel
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
泵
水泵
齿轮输油泵
环保设备