Benefits of using spring in the SSH framework

Source: Internet
Author: User

Spring acts as the role of the management container in the SSH box fake. We all know that hibernate is used to make the persistence layer, because it makes JDBC a good package, programmers can not write a large number of SQL statements when interacting with the database. Struts is used to make the application layer, and he is responsible for invoking the business logic serivce layer. So the process of the SSH framework is basically: JSP page----Struts------Service (Business logic processing Class)---Hibernate (left to right)
Struts is responsible for controlling the service (Business logic processing Class), thus controlling the service life cycle, so that the dependencies between layers and layers are strong and belong to the coupling. At this point, the spring framework is used to control the action object (in Strus) and the service class, and the relationship between the two is loose, and spring's IOC mechanism (control inversion and dependency injection) is used here.
Spring's IOC (control inversion and dependency injection)
Control reversal: It is the (dependency) relationship between the container control program, not the traditional implementation, directly manipulated by the program code
Dependency Injection: Dependencies between components are determined by the container at run time, and the container dynamically injects some kind of dependency into the component.
From the above we can see: The action is only as a service control tool, how these specific business methods are implemented, he simply does not care, and will not ask, he just know that these business implementation class provided by the method interface. In the past, when using the struts framework alone, the lifecycle of all business method classes, and even some of the business processes, were controlled by action. The coupling between layers and layers is too tight, which reduces the efficiency of data access and makes the business logic look complex and the amount of code is much. , the spring container controls the life cycle of all action objects and business logic classes, and because the upper layer no longer controls the lower life cycle, the layers are fully decoupled from each other, making the program more efficient to run and easier to maintain.
The second benefit of using spring (AOP application):
Handling of transactions:
In previous JdbcTemplate, the transaction was submitted successfully, and exception handling was done through Try/catch, and in spring. The spring container integrates transactiontemplate, which encapsulates all the functionality of transactional processing, including complex business functions such as transaction rollback during an exception, data submission when the operation succeeds. This is managed by the spring container, greatly reducing the amount of code for the programmer, and also has good administrative control over the transaction. Hibernate also has the management of transactions, hibernate in the transaction management is through the Sessionfactory creation and maintenance session to complete. Spring also integrates the Sessionfactory configuration without having to set the sessionafactory through Hibernate.cfg.xml. This will make good use of sping for transaction management. It avoids the need to get the session instance to start the transaction/COMMIT/ROLLBACK TRANSACTION and the tedious Try/catch operation for each data operation. These are good applications for the AOP (aspect-oriented programming) mechanism in spring. On the one hand, the development of business logic clearer, professional division of labor more easily. On the other hand, the application of SPIRNG AOP isolation reduces the coupling of the program so that we can combine the facets in different applications and greatly improve code reuse.

Core tips:
The Spring Framework (Spring) is a Java EE application framework developed according to Rod Johnson's renowned Expert one-on-one EE Design and development. 1. What is the spring Framework?
The Spring Framework (spring) is a Java EE application framework developed according to Rod Johnson's renowned Expert one-on-one EE Design and development. Currently developed mainly in accordance with Rod Johnson and Juergen Hoeller, the current release of the latest version is 1.1.4. Spring is the Java EE application Framework, but, more strictly speaking, it is a lightweight container (lightweight container) for managing the Bean's lifecycle, and can be built using spring alone or struts,webwork, Many Web application frameworks, such as tapestry, and can be combined with desktop application APIs such as swing. So spring is not only used in Java EE, but also in desktop applications and small applications. Components developed for spring do not require any external libraries.

2. What are the benefits of using spring?
(1) Spring can effectively organize your middle-tier objects.
(2) Spring can eliminate the overuse of singleton that is common in many projects.
(3) Spring can eliminate the need for a variety of custom-formatted properties files, unified configuration information.
(4) Spring can help us to really implement programming for the interface.
(5) Most business objects in spring applications are not dependent on spring.
(6) Applications built using spring are easy to unit test.
(7) Spring supports JDBC and O/R mapping Products (Hibernate)
(8) The MVC Web Framework provides a clear, non-aggressive way to implement MVC.
(9) The Jndi abstraction layer, which facilitates the change of implementation details, can easily be switched between remote services and local services.
(10) Simplify the exception handling when accessing the database.
(11) Spring can use AOP to provide declarative transaction management, and it can manage transactions without directly manipulating JTA.
(12) Support for JavaMail or other mail systems is provided.

3. What is a light weight (lightweight) level container?
Spring developers can avoid the drawbacks of developing ejbs with heavy-weight containers:
(1) An aggressive API. (Code relies on EJB)
(2) dependence on the container. (Code cannot work outside the EJB container)
(3) Provide a fixed set of functions, do not have the ability to configure.
(4) Different products, the deployment process is different, not easy to general.
(5) long start-up time.

For these issues, Spring uses the IOC to reduce the dependency on spring, depending on the Web application, the small application, the desktop application, and the container dependency level. Spring controls managed Beans as Pojo (Plain old Java Object) and can add additional functionality through AOP interceptor.
In addition to the lightweight containers outside of spring, there are picocontainer, (not lightweight containers) that manage the life cycle of the bean as well as the Avalon of Apache Avalon Project.

Summary: The core idea of spring is that the IOC and the aop,spring itself are a lightweight container, unlike the EJB container, the spring component is the ordinary Java Bean, which makes unit testing can no longer rely on the container, writing easier. Spring is responsible for managing all Java bean components, as well as declarative transaction management. We just need to write the Java Bean components and then "assemble" them, and the initialization and management of the components is done by spring, which is simply declared in the configuration file. The biggest advantage of this approach is that the coupling of the components is extremely loose and does not require us to implement the Singleton mode ourselves. The first purpose of dependency injection is decoupling, including transaction management is also to understand the coupling, so-called decoupling is to replace the bottom layer, the upper code without modification. Said the major is that the upper code does not care about the implementation of the bottom, interface docking good OK. But in fact, the bottom of the replacement of such things, the average person in the lifetime estimates are not met once, hehe.
So I like the dependency injection, because it helps me manage a single case, I don't have to tangle myself to write lazy single case, or a hungry man type single case, even the awesome enumeration single case. As for the multi-instance of the bean provided by spring, it never does, and my own new is no different. This should explain your "hand-injection".

I prefer spring's ability to integrate, and now that the Java field is more than a fire tool and direction, basic spring will be inserted in one foot. A joke, spring that most people code is too ugly, say forget it all you simply match in my XML, and then use the Xxxtemplate method I provide simple pass a few parameters, you can send JMS, send WebService, insert database, cache, batch processing, etc.

Benefits of using spring in the SSH framework

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.