Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_cn
Articles_pt
Articles_zh
CreateActions_zh
CreateDAO_zh
WebWorkActions_zh




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateManager_zh


Difference between version 7 and version 1:

At line 1 changed 1 line.
__Part II:__ [Creating new Managers|CreateManager] - A HowTo for creating Business Facades that talk to the database tier (DAOs) and handle transaction management.
__Part II:__ [创建新的Manager|CreateManager_zh] - 创建与数据库端(DAOs)交互的业务Facades和事务处理。
At line 3 changed 1 line.
;:''This tutorial depends on __Part I:__ [Creating new DAOs and Objects in AppFuse|CreateDAO].''
;:''本教程依赖于__Part I:__[在AppFuse建立DAO和POJO |CreateDAO_zh].''
At line 5 changed 2 lines.
!!About this Tutorial
This tutorial will show you how to create a Business Facade class (and a JUnit Test) to talk to the [DAO we created in Part I|CreateDAO].
!!关于本教程
本教程将会向你展示如何创建一个业务Facade类(和一个JUnit Test)与[Part I里生成的DAO|CreateDAO_zh]交互操作。
At line 8 changed 1 line.
In the context of [AppFuse], this is called a Manager class. Its main responsibility is to act as a bridge between the persistence (DAO) layer and the web layer. It's also useful for de-coupling your presentation layer from your database layer (i.e. for Swing apps). Managers should also be where you put any business logic for your application.
在[AppFuse]的语境下,这被称作一个Manager类,它的主要职责是持久户层(DAO)和web层之间的一个桥梁,它也很好的把展示层和数据库层(例如Swing应用)解耦,Managers必定是应用程序所有的业务逻辑所在的地方。
At line 10 changed 1 line.
;:%%(color: blue)''I will tell you how I do stuff in the __Real World__ in text like this.''%%
;:%%(color: blue)''我在“真实世界”中实际操作的方式用蓝色斜体表示。''%%
At line 12 changed 1 line.
Let's get started by creating a new ManagerTest and Manager in AppFuse's architecture.
让我们从在AppFuse的框架下创建一个ManagerTest和Manager。
At line 14 changed 5 lines.
!Table of Contents
* [1] Create a new ManagerTest to run JUnit tests on the Manager
* [2] Create a new Manager to talk to the DAO
* [3] Configure Spring for this Manager and Transactions
* [4] Run the ManagerTest
!目录
* [1] 创建一个新的运行JUnit测试的ManagerTest
* [2] 创建一个新的与DAO通讯的Manager
* [3] 为这个Manager和事务配置Spring
* [4] 运行ManagerTest
At line 20 changed 2 lines.
!!Create a new ManagerTest to run JUnit tests on the Manager [#1]
In [Part I|CreateDAO], we created a Person object and PersonDao - so let's continue developing this entity. First, let's create a JUnit test for the PersonManager. Create PersonManagerTest in the test/service/**/service directory. We'll want to test the same basic methods (get, save, remove) that our DAO has.
!!创建一个新的运行JUnit测试的ManagerTest [#1]
在[Part I|CreateDAO],我们创建了一个Person对象和一个PersonDao对象 - 所以我们继续开发这个实体,首先,我们创建PersonManager的JUnit test,在test/service/**/service目录下创建PersonManagerTest,我们会希望在DAO对象同样的基本方法(get, save, remove) 测试。
At line 23 changed 1 line.
;:''This may seem redundant (why all the tests!), but these tests are GREAT to have 6 months down the road.''
;:''这看起来是多余的(为什么全是测试!),但如果是一个6个月的过程这个测试是非常重要的。''
At line 25 changed 1 line.
This class should extend [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], which already exists in the ''service'' package. The parent class (BaseManagerTestCase) serves similar functionality as the BaseDaoTestCase.
这个类必须扩展''service''包下的[BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html],这个类(BaseManagerTestCase)的功能与BaseDaoTestCase类似。
At line 27 changed 1 line.
;:%%(color: blue)''I usually copy (open → save as) an existing test (i.e. UserManagerTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%%
;:%%(color: blue)''我通常会修改(打开 → 另存为)存在的测试(如UserManagerTest.java),查找/替换[[Uu]ser with [[Pp]erson,或者其他任何我的对象的名字。''%%
At line 29 changed 1 line.
The code below is what you need for a basic JUnit test of your Manager. Unlike the DaoTest, this test uses [jMock|http://jmock.org] to isolate the Manager from its dependencies and make it a ''true'' "unit" test. This can be very helpful because it allows you to test your business logic w/o worrying about other dependencies. The code below simply sets up the Manger and its dependencies (as Mocks) for testing.
以下代码是一个基本的Manager的JUnit测试的要求,与DaoTest不同,这个测试使用[jMock|http://jmock.org]来将Manager和他的依赖隔离,使它成为一个''真的''"单元" 测试。这可以使你只关心业务逻辑而不必担心它的依赖,以下代码简单的设置好Manager和它 的依赖。
At line 64 changed 1 line.
Now that you have the skeleton done for this class, you need to add the meat: the test methods to make sure everything works. Here's a snippet from the [DAO Tutorial|CreateDAO] tutorial to help you understand what we're about to do.
现在你已经把类的骨架搭好了,你需要添加肉了:填写确保所有测试通过的代码,以下来自[DAO Tutorial|CreateDAO]的片断帮助我们理解我们将要做的事情。
At line 66 changed 1 line.
;:''...we create methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our <junit> task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous.''
;:''...我们创建以"test"(全部小写)开头的方法,只要这些方法是public,返回类型是void,并且没有参数,它们就会被<junit>调用,以下是为了测试简单的CRUD操作,一件需要记住的事情是每一个方法(也可以称作测试)必须是自制的。''
At line 68 changed 1 line.
Add the following methods to your PersonManagerTest.java file:
添加如下方法到PersonManagerTest.java:
At line 81 added 1 line.
At line 127 changed 1 line.
This class won't compile at this point because we have not created our PersonManager interface.
这个类不会被编译,因为我们还没有创建PersonManager接口。
At line 129 changed 1 line.
;:%%(color: blue)''I think it's funny how I've followed so many patterns to allow __extendibility__ in AppFuse. In reality, on most projects I've been on - I learn so much in a year that I don't want to extend the architecture - I want to rewrite it. Hopefully by keeping AppFuse up to date with my perceived best practices, this won't happen as much. Each year will just be an upgrade to the latest AppFuse, rather than a re-write. ;-)''
;:%%(color: blue)''在AppFuse里遵从这么多规范来实现__可扩展性__看起来很可笑,事实上,在绝大多数我参与的项目里 - 我发现在一年里学了如此多的知识,以至于我不想扩展我的架构,我想去重写它,我希望通过采纳最佳实践来保持AppFuse的时效性,但这并不经常发生,每年都仅仅是一个道最新版本的升级,而不是一个重写,;-)''
At line 131 changed 1 line.
!!Create a new Manager to talk to the DAO [#2]
!!创建一个新的与DAO通讯的Manager[#2]
At line 133 changed 1 line.
First off, create a PersonManager.java interface in the src/service/**/service directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.'' The ''setPersonDao()'' method is not used in most cases - its just exists so the PersonManagerTest can set the DAO on the interface.
马上,为所有实现类在src/service/**/service目录创建一个PersonManager.java接口来指定基本的CRUD操作,为了显示的目的,我去掉了所有的JavaDocs, ''setPersonDao()''方法不是在所有的情况下出现,只是因为PersonManagerTest可以把DAO赋值。
At line 135 changed 1 line.
;:%%(color: blue)''As usual, I usually duplicate (open → save as) an existing file (i.e. UserManager.java).''%%
;:%%(color: blue)''通常,我会复制(打开 → 另存为)一个已存在的文件 (例如UserManager.java).''%%
At line 152 changed 1 line.
Now let's create a PersonManagerImpl class that implements the methods in PersonManager. To do this, create a new class in src/service/**/service/impl and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager.
我们创建一个PersonManagerImpl类来实现PersonManager中的方法,为此,在src/service/**/service/impl创建一个PersonManagerImpl.java类,他必须扩展BaseManage并且实现PersonManager。
At line 183 removed 1 line.
One thing to note is the {{setPersonDao()}} method. This is used by Spring to bind the PersonDao to this Manager. This is configured in the applicationContext-service.xml file. We'll get to configuring that in Step 3[3]. You should be able to compile everything now using "ant compile-service".
At line 185 changed 1 line.
Now you need to edit Spring's config file for our services layer so it will know about this new Manager.
需要注意的是{{setPersonDao()}}方法,Spring使用它来绑定PersonDao到Manager,这些配置在applicationContext-service.xml 文件,我们将在Step 3[3]配置这些,现在你可以使用"ant compile-service"编译所有代码。
At line 187 changed 1 line.
!!Configure Spring for this Manager and Transactions [#3]
现在你需要为服务层配置Spring文件,它才会知道这个新的Manager。
At line 189 changed 1 line.
To notify Spring of this our PersonManager interface and its implementation, open the src/service/**/service/applicationContext-service.xml file. In here, you should see a commented out definition for the "personManager" bean. Uncomment this, or add the following to the bottom of this file.
!!为这个Manager和事务配置Spring[#3]
At line 191 added 2 lines.
为了通知Spring我们的PersonManager接口和它的实现类,打开src/service/**/service/applicationContext-service.xml,你会看到注释掉的关于"personManager"的定义,去掉注释,或者直接在文件末尾添加:
At line 200 changed 1 line.
The "parent" attribute refers to a bean definition for a [TransactionProxyFactoryBean|http://www.springframework.org/docs/api/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.html] that has all the basic transaction attributes set.
"parent"属性会引用一个[TransactionProxyFactoryBean|http://www.springframework.org/docs/api/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.html]bean的定义,这也是所有的事物对象所要设置的。
At line 204 added 1 line.
!!运行ManagerTest[#4]
At line 203 changed 1 line.
!!Run the ManagerTest [#4]
保存所有的文件,并且运行__ant test-service -Dtestcase=PersonManager__。
At line 205 removed 2 lines.
Save all your edited files and try running __ant test-service -Dtestcase=PersonManager__.
At line 213 changed 1 line.
The files that were modified and added to this point are [available for download|https://appfuse.dev.java.net/files/documents/1397/7484/appfuse-tutorial-managers-1.6.zip].
此刻所有修改后的文件可以从[此处下载获得|https://appfuse.dev.java.net/files/documents/1397/7484/appfuse-tutorial-managers-1.6.zip].
At line 215 changed 1 line.
''Next Up:'' __Part III:__ [Creating Actions and JSPs|CreateActions] - A HowTo for creating Actions and JSPs in the AppFuse architecture.
''下一部分:'' __Part III:__ [创建 Struts Actions和JSPs|CreateActions_zh] - 在AppFuse架构下创建Actions和JSPs。

Back to CreateManager_zh, or to the Page History.