Architecture thinking of building Java Web Rapid Development framework based on REST+COC

Source: Internet
Author: User
Tags ruby on rails

In the first few chapters of the book Web development Agility, Dave shows us how to simply start a Ruby on Rails tour, build a table, execute a ruby command line to build a scaffold scaffold program, and then write a code that You can accomplish the functionality of a table or a crud operation that is a resource. This has stimulated my interest in using Java, can not be more simple to complete this matter.

I have implemented a JDBC version of the base class, and a hibernate version of the base class, to mimic Ror's activerecord, so that when we program, we just need to implement a subclass of the base to complete the crud.

My graduation topic is related to Rest , more precisely to Roa, and to spring MVC's preference for Conventions (Convention over CONFIGURATION,COC) Principles of implementation.

Four elements of a Web schema

Both rails and spring MVC are MVC implementations, so my research is certainly based on the MVC pattern. So let's focus on m,v and C, and also the router (or the mapping file in struts) that maps URLs and C.

The first is m, I do not want to make m, that automatically generated, how to automatically generate, according to the database automatically generated, and I generated if a ActiveRecord mode class. So it's not like I used to get a hashmap. After reading the knowledge of ASM3.1 and ClassLoader, I found that this can be achieved. As follows:

The whole test first:

@Test

Java code
  1. public void gerneratorclass () {
  2. try {
  3. map<string,object> fields=new hashmap<string,object> ();
  4. Fields.put ("id", 1l);
  5. fields.put ("name", "wanxing");
  6. entitygernerator.generateentity ("Student", fields);
  7. Entitygernerator.invoke ("add");
  8. } catch (Exception e) {
  9. //TODO auto-generated catch block
  10. E.printstacktrace ();
  11.     }  
  12. }  

The realization of this piece I think about, Entitygernerator's Generateentity method is just to create a class or the instantiation of the object, and it also has the Invoke method, which does not seem right, well, it should be only generated class to end the mission. But you can iterate later.

The implementation of the code is very simple, is the use of ASM3.1, it is done. There are two points to declare:

1 You can set the parent class of the generated class as needed

2 can only create class properties, cannot create methods, because the implementation of the method is too complex, with the JVM instructions to write the dead, instead of turning to the dynamic language, and it inherits the useful parent class, it has completed its own mission.

Both V and C can be used universally, just as with a generic DAO. What's special about V is that frontcontroller, which deploys the controllers, according to the URI and the controller's mapping, is consistent with Dr. Roy Fielding's definition of resources: resources are A conceptual mapping -The server receives an identifier (identifying this mapping) and applies it to the current mapping implementation (mapping implementation, which is usually a deep traversal of the tree associated with a particular set and/ Or a combination of a hash table, to discover the processor implementation that is currently responsible for processing the resource, and then the processor to select the appropriate action + response based on the requested content. It's called router in Ror. In addition, the appropriate presentation can be returned in V based on HTTP requests.

Rest and coc

According to the "formula first principle" can do a lot of things, such as blog class corresponding to BlogController, corresponding to/blog. We can put this principle into the above four elements: M,v,c and router. And then, in accordance with the above correspondence, to form a simple development framework, the only thing we need to do is:

1 Creating a database schema, such as creating a table blog

2 Configuring database connections, choosing which database to use

3 start Tomcat, enter http://localhost:8080/blog/new in the address bar of the browser to create a blog (return to the form of blogging), http://localhost:8080/blog/ 12, show the article; Http://localhost:8080/blog/edit, return to modify the form; http://localhost:8080/blog/list, return all blogs, plus q?time= 2009-05-08 or http://localhost:8080/2009-05-08/blog/list, query by time, the latter problem is that if the blog table has two fields is time is no way, but we can agree that it only a time when the default result , which is the "customary priority" role.

Entering these URLs only proves that the system works correctly. Then you can customize your HTML page, place these links where you like, or serve as a button. All the services are rest. A RCP client can also be used, or Delphi.

System in the background silently based on the database schema to create a blog class, and then the other is a common component, put into the blog class, such as Genericcontroller.setmodel (T model), the instance of the blog class into.

Flexibility and scalability

If that's the case, then the system doesn't actually do anything too valuable (it's just a thin layer out of the database), "formula first" is not a "custom decision" and we have to allow developer to develop its own individual components to accomplish more powerful functions.

Then the following logic is necessary:

1 for m: first check whether there is classpath in the URL of the entity (such as blog, we can not say that is a resource, the resource is a map), this entity is developer created, if not, go to the database to find a table named blog, Create a blog class (this class is not generated in the file system). If not, returns not found.

2 for C: First, according to the default definition in router, look for blogcontroller, check whether it exists in classpath, no Controllergenerator generates BlogController (same as entitygenerator, but since my generator can specify a parent class, actually using a classgenerator should be OK).

3 for V: Now the default path to find the page (JSP or HTML), if not, then create a blog page stream (stream) returned to the client.

4 for Router: The default is/blog corresponds to BlogController. Of course you can create your own router file to modify the mapping relationship.

"Practice first", then whose convention is it? In fact, router's customizable developer indicates that the system can support the creation of its own "conventions" between four web-schema elements. For example, all the table names plus T_blog, but the generated class name is a Blog, and controller is blogaction (someone just like to call it action), OK, this is fine.

Technical details

We go back to the M discussion. I am not only responsible for the ORM responsibility but also for the DVM (Domain View Mapping) responsibilities. That is, I expect my objects to be associated with other objects, and I want me to display the correct Chinese name on the UI instead of the field name.

First, the ORM, the correlation (inheritance is not expected to be automatically generated), I can be based on the primary foreign key relationship of the database table generation, and then use ASM Dynamic Annotated (or hibernate-based). The only thing I'm not sure about right now is whether the type can be another class with ASM-generated properties. The answer is yes, same as the built-in Java type.

And then I'm going to make the HTML or XML display the correct label for the field, which extracts the notes from the table, and then uses ASM to dynamically add annotations.

Just Play

The basic framework is useful for simple maintenance of basic data, or for your system design, and it is better to face a truly complex large enterprise application than to prevent you from implementing complex object graphs.

Writing this, I first felt: Why do I have to use Java? So finally I can only say, Just Play, as Dave says, in a real project, code generation is not as useful as it seems, but at least you have a good scaffold ~ ~ ~

clarify a few misconceptions

1 This architecture does not rely on code to generate Java and then modify it based on the generated code. Instead, generate bytecode in memory! This is not the same as rails.

2 In addition to the existing framework, a configurable "convention" is considered

3 rest style, the client is not limited to the browser, nor is it limited to Java

Architecture thinking of building Java Web Rapid Development framework based on REST+COC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.