今天完成了IOC对Spring的集成,不论是从WEB配置中读取还是从自定义中读取

这个是该框架配置方式
文件正常配置方式
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE remote-beas PUBLIC
"-//JAVAZONE//DTD Javazone Remote Object Invoker//EN"
"http://www.javazone.org/jroi/dtd/jroi10.dtd"
>
<remote-beans>

    
<spring-config>

        
<config name="application.context.type" value="classpath_xml"/>

        
<config name="application.context.location" value="spring-config.xml"/>

    
</spring-config>

    
<bean id="Test1" class="testBean"  scope="spring">
    
</bean>

</remote-beans>

注意,加入了spring-config的元素

application.context.type是根据Spring的方式划分的,其他的值还有filesystem_xml以及web

注意,spring-config.xml放在src下,或者说是classpath下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
              "http://www.springframework.org/dtd/spring-beans-2.0.dtd"
>
<beans>

    
<bean id="testBean" class="org.javazone.jroi.example.Test1" />

</beans>
这个是Spring的配置,没有任何修改或变动,这个不对spring的配置做任何改动

看一下Test1这个类
/**
 * 
 
*/

package org.javazone.jroi.example;


public class Test1
{

    @Override
    
public String toString()
    
{
        
return "class org.javazone.jroi.example.Test1 : hashcode = " + hashCode();
    }

}


这里打印出的hashCode是为了测试scope
下面是结果
DEBUG[2010-01-27 00:38:34:514][main](XMLClassPathSystemContext.java:47- parse xml document
DEBUG
[2010-01-27 00:38:34:569][main](XMLClassPathSystemContext.java:52- initialize system properties constructor
DEBUG
[2010-01-27 00:38:34:574][main](XMLClassPathSystemContext.java:57- initialize bean metadata constructor
DEBUG
[2010-01-27 00:38:34:579][main](XMLBeanMetasConstructor.java:38- construct bean metas
DEBUG
[2010-01-27 00:38:34:589][main](XMLSpringPropertiesConstructor.java:36- construct properties
DEBUG
[2010-01-27 00:38:34:589][main](XMLClassPathSystemContext.java:68- initialize IOC Container
INFO
[2010-01-27 00:38:34:731][main](AbstractApplicationContext.java:411- Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1833955: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1833955]; startup date [Wed Jan 27 00:38:34 CST 2010]; root of context hierarchy
INFO
[2010-01-27 00:38:34:846][main](XmlBeanDefinitionReader.java:323- Loading XML bean definitions from class path resource [spring-config.xml]
DEBUG
[2010-01-27 00:38:34:846][main](DefaultDocumentLoader.java:72- Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
DEBUG
[2010-01-27 00:38:34:851][main](BeansDtdResolver.java:72- Found beans DTD [http://www.springframework.org/dtd/spring-beans-2.0.dtd] in classpath: spring-beans-2.0.dtd
DEBUG
[2010-01-27 00:38:34:876][main](DefaultBeanDefinitionDocumentReader.java:86- Loading bean definitions
DEBUG
[2010-01-27 00:38:34:911][main](AbstractBeanDefinitionReader.java:185- Loaded 1 bean definitions from location pattern [spring-config.xml]
INFO
[2010-01-27 00:38:34:911][main](AbstractApplicationContext.java:426- Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1833955]: org.springframework.beans.factory.support.DefaultListableBeanFactory@c53dce
DEBUG
[2010-01-27 00:38:34:911][main](AbstractApplicationContext.java:430- 1 beans defined in org.springframework.context.support.ClassPathXmlApplicationContext@1833955: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1833955]; startup date [Wed Jan 27 00:38:34 CST 2010]; root of context hierarchy
DEBUG
[2010-01-27 00:38:34:931][main](AbstractApplicationContext.java:648- Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1dff3a2]
DEBUG
[2010-01-27 00:38:34:931][main](AbstractApplicationContext.java:672- Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@87a5cc]
INFO
[2010-01-27 00:38:34:936][main](DefaultListableBeanFactory.java:414- Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c53dce: defining beans [testBean]; root of factory hierarchy
DEBUG
[2010-01-27 00:38:34:936][main](DefaultSingletonBeanRegistry.java:214- Creating shared instance of singleton bean 'testBean'
DEBUG
[2010-01-27 00:38:34:941][main](AbstractAutowireCapableBeanFactory.java:383- Creating instance of bean 'testBean'
DEBUG
[2010-01-27 00:38:34:996][main](AbstractAutowireCapableBeanFactory.java:459- Eagerly caching bean 'testBean' to allow for resolving potential circular references
DEBUG
[2010-01-27 00:38:34:998][main](AbstractAutowireCapableBeanFactory.java:411- Finished creating instance of bean 'testBean'
DEBUG
[2010-01-27 00:38:35:001][main](BeanContainer.java:65- get bean 'Test1' from container
DEBUG
[2010-01-27 00:38:35:002][main](AbstractBeanFactory.java:214- Returning cached instance of singleton bean 'testBean'
class org.javazone.jroi.example.Test1 : hashcode 
= 11423854

注意看最下面一句(log,使用的是LOG4J)


WEB方式的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE remote-beas PUBLIC
"-//JAVAZONE//DTD Javazone Remote Object Invoker//EN"
"http://www.javazone.org/jroi/dtd/jroi10.dtd"
>
<remote-beans>

    
<spring-config>

        
<config name="application.context.type" value="web"/>

    
</spring-config>

    
<bean id="Test1" class="testBean"  scope="spring">
    
</bean>

</remote-beans>
这个只有一点点变化,这里的变化在于value的变化,以及没有了location

<listener>
    
<listener-class>org.javazone.jroi.web.SpringContextListener</listener-class>
  
</listener>
  
<context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>classpath:spring-config.xml</param-value>
  
</context-param>
在web.xml中加入SpringContextListener的东东
注意这个东东的配置方式,不是使用的Spring自带的Listener,注意!

package org.javazone.jroi.example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javazone.jroi.cfg.xml.XMLClassPathSystemContext;
import org.javazone.jroi.ioc.IoContainer;
import org.javazone.jroi.web.SpringWebContext;

/**
 * Servlet implementation class ExampleServlet
 
*/

public class ExampleServlet
    
extends HttpServlet
{
    
private static final long serialVersionUID = 1L;

    
public ExampleServlet()
    
{
        
super();
    }


    
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    
{
        doService(request, response);
    }


    
private void doService(HttpServletRequest request, HttpServletResponse response)
    
{
        IoContainer ioContainer 
= XMLClassPathSystemContext.configuration(
                
"org/javazone/jroi/example/jroi.config.xml").getIoContainer();

        System.out.println(ioContainer.getBean(
"Test1"));
        System.out.println(ioContainer.getBean(
"Test2"));
    }


    
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    
{
        doService(request, response);
    }


}
测试用的Servlet
下面是结果,其实是一样的

2010-1-27 0:49:24 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance 
in production environments was not found on the java.library.path: G:\Eclipse JEE 3.4\jre\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;G:/Eclipse JEE 3.5/jre/bin/client;G:/Eclipse JEE 3.5/jre/bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files (x86)\Rational\common;C:\Program Files\Common Files\Microsoft Shared\Windows Live
2010-1-27 0:49:24 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: 
[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:org.javazone.jroi' did not find a matching property.
2010-1-27 0:49:24 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP
/1.1 on http-8080
2010-1-27 0:49:24 org.apache.catalina.startup.Catalina load
信息: Initialization processed 
in 695 ms
2010-1-27 0:49:24 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2010-1-27 0:49:24 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat
/6.0.24
2010-1-27 0:49:25 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
DEBUG
[2010-01-27 00:49:25:511][main](SpringContextListener.java:30- create spring context listener
INFO
[2010-01-27 00:49:25:521][main](ContextLoader.java:189- Root WebApplicationContext: initialization started
INFO
[2010-01-27 00:49:25:619][main](AbstractApplicationContext.java:411- Refreshing org.springframework.web.context.support.XmlWebApplicationContext@11ca803: display name [Root WebApplicationContext]; startup date [Wed Jan 27 00:49:25 CST 2010]; root of context hierarchy
INFO
[2010-01-27 00:49:25:781][main](XmlBeanDefinitionReader.java:323- Loading XML bean definitions from class path resource [spring-config.xml]
DEBUG
[2010-01-27 00:49:25:799][main](DefaultDocumentLoader.java:72- Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
DEBUG
[2010-01-27 00:49:25:813][main](BeansDtdResolver.java:72- Found beans DTD [http://www.springframework.org/dtd/spring-beans-2.0.dtd] in classpath: spring-beans-2.0.dtd
DEBUG
[2010-01-27 00:49:25:829][main](DefaultBeanDefinitionDocumentReader.java:86- Loading bean definitions
DEBUG
[2010-01-27 00:49:25:878][main](AbstractBeanDefinitionReader.java:185- Loaded 1 bean definitions from location pattern [classpath:spring-config.xml]
INFO
[2010-01-27 00:49:25:879][main](AbstractApplicationContext.java:426- Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@11ca803]: org.springframework.beans.factory.support.DefaultListableBeanFactory@16acdd1
DEBUG
[2010-01-27 00:49:25:879][main](AbstractApplicationContext.java:430- 1 beans defined in org.springframework.web.context.support.XmlWebApplicationContext@11ca803: display name [Root WebApplicationContext]; startup date [Wed Jan 27 00:49:25 CST 2010]; root of context hierarchy
DEBUG
[2010-01-27 00:49:25:928][main](AbstractApplicationContext.java:648- Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@b60b93]
DEBUG
[2010-01-27 00:49:25:937][main](AbstractApplicationContext.java:672- Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@100ebec]
DEBUG
[2010-01-27 00:49:25:944][main](UiApplicationContextUtils.java:85- Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@1938039]
INFO
[2010-01-27 00:49:25:946][main](DefaultListableBeanFactory.java:414- Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16acdd1: defining beans [testBean]; root of factory hierarchy
DEBUG
[2010-01-27 00:49:25:959][main](DefaultSingletonBeanRegistry.java:214- Creating shared instance of singleton bean 'testBean'
DEBUG
[2010-01-27 00:49:25:962][main](AbstractAutowireCapableBeanFactory.java:383- Creating instance of bean 'testBean'
DEBUG
[2010-01-27 00:49:26:076][main](AbstractAutowireCapableBeanFactory.java:459- Eagerly caching bean 'testBean' to allow for resolving potential circular references
DEBUG
[2010-01-27 00:49:26:078][main](AbstractAutowireCapableBeanFactory.java:411- Finished creating instance of bean 'testBean'
DEBUG
[2010-01-27 00:49:26:079][main](ContextLoader.java:204- Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
INFO
[2010-01-27 00:49:26:079][main](ContextLoader.java:209- Root WebApplicationContext: initialization completed in 558 ms
DEBUG
[2010-01-27 00:49:26:080][main](SpringContextListener.java:36- initialize spring web application context
2010-1-27 0:49:26 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP
/1.1 on http-8080
2010-1-27 0:49:26 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening 
on /0.0.0.0:8009
2010-1-27 0:49:26 org.apache.jk.server.JkMain start
信息: Jk running ID
=0 time=0/96  config=null
2010-1-27 0:49:26 org.apache.catalina.startup.Catalina start
信息: Server startup 
in 1620 ms
DEBUG
[2010-01-27 00:49:37:635][http-8080-1](XMLClassPathSystemContext.java:47- parse xml document
DEBUG
[2010-01-27 00:49:37:651][http-8080-1](XMLClassPathSystemContext.java:52- initialize system properties constructor
DEBUG
[2010-01-27 00:49:37:656][http-8080-1](XMLClassPathSystemContext.java:57- initialize bean metadata constructor
DEBUG
[2010-01-27 00:49:37:666][http-8080-1](XMLBeanMetasConstructor.java:38- construct bean metas
DEBUG
[2010-01-27 00:49:37:676][http-8080-1](XMLSpringPropertiesConstructor.java:36- construct properties
DEBUG
[2010-01-27 00:49:37:676][http-8080-1](XMLClassPathSystemContext.java:68- initialize IOC Container
DEBUG
[2010-01-27 00:49:37:719][http-8080-1](BeanContainer.java:65- get bean 'Test1' from container
DEBUG
[2010-01-27 00:49:37:720][http-8080-1](AbstractBeanFactory.java:214- Returning cached instance of singleton bean 'testBean'
class org.javazone.jroi.example.Test1 : hashcode 
= 6781414
DEBUG
[2010-01-27 00:49:37:721][http-8080-1](BeanContainer.java:65- get bean 'Test2' from container
WARN
[2010-01-27 00:49:37:721][http-8080-1](BeanContainer.java:74- the bean named 'Test2' was not be found
null
上面的所有都是log,所以只需要看最下面几行

OK,今天的工作告一段落