Anotation Basic Use 2

Annotation for Transaction

The @Transactional annotation may be placed before an interface definition, a method on an interface, a class definition, or a public method on a class. However, please note that the mere presence of the @Transactional annotation is not enough to actually turn on the transactional behavior – the @Transactional annotation is simply metadata that can be consumed by something that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior. In the case of the above example, it is the presence of the <tx:annotation-driven/> element that switches on the transactional behavior.

 

@Transactional

public class DocumentDaoImpl extends HibernateDaoSupport implements DocumentDao {

 

<!– enable the configuration of transactional behavior based on annotations –>

<tx:annotation-driven transaction-manager="transactionManager"/>

 

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory"/>

</bean>

 

Annotation for Caches

 

Using annotation for caches could help to reduce a lot of the xml bulk in the xml configuration file (see the earlier article regarding to the osCache. http://mybiblebook.spaces.live.com/Blog/cns!8D4E97688DE1E193!458.entry?sa=752205576) – and should make cache configuration simpler and easier to configure.

 

Caching is now setup on a method by method basis – there are two parts to the configuration:

 

1)    Setup a caching model.

2)    Setup method annotation

 

1.    A caching model is kinda like a group of caching configuration – we can specify various properties- ie:

 

·         An oscache group

·         Cache refresh period

·         cron expression

 

These are grouped under a model ID – eg

 

          <prop key="testCaching">cacheName=testCache</prop>

          <prop key="testCaching">refreshPeriod=20</prop>

 

This would setup a caching model with an ID of ‘testCaching’  – which use a cache group name of ‘testCache’ – refreshing items after 20 seconds.

 

2.    To annotate the method we want to cache – we simply add the following to the implementation (not the interface) –

 

    @Cacheable(modelId = "testCaching")

    public int getDual() {

        try {

            return getJdbcTemplate().queryForInt("select 56 from dual");

        } catch(DataAccessException de) {

            // crap

            return -1;

        }

    }

 

This would setup this method to use the ‘testCaching’ caching model. 

 

The following is an example how you write the configuration in details. This includes what type cache model will be used, what sort of caches are available and etc.

 

<bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

 

<bean id="cacheManager" class="org.springmodules.cache.provider.oscache.OsCacheManagerFactoryBean"/>

 

<bean id="cacheProviderFacade" class="org.springmodules.cache.provider.oscache.OsCacheFacade">

    <property name="cacheManager" ref="cacheManager"/>

    <property name="failQuietlyEnabled" value="false"/>

</bean>

 

<bean id="cachingAttributeSource"

      class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource">

</bean>

 

<bean id="cachingAttributeSourceAdvisor"

      class="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor">

    <constructor-arg ref="cachingInterceptor"/>

</bean>

 

<bean id="cachingInterceptor"

      class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor">

    <property name="cacheProviderFacade" ref="cacheProviderFacade"/>

    <property name="cachingAttributeSource" ref="cachingAttributeSource"/>

    <property name="cachingModels">

        <props>

            <!– short cache –>

            <prop key="shortCacheModel">cacheName=shortCache</prop>

            <prop key="shortCacheModel">refreshPeriod=1000</prop>

 

            <!– medium cache –>

            <prop key="mediumCacheModel">cacheName=shortCache</prop>

            <prop key="mediumCacheModel">refreshPeriod=10000</prop>

 

            <!– long cache –>

            <prop key="longCacheModel">cacheName=longCache</prop>

            <prop key="longCacheModel">cronExpression=0 0 0 1 * ?</prop>

        </props>

    </property>

</bean>

 

Reference:

https://springmodules.dev.java.net/docs/reference/0.8/html/cache.html

Advertisements
This entry was posted in Spring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s