实验十四 Spring进阶
- 格式:doc
- 大小:54.50 KB
- 文档页数:5
实验十四 Spring进阶 实验目的: 1, 理解Spring的IoC/DI的原理 2, 掌握Spring使用注解方式和编程 3, 掌握Spring的分层设计的开发方式
实验内容: 1, 在MyEclipse 中创建web工程(假设工程名为test14)。 2, 使用Spring 2.5的注解为项目添加Spring支持 1)添加Spring 2.5的三个jar, spring.jar , commons-logging.jar 和common-annotations.jar(支持注解的包)
2)添加配置文件beans.xml,添加context命名空间,并加入component-scan的标签: xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
第一个 第二个 第三个
第一个list元素 第二个list元素 第三个list元素
value1 value2 value3
3)测试,并观察结果,看集合属性是如何被注入的: ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonServiceBean personService = (PersonServiceBean)ctx.getBean("personService");
System.out.println("========set==========="); for(String value : personService.getSets()){ System.out.println(value); }
System.out.println("========list==========="); for(String value : personService.getLists()){ System.out.println(value); }
System.out.println("========properties==========="); for(Object key : personService.getProperties().keySet()){ System.out.println(key+"="+ personService.getProperties().getProperty((String)key)); } System.out.println("========map==========="); for(String key : personService.getMaps().keySet()){ System.out.println(key+"="+ personService.getMaps().get(key)); }
4, 导入并运行和观察“Spring2.5整合Struts2和Hibernate3”工程,打开Spring的配置文件,applicationContext.xml,如下:
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
打开并分析action, service, service.impl和 dao四个包(package)下的代码及其作用,并仿