日期: 2016-7-16
内容: 在完成了Maven开发环境的搭建之后,接着开始添加框架并实现测试。
这里使用的版本是:
Spring4.1.3;
Juint4.10;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.spring</groupId> <artifactId>Spring_Struts_HibernateWebProject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <description>使用Maven搭建SSH框架</description> <url>http://maven.apache.org</url> <dependencies> <!-- 添加Junit4为单元测试做准备 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- 添加Spring3的依赖jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.3.RELEASE</version> </dependency> </dependencies> </project>
在添加的jar包的过程中遇到不少问题,可以参照我的其他博文。
在src目录下添加applicationContext.xml Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> </beans>
编写接口和实现类:
package com.test.dao; public interface InjectionDAO { public void save(String arg); }
package com.test.dao; public class InjectionDAOImpl implements InjectionDAO { @Override public void save(String arg) { System.out.println("保存的数据: "+arg); } }
package com.test.service; public interface InjectionService { public void save(String arg); }
package com.test.service; import com.test.dao.InjectionDAO; public class InjectionServiceImpl implements InjectionService { InjectionDAO injectionDAO; //实现设值注入 public void setInjectionDAO(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; } @Override public void save(String arg) { System.out.println("接收的数据: "+arg); //处理数据 arg = arg+":"+this.hashCode(); //调用InjectionDAO的save方法处理数据 injectionDAO.save(arg);//--->配置xml配置文件 } }
Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="injectionService" class="com.test.service.InjectionServiceImpl"> <property name="injectionDAO" ref="injectionDAO"></property> </bean> <bean id="injectionDAO" class="com.test.dao.InjectionDAOImpl"></bean> </beans>
测试类:
package com.test.test; import java.text.SimpleDateFormat; import java.util.Date; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.service.InjectionService; public class TestSpringCase { <span style="white-space:pre"> </span>//获取当前时间 <span style="white-space:pre"> </span>Date date = new Date(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//格式化格式 <span style="white-space:pre"> </span>String ftype="yyyy年MM月dd日 hh时:mm分:ss秒"; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//时间格式化对象 <span style="white-space:pre"> </span>SimpleDateFormat sdf = new SimpleDateFormat(ftype); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//before设置 <span style="white-space:pre"> </span>@Before <span style="white-space:pre"> </span>public void beforeMethod() <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>System.out.println("开始执行: "+sdf.format(date)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//after设置 <span style="white-space:pre"> </span>@After <span style="white-space:pre"> </span>public void afterMethod() <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>System.out.println("执行完成: "+sdf.format(date)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//测试Spring环境 <span style="white-space:pre"> </span>@Test <span style="white-space:pre"> </span>public void testSpring() <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//加载配置文件 <span style="white-space:pre"> </span>ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); <span style="white-space:pre"> </span>//获得实例 <span style="white-space:pre"> </span>InjectionService is = (InjectionService)ac.getBean("injectionService"); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//调用保存方法保存数据 <span style="white-space:pre"> </span>is.save("我是需要保存的data"); <span style="white-space:pre"> </span>} }
开始执行: 2016年07月16日 10时:03分:44秒 七月 16, 2016 10:03:45 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 10:03:45 CST 2016]; root of context hierarchy 七月 16, 2016 10:03:45 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 接收的数据: 我是需要保存的data 保存的数据: 我是需要保存的data:394714818 执行完成: 2016年07月16日 10时:03分:44秒
Spring配置完毕!