国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Integrating Spring and EHCache

Using Spring Modules and EHCache,one can transparently cache method results. Spring Modules uses a proxywhich intercepts call to the method of bean; consults the cache tocheck if method was called with same parameters before, if so willreturn cached result.

EHCache is the actual provider of caching solution and Spring Module handles method interception and result storing in cache.

Since I am using Maven2, so added following dependancies -


01.<dependency>
02.    <groupId>net.sf.ehcache</groupId>
03.    <artifactId>ehcache</artifactId>
04.    <version>1.6.1</version>
05.</dependency>
06.<dependency>
07.    <groupId>org.springmodules</groupId>
08.    <artifactId>spring-modules-cache</artifactId>
09.    <version>0.9</version>
10.</dependency>

My complete pom.xml -


01.<?xml version="1.0" encoding="UTF-8"?>
02.<project xmlns="

Contact Table sql -


1.CREATE TABLE `contact` (
2.  `ID` int(15) NOT NULL AUTO_INCREMENT,
3.  `FIRSTNAME` varchar(50) DEFAULT NULL,
4.  `LASTNAME` varchar(50) DEFAULT NULL,
5.  `EMAIL` varchar(150) DEFAULT NULL,
6.  PRIMARY KEY (`ID`),
7.  UNIQUE KEY `EMAIL` (`EMAIL`)
8.) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

Adding ehcache namespace to Spring config file -

Created ehcache.xml file in classpath.


1.<ehcache>
2.    <defaultCache
3.            maxElementsInMemory="500" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LFU"/>
4.    <cache name="contactCache" maxElementsInMemory="500" eternal="true" overflowToDisk="false"
5.           memoryStoreEvictionPolicy="LFU"/>
6.</ehcache>

Adding caching to application-context.xml –


01.<?xml version="1.0" encoding="UTF-8"?>
02.<beans xmlns="
26.    </bean>
27. 
28.    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
29.        <constructor-arg type="javax.sql.DataSource" ref="datasource"/>
30.    </bean>
31.</beans>

Here is my test class -


01.package byteco.de.spring;
02. 
03.import org.junit.Test;
04.import org.junit.runner.RunWith;
05.import org.springframework.beans.factory.annotation.Autowired;
06.import org.springframework.dao.DuplicateKeyException;
07.import org.springframework.test.annotation.ExpectedException;
08.import org.springframework.test.context.ContextConfiguration;
09.import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10. 
11.import java.util.List;
12. 
13.import static org.junit.Assert.assertTrue;
14. 
15.@RunWith(SpringJUnit4ClassRunner.class)
16.@ContextConfiguration(value = "/application-context.xml")
17.public class ContactDAOTest {
18. 
19.    @Autowired
20.    ContactDAO contactDAO;
21. 
22.    @Test
23.    public void shouldCreateNewContact(){
24.         assertTrue(contactDAO.createContact("gabbar","singh","gabbar@singh.com"));
25.         assertTrue(contactDAO.createContact("kalia","singh","kalia@singh.com"));
26.    }
27. 
28.    @Test
29.    public void shouldReturnListOfContact(){
30.        assertTrue(contactDAO.findAll().size()>0);
31.        assertTrue(contactDAO.findAll().size()>0);
32.        assertTrue(contactDAO.findAll().size()>0);
33.        // This will cause the cache to be cleared. refer to logs.
34.        assertTrue(contactDAO.createContact("samba","singh","samba@singh.com"));
35.        assertTrue(contactDAO.findAll().size()>0);
36.        assertTrue(contactDAO.findAll().size()>0);
37.        assertTrue(contactDAO.findAll().size()>0);
38.    }
39.}

Executed test shouldCreateNewContact first then shouldReturnListOfContact. Below is the console output -


01.DEBUG[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor]- Autowiring by type from bean name 'byteco.de.spring.ContactDAOTest'to bean named 'contactDAO'
02.DEBUG[org.springframework.beans.factory.support.DefaultListableBeanFactory] -Returning cached instance of singleton bean'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
03.DEBUG[org.springframework.beans.factory.support.DefaultListableBeanFactory] -Returning cached instance of singleton bean'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
04.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toretrieve a cache entry using key <2056181503|2056171012> and cachemodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
05.DEBUG [org.springmodules.cache.provider.ehcache.EhCacheFacade] - Retrieved cache element <null>
06.DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL query
07.DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL statement [select * from contact]
08.DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
09.DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
10.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt tostore the object <[0. 1 gabbar singh , 1. 2 kalia singh ]> in thecache using key <2056181503|2056171012> and model<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
11.DEBUG [org.springmodules.cache.provider.ehcache.EhCacheFacade] - Object was successfully stored in the cache
12.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toretrieve a cache entry using key <2056181503|2056171012> and cachemodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
13.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Retrievedcache element <[0. 1 gabbar singh , 1. 2 kalia singh ]>
14.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toretrieve a cache entry using key <2056181503|2056171012> and cachemodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
15.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Retrievedcache element <[0. 1 gabbar singh , 1. 2 kalia singh ]>
16.DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL update
17.DEBUG[org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQLstatement [insert into contact (firstname,lastname,email) values(?,?,?)]
18.DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
19.DEBUG [org.springframework.jdbc.core.JdbcTemplate] - SQL update affected 1 rows
20.DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
21.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toflush the cache using model<org.springmodules.cache.provider.ehcache.EhCacheFlushingModel@2dd59d3c[cacheNames={'contactCache'},flushBeforeMethodExecution=false]>
22.DEBUG [org.springmodules.cache.provider.ehcache.EhCacheFacade] - Cache has been flushed.
23.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toretrieve a cache entry using key <2056181503|2056171012> and cachemodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
24.DEBUG [org.springmodules.cache.provider.ehcache.EhCacheFacade] - Retrieved cache element <null>
25.DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL query
26.DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL statement [select * from contact]
27.DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
28.DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
29.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt tostore the object <[0. 1 gabbar singh , 1. 2 kalia singh , 2. 3 sambasingh ]> in the cache using key <2056181503|2056171012> andmodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
30.DEBUG [org.springmodules.cache.provider.ehcache.EhCacheFacade] - Object was successfully stored in the cache
31.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toretrieve a cache entry using key <2056181503|2056171012> and cachemodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
32.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Retrievedcache element <[0. 1 gabbar singh , 1. 2 kalia singh , 2. 3 sambasingh ]>
33.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Attempt toretrieve a cache entry using key <2056181503|2056171012> and cachemodel<org.springmodules.cache.provider.ehcache.EhCacheCachingModel@1531164[cacheName='contactCache',blocking=false, cacheEntryFactory=null]>
34.DEBUG[org.springmodules.cache.provider.ehcache.EhCacheFacade] - Retrievedcache element <[0. 1 gabbar singh , 1. 2 kalia singh , 2. 3 sambasingh ]>

Here are my DAO classes –


1.package byteco.de.spring;
2.import java.util.List;
3.public interface ContactDAO {
4.    boolean createContact(String firstName, String lastName, String email);
5.    List findAll();
6.}

01.package byteco.de.spring;
02. 
03.import org.springframework.jdbc.core.JdbcTemplate;
04.import org.springframework.jdbc.core.RowMapper;
05.import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
06. 
07.import java.sql.ResultSet;
08.import java.sql.SQLException;
09.import java.util.HashMap;
10.import java.util.List;
11.import java.util.Map;
12. 
13.public class ContactDAOImpl implements ContactDAO {
14. 
15.    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
16. 
17.    public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
18.        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
19.    }
20. 
21.    public boolean createContact(String firstName, String lastName, String email) {
22.        Map<String,String> map = new HashMap<String, String>();
23.        map.put("firstname",firstName);
24.        map.put("lastname",lastName);
25.        map.put("email",email);
26.        int i = namedParameterJdbcTemplate.update("insert into contact (firstname,lastname,email) values (:firstname,:lastname,:email)", map);
27.        return i>0;
28.    }
29. 
30.    public List findAll() {
31.        return namedParameterJdbcTemplate.query("select * from contact", (Map<String, ?>) null, new RowMapper() {
32.            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
33.                return i + ". " + resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3) + " ";
34.            }
35.        });
36.    }
37.}

We are caching the result of findAll and clearing cache when createContact is executed.


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
生活服務(wù)
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服