北京网站优化外包公司,怎样做推广才有效,广告制作方案,北京制作手机网站spring 事件模型我们将为时间表管理构建应用程序。 因此#xff0c;让我们首先考虑一些用例和实体。 让我用几个项目符号写它们#xff1a; 任务由经理分配给员工。 一项任务可以分配给许多员工。 员工将他在某些任务上工作的小时数填充到系统中。 经理/员工查看时间表上的… spring 事件模型 我们将为时间表管理构建应用程序。 因此让我们首先考虑一些用例和实体。 让我用几个项目符号写它们 任务由经理分配给员工。 一项任务可以分配给许多员工。 员工将他在某些任务上工作的小时数填充到系统中。 经理/员工查看时间表上的报告时间表可以更改。 让我们稍微回顾一下这些要点然后尝试将这种简单的人类语言转换为程序员可以发现的某些关系和实体。 实体经理员工时间表任务 好的我们现在应该对领域有了更好的了解所以让我们创建maven项目并实现类。 有了Maven您将获得漂亮干净的项目结构。 您所需要做的就是安装Maven并在项目中包含pom.xml。 您可以“手动”执行操作并通过终端构建应用程序在这种情况下只需创建常规项目并添加pom.xml文件。 我更喜欢使用一些其他工具。 IntelliJ IDEANetBeans和Springsource Tool Suite具有现成的Maven支持。 如果您使用的是纯Eclipse请检查m2eclipse插件。 无论哪种方式这都是我们项目的一些基本Maven配置 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.timesheet/groupIdartifactIdorg.timesheet/artifactIdversion0.0.1-SNAPSHOT/versionnameTimesheet Management On Spring/namebuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource1.6/sourcetarget1.6/target/configuration/plugin/plugins/build
/project 现在让我们实现领域模型。 创建包org.timesheet.domain并定义以下类。 package org.timesheet.domain;public class Employee {private String name;private String department;public Employee(String name, String department) {this.name name;this.department department;}public String getName() {return name;}public String getDepartment() {return department;}
}package org.timesheet.domain;public class Manager {private String name;public Manager(String name) {this.name name;}public String getName() {return name;}
}package org.timesheet.domain;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class Task {private ListEmployee assignedEmployees new ArrayListEmployee();private Manager manager;private boolean completed;private String description;public Task(String description, Manager manager, Employee... employees) {this.description description;this.manager manager;assignedEmployees.addAll(Arrays.asList(employees));completed false;}public Manager getManager() {return manager;}public ListEmployee getAssignedEmployees() {return assignedEmployees;}public void addEmployee(Employee e) {assignedEmployees.add(e);}public void removeEmployee(Employee e) {assignedEmployees.remove(e);}public void completeTask() {completed true;}
}package org.timesheet.domain;public class Timesheet {private Employee who;private Task task;private Integer hours;public Timesheet(Employee who, Task task, Integer hours) {this.who who;this.task task;this.hours hours;}public Employee getWho() {return who;}public Task getTask() {return task;}public Integer getHours() {return hours;}/*** Manager can alter hours before closing task* param hours New amount of hours*/public void alterHours(Integer hours) {this.hours hours;}Overridepublic String toString() {return Timesheet [who who , task task , hours hours ];}} 如您所见Manager和Employee类没有很多属性它们在这里只是为了拥有类型安全模型。 在“现实世界”中他们可能还有其他各种属性例如姓生日地址等等甚至可能是普通的父类。 另外我们现在并不真正在乎各种约束。 例如我们只能在任务上填写整数小时依此类推。 现在是时候定义我们的服务层–定义业务操作并为这些操作建立接口。 因此让我们制作软件包org.timesheet.service 。 首先我们将创建GenericDao接口在其中我们将为系统中的每个实体定义基本的CRUD操作。 package org.timesheet.service;import java.util.List;public interface GenericDaoE, K {void add(E entity);void update(E entity);void remove(E entity);E find(K key);ListE list();} 现在让我们不必担心实际的持久层-让我们创建一些虚拟实现并将所有数据存储在内存中。 我们将其放入新包– org.timesheet.service.impl中 。 不用担心稍后我们将使用Hibernate。 这是虚拟实现的代码 package org.timesheet.service.impl;import java.util.ArrayList;
import java.util.List;import org.timesheet.service.GenericDao;public class InMemoryDaoE, K implements GenericDaoE, K {private ListE entities new ArrayListE();Overridepublic void add(E entity) {entities.add(entity);}Overridepublic void update(E entity) {throw new UnsupportedOperationException(Not supported in dummy in-memory impl!);}Overridepublic void remove(E entity) {entities.remove(entity);}Overridepublic E find(K key) {if (entities.isEmpty()) {return null;}// just return the first one sice we are not using any keys ATMreturn entities.get(0);}Overridepublic ListE list() {return entities;}} 接下来我们将编写我们的第一个简单测试。 现在我们将第一个依赖项添加到JUnit库的pom.xml文件中。 因为它是第一个所以我们还需要将其包装到dependencies元素中如下所示 dependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.10/version/dependency/dependencies 这是我们针对Employee DAO的第一个非常简单的单元测试。 我们现在不会做其他事情因为我们还没有真正要测试的东西。 不过更重要的是我们如何依赖测试中DAO的实现我们使用新的InMemoryDao… 。 这很不好因为我们应该只测试定义接口的公共API。 在本教程的后面您将看到Spring如何帮助我们解决此类问题。 package org.timesheet.service;import static org.junit.Assert.*;import java.util.List;import org.junit.Before;
import org.junit.Test;
import org.timesheet.domain.Employee;
import org.timesheet.service.impl.InMemoryDao;public class EmployeeDaoTest {private GenericDaoEmployee, Long employeeDao new InMemoryDaoEmployee, Long();Beforepublic void setUp() {for (int i 0; i 5; i) {Employee e new Employee(Mike i, IT);employeeDao.add(e);}}Testpublic void testAdd() {int oldSize employeeDao.list().size();Employee e new Employee(Bob, IT);employeeDao.add(e);int newSize employeeDao.list().size();assertFalse (oldSize newSize);}Testpublic void testRemove() {int oldSize employeeDao.list().size();Employee e employeeDao.find(1L);employeeDao.remove(e);int newSize employeeDao.list().size();assertFalse (oldSize newSize);}Testpublic void testUpdate() {//TODO: need real implementation}Testpublic void testList() {ListEmployee list employeeDao.list();assertNotNull (list);assertFalse (list.isEmpty());}} 如果需要还可以为其他DAO剩余的测试编写单元测试。 但是由于我们现在没有合适的实现来测试因此我们将在以后一起进行测试。 事情并非总是那么容易。 这不仅与CRUD操作有关还与业务操作的通用性不足以在简单DAO中表达有关。 因此让我们定义一些业务操作并为其创建单独的服务。 我们将此服务称为TimesheetService。 package org.timesheet.service;import org.timesheet.domain.Employee;
import org.timesheet.domain.Manager;
import org.timesheet.domain.Task;import java.util.List;/*** Business that defines operations on timesheets*/
public interface TimesheetService {/*** return Finds the busiest task (with the most of employees).* Returns {code null} when tasks are empty.*/Task busiestTask();/*** Finds all the tasks for the employee.* param e Employee* return Tasks*/ListTask tasksForEmployee(Employee e);/*** Finds all the tasks for the manager.* param m Manager* return Tasks*/ListTask tasksForManager(Manager m);} 好的到目前为止很好。 您现在已经知道在下一个示例中我们将使用什么业务领域。 您可能现在想知道-我们还没有使用Spring为什么呢 请记住 Spring的最初目的是简化企业Java开发并鼓励POJO开发模型。 因此将Spring与该基本模型一起使用将非常容易因此我们不会将核心逻辑与不必要的依赖项混合在一起。 在下面的图片中到目前为止我们已经构建了项目的结构因此请确保您表现良好。 参考 第1部分–在vrtoonjava博客上由我们的JCG合作伙伴 Michal Vrtiak 设计域模型和服务层 。 翻译自: https://www.javacodegeeks.com/2012/09/spring-designing-domain-model-and.htmlspring 事件模型