Problems encountered when deploying memcached aop in spring

Source: Internet
Author: User

Problems encountered when deploying memcached aop in spring

The following problems occur when you deploy the memcached cache:

1. Deploy the xml configuration of memcached, memcached. xml




Socketpool = "pool_0">

Com. alisoft. xplatform. asf. cache. memcached. MemcachedErrorHandler




Failover = "true" aliveCheck = "true" initConn = "5" minConn = "5" maxConn = "250"
Nagle = "false">

127.0.0.1: 11211


 

2. memcached tool

Package com. lqg. memcache;


Import com. alisoft. xplatform. asf. cache. ICacheManager;
Import com. alisoft. xplatform. asf. cache. IMemcachedCache;
Import com. alisoft. xplatform. asf. cache. memcached. CacheUtil;
Import com. alisoft. xplatform. asf. cache. memcached. MemcachedCacheManager;


Public class MemCachedTool {
Private ICacheManager Manager;
Private IMemcachedCache cache;

Public MemCachedTool (){
Manager = CacheUtil. getCacheManager (IMemcachedCache. class, MemcachedCacheManager. class. getName ());
Manager. setConfigFile ("memcached. xml ");
Manager. setResponseStatInterval (5*1000 );
Manager. start ();
Cache = manager. getCache ("mclient_0 ");
}
/**
* Obtain the cache Interface
* @ Return
*/
Public IMemcachedCache getCache (){
Return this. cache;
}

/**
* Put data into Cache
* @ Param key
* @ Param object
*/
Public void put (String key, Object object ){

Cache. put (key, object );
}

/**
* Read data from the cache
* @ Param key
* @ Return
*/
Public Object get (String key ){
Return cache. get (key );
}


}

3. spring configuration of memcached tool


 

4. aop configuration class

Package com. lqg. aop;


Import org. apache. log4j. Logger;
Import org. aspectj. lang. ProceedingJoinPoint;
Import org. aspectj. lang. annotation. Around;
Import org. aspectj. lang. annotation. Aspect;
Import org. aspectj. lang. annotation. Pointcut;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. stereotype. Component;


Import com. lqg. memcache. MemCachedTool;
Import com. lqg. model. user. Admin;


@ Aspect
Public class AdminInterceptor {
Public static final Logger log = Logger. getLogger (AdminInterceptor. class );

// Weave the MemcahcedTool of the cache client.
@ Autowired
Private MemCachedTool memcachedCache;

/*
* Define poincut
*/
@ Pointcut ("execution (* com. lqg. dao. user. AdminDaoImpl .*(..))")
Public void aPointcut (){

}

/**
* The surround equipment is used to intercept queries. If there is data in the cache, it will directly read the data from the cache; otherwise, it will read the data from the database and put the results into the cache.
*
* @ Param call
* @ Param name
* @ Return
*/
@ Around ("aPointcut () & args (username, password )")
Public Admin doFindUserByNameAround (ProceedingJoinPoint call, String username, String password ){
Admin admin = null;
If (memcachedCache. getCache (). containsKey ("findAdminByName _" + username )){
Admin = (Admin) memcachedCache. get ("findAdminByName _" + username );
Log. debug ("read from cache! FindAdminByName _ "+ username );
System. out. println ("read from cache! FindAdminByName _ "+ username );
} Else {
Try {
Admin = (Admin) call. proceed ();
If (admin! = Null ){
MemcachedCache. put ("findAdminByName _" + username, admin );
Log. debug ("cache equipment executed: findAdminByName _" + username );
System. out. println ("cache device executed: findAdminByName _" + username );
}
} Catch (Throwable e ){
E. printStackTrace ();
}
}
Return admin;
}


}

Problems may occur;

1.7. My JDK is, so I tried to use the aspectjrt. jar file of different versions and found that the error still exists! This is not a problem with aspectjrt. jar.

So I downloaded the latest version of aspectjweaver. jar and replaced the original version. The error at: 0 can't find referenced pointcut allAddMethod was successfully solved and the program runs properly. 3.2.error at: 0 formal unbound in pointcut

Solution: remove the parameters in the function notification function. For example

@ Before ("execution (public void com. bjsxt. dao. impl. UserDAOImpl .*(..))")

Public void beforeMethod (Method method ){

System. out. println ("method before ");

}

Change

@ Before ("execution (public void com. bjsxt. dao. impl. UserDAOImpl .*(..))")

Public void beforeMethod (){

System. out. println ("method before ");

}

3.3 @ Pointcut ("execution (* com. lqg. dao. user. AdminDaoImpl .*(..))")
Public void aPointcut (){
}

The red part of Pointcut definition is a space. Otherwise, an error occurs. This is strange.

4. Configure aop in spring


At the same time


5. juint test class

Package com. lqg. dao;


Import static org. junit. Assert .*;


Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. junit. After;
Import org. junit. Before;
Import org. junit. Test;
Import org. junit. runner. RunWith;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. orm. hibernate3.SessionFactoryUtils;
Import org. springframework. orm. hibernate3.SessionHolder;
Import org. springframework. test. context. ContextConfiguration;
Import org. springframework. test. context. junit4.SpringJUnit4ClassRunner;
Import org. springframework. transaction. support. TransactionSynchronizationManager;


Import com. lqg. dao. user. AdminDao;
Import com. lqg. memcache. MemCachedTool;
Import com. lqg. model. user. Admin;
/**
* Why are there too many other users?
* @ Author QiuguoLi
*
*/
@ RunWith (SpringJUnit4ClassRunner. class)
@ ContextConfiguration (locations = "classpath: applicationContext-common.xml ")

Public class DaoSupportTest {
@ Autowired
Private AdminDao adminDao; // DAO
@ Autowired
Private SessionFactory sessionFactory;
Private Session session;
@ SuppressWarnings ("unchecked ")
@ Before
Public void setUp () throws Exception {// please wait until the peak is reached when there are too many attempts when there are too many errors. why are there too many other users?

// DaoSupport = new DaoSupport ();
// DaoSupport = (DaoSupport ) Factory. getBean ("bookItemDao ");
Session = SessionFactoryUtils. getSession (sessionFactory, true );
Session s = sessionFactory. openSession ();
TransactionSynchronizationManager. bindResource (sessionFactory, new SessionHolder (s ));
}
@ After
Public void tearDown () throws Exception {// too many errors have been reported when too many errors have been reported.?

AdminDao = null; // Why are there any bees?
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager. getResource (sessionFactory );
Session s = holder. getSession ();
S. flush ();
TransactionSynchronizationManager. unbindResource (sessionFactory );
SessionFactoryUtils. releaseSession (s, sessionFactory );
// SessionFactoryUtils. closeSessionIfNecessary (s, sessionFactory );
}
@ SuppressWarnings ("unchecked ")
@ Test
Public void testFindIntInt (){
// Map Orderby = new HashMap (1); // zookeeper has just been written into another zookeeper Map without being written into another zookeeper.
// Orderby. put ("createTime", "desc "); // please refer to the following:
String username = "admin ";
String password = "admin ";
Admin admin = adminDao. login (username, password );
System. out. println (admin. getUsername () + "" + admin. getPassword ());
AssertNotNull () when there are too many threads, too many threads?
}

}
5.1 if you want to open a session or join a class, you cannot exchange

5.2 @ RunWith (SpringJUnit4ClassRunner. class)
@ ContextConfiguration (locations = "classpath: applicationContext-common.xml") Remember to write






 


 

 

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.