Spring Study Notes-Why use Spring

Source: Internet
Author: User

Spring Study Notes-Why use Spring

In our project, we don't need Hibernate, Struts, or ibatis. But we use Spring in almost every project. Why? Let's analyze the advantages of Spring:
1. Reduce coupling between components to achieve decoupling between different layers of software
2. You can use many services provided by containers, such as transaction management and message service. When we use containers to manage transactions, developers no longer need to manually control the transactions or process the responsible transactions.
Hibernate transaction operations:

public void save(){    Session session = SessionFactory.getCurrentSession();    session.beginTransaction();    session.save("internet finance");    session.getTransaction().commit();}

JDBC transaction operations:

Connection conn = null;try{    ...    Statement stmt = conn.createStatement();    stmt.executeUpdate("update balance = 100000 where account = '622545878411212'");    conn.commit();    ....}catch(Exception e){    conn.rollback();}finally{    conn.close();}

If we use Spring, we do not need to handle complicated transaction propagation behaviors. Let's look at the following example:

Public void payment () {Bean1.update (); // update Bean2.save (); // record operation logs} // if we do not use Spring, what should we do for the following two business needs? // 1. The Bean1 and Bean2 operation methods must be executed in the same transaction // 2. Whether or not Bean1 operations are successful, I have logged the public class Bean1 {public void update () {Connection conn = null; conn. setAutoCommit (false); Statement.exe cuteUpdate ("update account set amount =? Where id =? ") ;}} Public class Bean2 {public void save () {Connection conn = null; conn. setAutoCommit (false); Statement.exe cuteUpdate ("insert into log (content) values (?) ");}}

Obviously, the methods of the two beans are in two transactions (meeting the requirements of the second business scenario), because they each have their own Connection. To meet the needs of the first business scenario, we can perform the following Transformation:

Public void payment (Connection conn) {Connection conn = null; conn. setAutoCommit (false); Bean1.update (conn); // Bean2.save (conn); // record operation logs} public class Bean1 {public void update (Connection conn) {Statement.exe cuteUpdate ("update account set amount =? Where id =? ") ;}} Public class Bean2 {public void save (Connection conn) {Statement.exe cuteUpdate (" insert into log (content) values (?) ");}}

Below I use Spring to implement these two business scenarios:
First business scenario

// Configure @ transactional (propagation = Propagation. required) public void payment () {Bean1.update (); // update Bean2.save (); // record operation logs} public class Bean1 {@ transactional (propagation = Propagation. required) public void update () {Connection conn = null; conn. setAutoCommit (false); Statement.exe cuteUpdate ("update account set amount =? Where id =? ") ;}} Public class Bean2 {@ transactional (propagation = Propagation. required) public void save () {Connection conn = null; conn. setAutoCommit (false); Statement.exe cuteUpdate ("insert into log (content) values (?) ");}}

Second business scenario

// Configure @ transactional (propagation = Propagation. required) public void payment () {Bean1.update (); // update Bean2.save (); // record operation logs} public class Bean1 {@ transactional (propagation = Propagation. required) public void update () {Connection conn = null; conn. setAutoCommit (false); Statement.exe cuteUpdate ("update account set amount =? Where id =? ") ;}} Public class Bean2 {@ transactional (propagation = Propagation. requiredNew) public void save () {Connection conn = null; conn. setAutoCommit (false); Statement.exe cuteUpdate ("insert into log (content) values (?) ");}}

3. Spring provides support for the singleton mode. Developers no longer need to write their own code for implementation.
4. The Container provides the AOP technology, which can easily implement functions such as permission interception and runtime monitoring.
5. The Container provides many auxiliary tool classes that can accelerate application development, such as JDBCTemplate and HibernateTemplate.
6. Spring provides integration support for mainstream application frameworks, such as Hibernate, JPA, and Struts.
Lightweight and heavyweight division
It mainly depends on the number of services it uses. The more services it uses, the more work containers need to do for common Java objects, which will inevitably affect the application release time or running performance.
For Spring containers, it provides many services, but these services are not opened by default for the application. The application requires a certain service and also needs to specify to use this service. If the application uses few services, for example, if you only use Spring's core service, you think it is lightweight. If the application uses most of Spring's services, then the application is heavyweight. Currently, the EJB container provides all EJB functions for the application by default.

Related Article

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.