Spring in Practice Note: Spring in the back end

Source: Internet
Author: User
Tags exception handling

I. Using object-relational mappings to persist data

Object/Relationship mapping (object-relational mapping,orm).

Using ORM Tools at the persistence layer can save code and a lot of development time.

Spring's support for ORM frameworks provides an integration point with these frameworks as well as some additional services:

--Support for integration of spring declarative transactions;

--transparent exception handling;

--thread-safe, lightweight template class;

--DAO support class;

--Resource management.


1.Spring data Access

Programming for interfaces in accordance with the object-oriented principle. Put the data Access feature in the component that is focused on this task, that is, the data Access object (DAO) or repository.

The template method mode is used. The template method defines the main framework of the process. The template method delegates the part of the process to the interface that is related to the specific implementation, and the different implementations of the interface define the specific behavior in the process.

Spring divides the fixed and mutable portions of the data access process into templates (template) and callbacks (callback). A fixed part of the template management process, callback handles the custom data access code.

Spring provides a data access template that applies to different persistence mechanisms, respectively.

Template Class (org.springframework.*) Use
Jca.cci.core.CciTemplate JCA CCI Connection
Jdbc.core.JdbcTemplate JDBC Connection
Jdbc.core.namedparam.NamedParameterJdbcTemplate JDBC connections that support named parameters
Jdbc.core.simple.SimpleJdbcTemplate Simplified JDBC Connection via JAVA5 (obsolete in Spring3.1)
Orm.hibernatetemplate Hibernate 3.x or more session
Orm.ibatis.SqlMapClientTemplate IBATIS Sqlmap Client
Orm.jdo.JdoTemplate Java Data Object Implementation
Orm.jpa.JpaTemplate Entity manager for the Java Persistence API

DBPC Basicdatasource Pool Configuration properties:

Pool Configuration Properties The content that is specified
InitialSize Number of connections created at pool startup
Maxactive The maximum number of connections that can be allocated from the pool at the same time. If set to 0, indicates no limit
Maxidle The maximum number of idle connections that will not be freed in the pool. If set to 0, indicates no limit
Maxopenpreparedstatements The maximum number of preprocessing statements (prepared statement) that can be allocated from the statement pool at the same time. If set to 0, indicates no limit
Maxwait The maximum time the pool waits for a connection to be reclaimed before throwing an exception (when there is no available connection). If set to-1, indicates infinite wait
Minevictableidletimemillis Maximum time that a connection remains idle in the pool and not reclaimed
Minidle The minimum number of connections that remain idle in the pool without creating a new connection
Poolpreparedstatements Whether to pool management (Boolean) for Preprocessing statements (prepared statement)

Using @repository annotations when creating beans automatically

2. Integrating hibernate in spring

The primary interface required to use Hibernate is org.hibernate.Session. The session interface provides basic data access capabilities, such as saving, updating, deleting, and loading objects from the database. By changing the interface, the repository of the application can meet all the requirements of persistence.

The standard way to get Hibernate session objects is through the implementation class of the Hibernate Sessionfactory interface. In addition to some other tasks, sessionfactory is primarily responsible for opening, closing, and managing hibernate sessions.

@Repository is another structural annotation of spring, which can be scanned by spring's components like other annotations. This eliminates the need to declare the hibernatespitterrepository bean, as long as the Repository class is in the package covered by the component scan In addition, a persistentexceptiontranslationpostprocessor bean catch exception is added to the spring application context, which is a bean post processor. It adds a notifier (advisor) on all classes that have @repository annotations, which captures any platform-related exceptions that are re-thrown in the form of spring non-checked data access exceptions.

@Transcational indicates that the persistence method in this resposity is executed in the context of the transaction.


Two. Using a NoSQL database

Some of the best representations of data are documents. It is more meaningful to collect data information into a non-normalized (document) structure.

The document database does not apply to data that has a rich association relationship.

Using MongoDB in 1.Spring applications:

--Implement object-document mapping through annotations;

--using Mongotemplate to implement template-based database access;

--Automated run-time repository generation capabilities (recommended).

@EnableMongRepositories: Enable the repository feature of MongoDB.

2.Spring Data Redis provides a connection factory for four Redis client implementations:

--Jedisconnectionfactory

--Jredisconnectionfactory

--Lettuceconnectionfactory

--Srpconnectionfactory

Redistemplate (or stringredistemplate) simplifies Redis data access and enables the persistence of various types of key-value.

Three. Cache data

The cache (Caching) can store information that is often used.

1. Enable support for caching:

--annotation-driven caching

@Configuration @enablecaching--Enable cache public class Cachingconfig {@Bean public cachemanage    R CacheManager () {--Declare cache manager return new Concurrentmapcachemanager (); }}

--Caching of XML declarations

<cache:annotation-driven/>--Enable cache <bean id= "CacheManager" class= "org.springframework.cache.concurrent . Concurrentmapcachemanager ">--Declaration Cache Manager

Spring built-in Cache manager:

--Simplecachemanager

--Noopcachemanager

--Concurrentmapcachemap

--Compositecachemap

--Encachecachemanager

--Rediscachemanager

--Gemfirecachemanager



Four. Protection method Application

1. Annotation Method Protection

Spring Security provides three types of safety annotations:

-Spring security comes with the @secured;

--@rolesallowed annotation of JSR-250;

--expression-driven annotations, including @preauthorize, @PostAuthorize, @PreFilter, and @postfilter.

Similar to the @rolesallowed scenario, @Secured can restrict access to the other method based on the permissions granted by the user.

@PreAuthorize and @postauthorize can define more flexible security rules on a method.

@PreFilter and @postfilter can filter the collection of first-level incoming methods returned by the method.

annotations Description
@PreAuthorize
@ Postauthorize allows method invocation, but throws a security exception if the expression evaluates to False
@PreFilter Allow method invocation, but you must filter the result of the method by expression
@Post Filter Allow method calls, but must filter input values before entering the method


Spring in Practice Note: Spring in the back end

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.