Best Practice of Spring Framework

xiaoxiao2021-03-06  34

Spring Framework has received more and more attention from the date of birth. Recently, new open source projects support Spring Framework. There are also special websites in China (http://spring.jactiongroup.net/). Then why is it so popular? What I think is the most important thing is that EJB has a hate everyone. To write an EJB, you need to write localhome, removehome, bean, localinterface, remoteInterface, require a standard descriptor, a special manufacturer descriptor (WebLogic, WebSphere is not the same), if it is an ENTITY BEAN, a mapping file is required. So much, it is really trouble. But EJB is the most important thing to solve the Transaction problem. No other methods can describe it without Spring. Everyone, each company in order to solve the Transaction problem, the programming is different, and the flowers are released. So, Spring appeared when it is most needed. Spring has a lot of features. But for a product, the most important thing is how to use it. Spring contains several parts of AOP, ORM, DAO, Context, Web, and MVC. WEB, MVC is not considered, with mature struts, JSP or Webwork better. DAO can also be considered due to the popularity of Hibernate, JDO. So the most needed is AOP, ORM, and Context. In Context, the most important thing is BeanFactory, which is very important to separate interfaces and implementation. I used to write the program, such as an interface IDocument, an implementation class Document1. When writing a program, you need to write idocument doc = new document1 (), once our implementation needs to change, change to Document2, the program needs to be written as idocument doc = new document2 (), all used locations need to be changed. Beanfactory helped us solve this problem. After using Context, it is written to idocument doc = (idocument) beanfactory.getBean ("DOC"). If the implementation class is changed from Document1 to Document2, it can be changed directly in the configuration file. Context is a further abstraction of Bean Factory. Many people like to use ApplicationConext and use servlet to put it LOAD. This is bound to the bean factory with the web. If it is a Fat Client or Remote call, these bean factories is difficult to call, which is actually tighter with the business layer with the business layer. The recommended method is SingletonBeanFactoryLocator.

Specifically: BeanFactoryLocator bfLocator = SingletonBeanFactoryLocator.getInstance (); BeanFactoryReference bf = bfLocator.useBeanFactory ( "beanFactory"); // now use some bean from factory return bf.getFactory () getBean (name); dataAccessContext.xml securityContext.xml . .. can be dynamically expanded, and component development can be implemented. Spring framework is the most famous is the seamless link with Hibernate. It is basically used to use hibernate. Unfortunately, the HibernateTemplate function provided by Spring is not enough. It is not very convenient to use. When we program, you generally write businessService first, by BusinessService Tune DAO to perform storage, Spring does not have a good example in this area, causing truth to use it, it is not easy.

Our idea is to write a BaseDao, modeled HibernateTemplate, all the realization of the basic functions: public class BaseDao extends HibernateDaoSupport {private Log log = LogFactory.getLog (getClass ()); public Session openSession () {return SessionFactoryUtils.getSession (getSessionFactory ( ), false);} public Object get (Class entityClass, Serializable id) throws DataAccessException {Session session = openSession (); try {return session.get (entityClass, id);} catch (HibernateException ex) {throw SessionFactoryUtils.convertHibernateAccessException ( ex);}} public Serializable create (Object entity) throws DataAccessException {Session session = openSession (); try {return session.save (entity);} catch (HibernateException ex) {throw SessionFactoryUtils.convertHibernateAccessException (ex);}}. .. Other DAO, inherited from Basedao, write other DAOs, the code will be very small. Inheriting from Basedao Entitydao, it is more convenient to be responsible for the basic operation of general entities.

public interface EntityDao {public Object get (Class entityClass, Serializable id) throws DataAccessException; public Object load (Class entityClass, Serializable id) throws DataAccessException; public Serializable create (Object entity) throws DataAccessException; ...} / ** * Base class .. for Hibernate DAOs This class defines common CRUD methods for * child classes to inherit User Sping AOP Inteceptor * / public class EntityDaoImpl extends BaseDao implements EntityDao {} in order to control the Transaction using AOP way: public interface EntityManager {public Object get ( class entityClass, Serializable id); public Object load (class entityClass, Serializable id); public Serializable create (Object entity);. ...} / ** * Base class for Entity Service User Sping AOP Inteceptor * / public class EntityManagerImpl implements EntityManager {Private EntityDao EntityDao; public void setentitydao (entitydao entitydao) {this.entitydao = entitydao;} public object get (Class Entitycla) SS, Serializable ID) {Return EntityDao.get (EntityClass, ID);} public object load (class entryclass, serializable id) {return entitydao.load (entryclass, id);} ...} We have a general purpose The Hibernate entity engine can achieve basic increase, modification, deletion, query, etc. for any Hibernate entity. Other BusinessService can inherit EntityManager and quickly implement business logic.

The specific XML configuration is as follows: Java: Comp / env / jdbc / testpool User-hbm.xml net.sf.hibernate.diaalect.raclediaalect net.sf.ehcache.hib> true false com.gpower.services.entity.dao.EntityDao HibernateInterceptor EntityDaotarget <-! Transaction manager that Delegates to JTA (for a transactional jndi datasource) -> -> <-! Transactional proxy for the Application primary business object -> <

转载请注明原文地址:https://www.9cbs.com/read-64967.html

New Post(0)