Entries Tagged ‘Spring’:

Spring notes(六) - DAO and ORMapping support

Spring provides all kinds of DAO layers:

  1. JdbcDaoSupport: you can get JdbcTemplate.
  2. HibernateDaoSupport:you can get hibernateTemplate and SessionFactory
  3. JdoDaoSupport: you can get jdoTemplate and PersistenceManagerFactory
  4. JpaDaoSupport: you can get jpaTemplate and EntityManagerFactory
  5. SqlMapDaoSupport: you can get sqlMapDaoTemplate and SqlMapClient

So, you can extend these XxxDaoSupport to expose CRUD APIs.

In spring applicationContext.xml, you should inject Datasource, SessionFactory, PersistenceManagerFactory, EntityManagerFactory, SqlMapClient bean to these XxxDaoSupport descendants respectively, so that you can get their XxxTemplate in your DAO subclasses.
Lets see these XxxTemplates:

JdbcTemplate encapsulates Jdbc operations, so that you can use it handily. The other XxxTemplates encapsulate the respective OR-Mappings.

E.g.  jdbcTemplate.queryForObject(”select **** “);

Next, i will talk about how smartly the XxxTemplates are designed.  They are all best practices of Template-Method.    Please keep your focus on.


Tags: , , - Views: 262 - Trackback -

Leave a Comment

Spring notes(五) - transaction

Spring transaction mechanism supports:

  • programming transaction
  • declaring transaction

I. Programming transaction:

  1. At first, you should define a transactionManager which is the subclass derived from PlatformTransactionManager.
    1. DataSourceTransactionManager: inject datasource instance to it.
    2. JtaTransactionManager: no need to inject anything
    3. HibernateTransactionManager: inject sessionFactory instance to it
    4. JdoTransactionManager: inject persistenceManagerFactory instance to it
  2. Then, inject these transactionManagers to JdbcTemplate, HibernateTemplate, JdoTemplate, sqlMapDaoTemplate respectively. All these templates encapsulate transaction operations, so its handy to use them to do transactions. But you can also use transactionManagers directly.
public void enrollStudentInCourse() {
  transactionTemplate.execute(
    new TransactionCallback() {
      public Object doInTransaction(TransactionStatus ts) {
        try {
          // do stuff
        } catch (Exception e) {
          ts.setRollbackOnly();
        }
        return null;
      }
    }
  );
}

Do transaction without result:

transactionTemplate.execute(
    new TransactionCallbackWithoutResult() {
        public Object doInTransactionWithoutResult(TransactionStatus ts) {
            try {
                // do stuff
            } catch (Exception e) {
                ts.setRollbackOnly();
            }
        }
    }
);

Do transaction with PlatformTransactionManager subclass directly:

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(def);

try {
  // do stuff
} catch (Exception e) {
  transactionManager.rollback(status);
}

transactionManager.commit(status);

II. We do not talk about Declaring Transaction here…


Tags: , , - Views: 278 - Trackback -

Leave a Comment

Spring notes(四) - AOP

AOP in springframework…


Tags: , , - Views: 224 - Trackback -

Leave a Comment

Spring notes(三) - Resource

Resource interface defines the accessor to all various resources, such as file, http resource, ftp resource, class, etc.

Built-in implementations:

  1. UrlResource
  2. ClassPathResource
  3. FileSystemResource
  4. ServletContextResource
  5. InputStreamResource
  6. ByteArrayResource

Tags: , , - Views: 241 - Trackback -

Leave a Comment

Spring notes(二) - Ioc container

IOC container, in spring framework is BeanFactory, contains the following functions:

  1. controls the lifecycle (creation, init, destroy)
  2. controls the scope (like singleton, prototype, request, session, applicationcontext, new in spring)
  3. maintains beans’ dependence

BeanFactory is the base of the other modules in springframework.
Collection type for bean’s property:

  1. list
  2. set
  3. map
  4. props

Lazy init for bean:  <bean id=”xxx” class=”xxx” lazy-init=”true”>
ApplicationContext: a more useful Ioc container which is BeanFactory’s subclass,  is much more suitable for programmers to choose in their Enterprise Applications.

including more functions than BeanFactory:

  1. MessageResource
  2. Resource loading
  3. Event-broadcasting
  4. Load more than one xml files at one time

Tags: , , , - Views: 250 - Trackback -

Leave a Comment

Spring notes(一) - Overview

Spring contains following modules:

springframework

  • Core: the core(beanfactory) of springframework providing bean management
  • Context: based on BeanFactory, but supports more functions, such as i18n, event-broadcasting, resource-loading, mail, validation, etc.
  • DAO: series of interfaces that you can choose to implement DAOs.
  • ORM: corresponding classes for integrating many ORM utilities to springframework.
  • Web/Web MVC
  • AOP: AOP function you can use to log or do transactions.

Tags: , , , - Views: 334 - Trackback -

Leave a Comment