Jive forum and Spring framework, classic code and architecture!

Source: Internet
Author: User
  

 

No new technology is born out of thin air. Its Sprout or embryo is always more or less apparent in previous technologies. The jive forum is a design-oriented application that everyone is devoted, the related analysis shows the gof design mode column in this column.

Jive and spring are J2EE Web Systems Composed of JavaBeans. As an early successful design case, jive has become a popular architecture for most pure ans systems, and spring is no exception.

In addition to a Web-layer application framework, the Spring framework also provides interfaces for accessing the EJB layer and direct JDBC/ORM operations. The main charm of the Spring framework is that the IOC mode and AOP are used to implement the general functions of the jive system, so that the pure JavaBeans architecture design such as jive can be reused in other systems.

If you feel deeply about the design concept of jive, but you cannot reuse it, the Spring framework has already helped you implement it.

At the same time, it should be noted that the Spring framework is similar to the "hodgedge". It contains many J2EE application tools, such as calling EJB. Its MVC and Struts JSF are also competing, for pure IOC and AOP design, the Spring framework is also a very heavy (heavy, weight) framework. The Spring framework is complex. If you want to replace EJB with spring, you will undoubtedly float the handler.

Linking the jive forum with the Spring framework will help more programmers who understand the design pattern quickly grasp the latest design trend, rather than a sudden forced acceptance. If you have a good research on jive, you will find that the Spring framework is a more general improvement of jive design.

In jive, forumfactory is the entry point and breakthrough point of the entire system. Jive uses forumfactory to control the entire system in a factory mode. The advantage of this is that it is easy to control the system's JavaBeans, for example, the client can create a forum or access a forum through forumfactory, but is there access permission? For example:


  
Jive uses forumfactory to direct such access to the corresponding proxy class, such as the forumfactoryproxy class, and uses the proxy mode to control access to these classes. This is a major use of the proxy mode, but studying the proxy mode of jive will find that it is very trivial to implement a proxy class for each class. Is there a more elegant way? Of course, dynamic proxy is used.

The Spring framework basically abstracts the above design. The Spring framework manages all JavaBeans Based on the Bean Factory mechanism at the same entrance. The difference is that beanfactory can manage the JavaBeans of all applications, the user only needs to tell beanfactory through the configuration file of their own JavaBeans, then beanfactory will load these JavaBeans, for example:

<Beans>
<Bean id = "examplebean" class = "eg. examplebean"/>
<Bean id = "anotherexample" class = "eg. examplebeantwo"/>
</Beans>

In jive, forumfactory implements dbforumfactory to load jive's own JavaBeans through the factory. The following code triggers a series of background functions, which is vertical, the return New forumfactoryproxy statement is similar to the statement that draws a plane and implements permission access and other functions from a horizontal perspective:

Private Static string classname = "com. jivesoftware. Forum. database. dbforumfactory ";

Public static forumfactory getinstance (authorization ){
// If no valid authorization passed in, return null.
If (authorization = NULL ){
Return NULL;
}
// Singleton single-State mode is used below
If (factory = NULL ){
Synchronized (initlock ){
If (factory = NULL ){
......

Try {
// Dynamic reprint class
Class C = Class. forname (classname );
Factory = (forumfactory) C. newinstance ();
}
Catch (exception e ){
Return NULL;
}
}
}
}

// Now, return proxy. Used to restrict access to Forum
Return new forumfactoryproxy (authorization, factory, factory. getpermissions (authorization ));
}

Since the spring framework loads all classes through a bean factory, how is it loaded? The IOC mode is used, that is, the dependent injection mode. In my previous article "IOC mode", I compared the similarities and differences between the injection object implementation in the factory mode and the IOC mode, compared with the factory mode, IOC decouples the relationship between the caller and the called. The IOC mode does not need to involve the specific implementation of the called in the caller code.

The Spring framework can not only inject the ans defined by the app to its own containers (that is, create them), but also assign values to these JavaBeans using the set method.

Once Bean Factory is running in charge of these activated objects, spring uses the AOP method to provide these JavaBeans with the implementation of common functions such as permission access and transaction lock from a horizontal plane, this implementation is based on the dynamic proxy mode, while dynamic proxy is a way of Implementing AOP.

As mentioned above, in jive, permission access is implemented in proxy mode, which is more concise and abstract than the proxy mode. Dynamic proxy makes it unnecessary for the caller to specify the proxy class of the caller, this is the essence of the dynamic proxy mode.

The advantage of dynamic proxy can also be reflected in another sentence: Dynamic proxy intercepts the caller's calls to the called, which is exactly in line with the AOP interceptor function, it provides a possibility for AOP implementation.

The Spring framework uses dynamic proxy to implement AOP. It intercepts external calls to objects managed by Bean Factory through the dynamic proxy mechanism. For example:

 

The above just roughly deconstruct the spring architecture. In this architecture, the Spring framework also carries many other features, such as web MVC, Dao JDBC, Dao Orm, and remote, the latter is similar to the EJB method call framework I designed.

In short, spring is indeed a perfect application of IOC and AOP. IOC is used to load JavaBeans and create these objects. AOP is used to intercept the use of these objects, which is the inevitable classic method of framework design.

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.