VB_
VB_

Reputation: 45722

Spring Framework purpose

I keep on learning Spring, so I decide to read Spring documentation from here. I faced the next phrase:

Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.

What does mean comprehensive infrastructure? As I understand Spring:

  1. Help us to bind the entire dependency tree;
  2. Provide integration with diferent technologies\frameworks an so on; (means JPA, JTA, AOP etc)
  3. Provide additional services, such as Spring Social, Big Data and so on.

Is comprehensive infrastructure support the same as three statements mentioned above?

Upvotes: 2

Views: 388

Answers (2)

Amir Pashazadeh
Amir Pashazadeh

Reputation: 7322

There are many other things Spring Framework handle and take away from your code, including:

  • IoC And AOP, Configuring Application
  • JNDI
  • Transaction management abstraction
  • Handling single transaction/connection using different technologies (such as JDBC and JPA)
  • Ensuring resource releasing (using Templates)
  • Unified event dispatcher
  • Provides common Aspects, such as async method call and transaction
  • Integration with JMS, JPA, Hibernate, ...
  • Transparent remoting (with different technologies)
  • MVC Servlet and Portlet Frameworks
  • Easily implementing and consuming web services (including REST)
  • Caching
  • Easy integration with many tools such as CXF, Apache Camel, ...

Other than that there are other projects in Spring Portfolio, which handle other things, just name important ones:

  • Spring Security
  • Spring Integration
  • Spring Batch
  • Spring Data

More Information: - Transaction Management Abstraction is what Spring provides to uniform access to different transaction APIs such as JTA, JDBC Transaction, Hibernate Transaction, .... So you can start coding with Hibernate Transaction and change the transaction manager to container managed by an application server without changing any code (just by changing configurations).

  • When working with JDBC based APIs (and which are depended on JDBC) such as Hibernate, JPA, and MyBatis. Spring can handle uniqueness of connection/transaction per thread, so if you do some query using JDBC, some with HQL and some with JPQL in a single transaction all would be executed using a single connection/transaction.

  • When comes to resource management things go ugly, for example when dealing with a JDBC work you shall do something like:

    Connection connection = null;
    PreparedStatement statement = null;
    try {
        connection = dataSouce.getConnection();
        statement = connection.prepareStatement('some query');
    
        statement.executeUpdate();
    } catch(SQLException e) {
        throw new SomeWrapperException(e);
    } finally {
        try {
        //    if (statement != null) {
        //        statement.close();
        //    }
            if (connection != null) {
               connection.close();
            }
        } catch (SQLException e) {
            throw new SomeWrapperException(e);
        }
    }
    

    as you can see there are lots of lines of code just to execute a single query, and I also ignored closing the Statement (if I wanted to release that I should add some more nested try-catch-finally blocks).

    Using Spring JdbcTemplate, Spring handles all steps which are just for resource creation, and releasing. And you just code what is required for business logic.

    Besides JdbcTemplate, there are some other templates available in Spring including JpaTemplate, HibernateTemplate, TransactionTemplate, JmsTemplate, JndiTemplate, RestTemplate, ReflectionUtils,... (and others in some other Spring Portfolio projects). Good news is most of template's callbacks are single-method-interfaces which can be substituted with Java 8 Labmdas if you are using Java 8.

  • Unified hierarchy of data-access-exceptions is another feature when using Spring data-access-templates. So exceptions will be the same if you use JDBC, Hibernate, ..., so changing implementation of data-access-layer does not propagate to other layers.

  • Spring can apply aspects to beans defined in its container. One of the aspects is asynchronous method calls, which is described here.

  • You can use RMI, WebServices for remoting without adding framework specific exceptions (such as RemoteException) or implementing interfaces (such are Remote). In fact your client core is not aware of remoteness of the bean, and being remote or local is a matter of configuration, more info can be seen here.

Spring provides many more features, and was one of the most important things that influenced Java EE. In fact Java EE 5+ starts to get simpler by looking at what Spring has done. For more info you shall dig into docs yourself.

Upvotes: 3

Vidya
Vidya

Reputation: 30310

I would say they are referring to the first two in particular. By comprehensive infrastructure, the Spring documentation means that they provide abstractions over Inversion of Control and other low-level implementations like JDBC. Wiring objects together manually, connecting to databases, dealing with unnecessary checked exceptions, etc. just aren't fun and take away from the real work of your application.

Upvotes: 1

Related Questions