6.3.3 实现 Service模块
同DAO层定义的接口类似,前我们将Service的接口定义好,并且发布成一个单独的构件了,解析来由独立的开发人员,在自己的电脑上创建一个新的工程,继承SpringPOM,集成DAO接口的依赖和Service接口的依赖,独立进行Service的实现代码编写和测试。
因为要对Service实现方法进行测试,编码的时候可以面向接口编程,测试的时候,肯定要基于DAO的实现才能操作数据库数据,所以测试的时候,还需要额外的添加前面Hibernate的DAO实现依赖,不过该依赖的score是test,也就是只在测试的时候有效,详细情况,请注意接下来介绍的工程pom.xml中的备注。
接下来,我们按类似Hibernate的DAO实现的思路,介绍Service的实现模块。
第一步:创建工程,配置好pom.xml
同以前一样,创建一个Maven工程,工程目录结构图如下:
根据本节开篇的介绍,我们需要在pom.xml中做如下几个事情:
1、配置继承SpringPOM构件的信息(里面配置了Spring需要的依赖)
2、添加DAO接口构件和Service接口构件的依赖
3、添加Hibernate DAO实现构件的依赖,作用范围是test。
请查看如下pom.xml,注意加粗部分内容和注释,细心的同学会发现里面没有添加DAO接口的构件依赖,只添加Service接口的构件依赖,同我们前面介绍的第二点要求不符合。原因是Service接口构件内部,有配置好对应DAO接口构件的依赖,只要我们在这里配置Service接口构件的依赖,Maven会在加载Service接口构件依赖的同时,自动的连带着将Service接口构件自己内部所需要的其它依赖加进来。
pom.xml
<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>
<!--继承SpringPOM构件-->
<parent>
<groupId>cn.com.mvnbook.pom</groupId>
<artifactId>SpringPOM</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.com.mvnbook.ssh.demo</groupId>
<artifactId>MvnBookSSHDemo.Service.impl</artifactId>
<packaging>jar</packaging>
<name>MvnBookSSHDemo.Service.impl</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--Service接口构件依赖-->
<dependency>
<groupId>cn.com.mvnbook.ssh.demo</groupId>
<artifactId>MvnBookSSHDemo.Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--Hibernate DAO实现构件依赖-->
<dependency>
<groupId>cn.com.mvnbook.ssh.demo.dao.hibernate</groupId>
<artifactId>MvnBookSSHDemo.DAO.Hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--作用范围-->
<scope>test</scope>
</dependency>
</dependencies>
</project>
第二步:编写Service实现代码
Service的实现代码相对比较简单,只是要有Spring4容器管理相关的基础,因为里面用到Spring内部的组件注解、依赖注入注解和事务管理注解,详情请看代码和Spring4相关的资料。
UserServiceImpl.java
package cn.com.mvnbook.ssh.demo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.com.mvnbook.ssh.demo.dao.IMvnUserDAO;
import cn.com.mvnbook.ssh.demo.entity.MvnUser;
import cn.com.mvnbook.ssh.demo.service.IUserService;
@Service("userService") // 注册成服务组件
@Transactional // 要求启动事务
public class UserServiceImpl implements IUserService {
@Autowired // 自动根据类型注入
@Qualifier("userDAO") // 根据name注入
private IMvnUserDAO userDAO;
@Transactional(propagation=Propagation.REQUIRED)//需要進行事务管理
public void createUser(MvnUser user) {
// 验证输入的用户是否为null
if (user == null) {
throw new RuntimeException("创建的user不能为null");
}
// 验证用户名是否有存在
MvnUser u = this.userDAO.findUserByUserName(user.getUrUserName());
if (u != null) {
throw new RuntimeException(u.getUrUserName() + " 已经存在");
}
this.userDAO.addUser(user);
}
@Transactional(propagation=Propagation.REQUIRED)//需要進行事务管理
public void editUser(int age, String status, int id) {
// 根据id找到以前的用户对象
MvnUser user = this.userDAO.findUserById(id);
// 判断用户是否存在,不存在抛异常,存在就更新
if (user == null) {
throw new RuntimeException("id为" + id + "用户不存在");
} else {
user.setUrAge(age);
user.setUrStatus(status);
this.userDAO.update(user);
}
}
@Transactional(propagation=Propagation.REQUIRED)//需要進行事务管理
public void deleteUser(int id) {
// 根据id找到以前的用户对象
MvnUser user = this.userDAO.findUserById(id);
// 判断用户是否存在,不存在抛异常,存在就删除
if (user == null) {
throw new RuntimeException("id为" + id + "用户不存在");
} else {
this.userDAO.deleteUser(user);
}
}
@Transactional(readOnly=true)//只读,不需要進行事务管理
public MvnUser searchUser(int id) {
MvnUser user = null;
user = this.userDAO.findUserById(id);
return user;
}
@Transactional(readOnly=true)//只读,不需要進行事务管理
public MvnUser searchUser(String userName) {
MvnUser user = null;
user = this.userDAO.findUserByUserName(userName);
return user;
}
@Transactional(readOnly=true)//只读,不需要進行事务管理
public List<MvnUser> searchUsers() {
List<MvnUser> userList = null;
userList = this.userDAO.findUsers();
return userList;
}
}
第三步:编写Service的测试案例代码和必须的配置资源文件
因为测试代码的测试环境是依赖Spring容器的,所以测试部分的内容,除了有针对UserServiceImpl.java的测试案例类之外,需要配置一个applicationContext.xml。而且还要注意,不管是测试类还是测试资源,都需要放在src/test的对应子目录下(看结构图)。
TestUserServiceImpl.java
package cn.com.mvnbook.ssh.demo.service.impl;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.mvnbook.ssh.demo.entity.MvnUser;
import cn.com.mvnbook.ssh.demo.entity.Status;
import cn.com.mvnbook.ssh.demo.service.IUserService;
import junit.framework.Assert;
public class TestUserServiceImpl {
private IUserService userService;
private ApplicationContext ctx = null;
@Before
public void init(){
this.ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
this.userService = (IUserService)ctx.getBean("userService");
}
@Test
public void testCreateUser(){
MvnUser user = new MvnUser();
user.setUrAge(11);
user.setUrPassword("11");
user.setUrStatus(Status.ACTIVE.getStatus());
user.setUrUserName("service1");
this.userService.createUser(user);
MvnUser u = this.userService.searchUser("service1");
boolean bool = u != null && u.getUrAge()==11 && u.getUrStatus().equals(Status.ACTIVE.getStatus());
Assert.assertTrue(bool);
// 删除用户
this.userService.deleteUser(u.getUrId());
}
@Test
public void testEditUser(){
MvnUser user = new MvnUser();
user.setUrAge(11);
user.setUrPassword("11");
user.setUrStatus(Status.ACTIVE.getStatus());
user.setUrUserName("service1");
this.userService.createUser(user);
MvnUser u = this.userService.searchUser("service1");
this.userService.editUser(88, Status.INACTIVE.getStatus(), u.getUrId());
u = this.userService.searchUser("service1");
Assert.assertTrue(u.getUrAge()==88 && u.getUrStatus().equals(Status.INACTIVE.getStatus()));
this.userService.deleteUser(u.getUrId());
}
@Test
public void testDeleteUser(){
MvnUser user = new MvnUser();
user.setUrAge(11);
user.setUrPassword("11");
user.setUrStatus(Status.ACTIVE.getStatus());
user.setUrUserName("service1");
this.userService.createUser(user);
MvnUser u = this.userService.searchUser("service1");
this.userService.deleteUser(u.getUrId());
MvnUser u2 = this.userService.searchUser(u.getUrId());
Assert.assertTrue(u != null && u2 == null);
}
@Test
public void testSearchUserById(){
MvnUser user = this.userService.searchUser(1);
Assert.assertNotNull(user);
}
@Test
public void testSearchUserByUserName(){
MvnUser user = this.userService.searchUser("zhangsan");
Assert.assertNotNull(user);
}
@Test
public void testSearchUsers(){
List<MvnUser> userList = this.userService.searchUsers();
Assert.assertTrue(userList != null && userList.size()>0);
}
@After
public void destory(){
this.userService = null;
this.ctx = null;
}
}
applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan
base-package="cn.com.mvnbook.ssh.demo"></context:component-scan>
</beans>
第四步:测试 安装 和 发布
这里的测试 安装 和 发布等,同Hibernate DAO实现里面的一样。
1、测试
工程鼠标右击 Run As -> Maven test
效果图如下:
2、安装
工程鼠标右击 Run As ->Maven Install
效果图如下:
3、发布
工程鼠标右键 Run As -> Maven build...
在打开窗口的 Goals输入框中输入 deploy,点击Run按钮
效果图如下:
版权所有,转载请注明 系列参考视频 http://cyedu.ke.qq.com