Tuesday, October 5, 2010

Spring v3.0.2 Learning Note 2 - A Sample Example

  • 准备
Spring v3的发布包与之前的版本有了很大的不同,先下载spring-framework-3.0.2.RELEASE.zip和spring- framework-3.0.2.RELEASE-dependencies.zip,并解压至本地硬盘,解压后的目录标示为%SPRING% 和%SPRING_DEP%
  • 在eclipse中创建一个java项目
  • 在eclipse的'Configure Build Path...'选项中将以下依赖包配置在classpath下
          %SPRING%\dist\org.springframework.core-3.0.2.RELEASE.jar
          %SPRING%\dist\org.springframework.asm-3.0.2.RELEASE.jar
          %SPRING%\dist\org.springframework.beans-3.0.2.RELEASE.jar
          %SPRING%\dist\org.springframework.context-3.0.2.RELEASE.jar
          %SPRING%\dist\org.springframework.context.support-3.0.2.RELEASE.jar 
          %SPRING%\dist\org.springframework.expression-3.0.2.RELEASE.jar 
          %SPRING_DEP%\org.apache.commons\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1\com.springsource.org.apache.commons.logging-1.1.1.jar
  • 在eclipse中创建spring配置文件spring.xml, 在spring framework reference中搜索'<beans'找到spring配置文件的模板,并将其拷贝入spring.xml。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
  • spring容器初始化
在类路径下寻找配置文件来初始化容器:
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
or
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
或者在文件系统路径下寻找配置文件初始化容器:
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"d:\\spring.xml"})
spring还提供了其他方法初始化容器,详见参考手册。

  • 源代码
package com.spring.test.manager;

public interface IPersonService {

    public abstract void save();

}

------------------------------------------------
package com.spring.test.manager.impl;

import com.spring.test.manager.IPersonService;

public class PersonManager implements IPersonService {
    /* (non-Javadoc)
     * @see com.spring.test.manager.IPersonService#save()
     */
    public void save() {
        System.out.println("This is save() method.");
    }
}
  • 配置spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="personService" class="com.spring.test.manager.impl.PersonManager"/>

</beans>

注意:id属性与name属性,都是标示一个bean,但是id属性受限于xml解析器的限制,不可以包含一些特殊字符,类似于这样的值"\sss\aaa",id不允许,但是name允许。
  • 在eclipse创建一个junit test
package com.spring.test.junit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test.manager.IPersonService;

public class PersonServiceTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");
        IPersonService personService = (IPersonService) ctx
                .getBean("personService");
        personService.save();

    }

}
运行结果:
Oct 5, 2010 5:29:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@93dcd: startup date [Tue Oct 05 17:29:51 CST 2010]; root of context hierarchy
Oct 5, 2010 5:29:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Oct 5, 2010 5:29:51 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12b7eea: defining beans [personService]; root of factory hierarchy
This is save() method.

No comments:

Post a Comment