我昨天设置了Memcached并最终获得了Spring @Cachable
注释...
这段代码完美缓存:
@Component("CacheProcessor")
public class CacheProcessor {
@Cacheable(value = "defaultCache", key="'dateTime-'.concat(#anything)")
public String getDateTime2(String anything) {
Date d = new Date();
String response = Long.toString(d.getTime());
return response;
}
}
我尝试了为每个请求调用的缓存方法:
@org.springframework.cache.annotation.Cacheable(value="defaultCache", key="username")
public static List<Session> findSessionByUserName(String username) {
String hql = "SELECT o FROM Session AS o WHERE o.username=:username";
TypedQuery<Session> query = Session.entityManager().createQuery(hql, Session.class);
query.setParameter("username", username);
return query.getResultList();
}
...但我接受了堆栈跟踪而不是缓存结果:
java.lang.IllegalStateException: Post-processor tried to replace bean instance of type [com.ahp.core.model.Session] with (proxy) object of type [com.sun.proxy.$Proxy66] - not supported for aspect-configured classes!
at org.springframework.beans.factory.wiring.BeanConfigurerSupport.checkExposedObject(BeanConfigurerSupport.java:168)
at org.springframework.beans.factory.wiring.BeanConfigurerSupport.configureBean(BeanConfigurerSupport.java:140)
at org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect.configureBean(AnnotationBeanConfigurerAspect.aj:60)
at org.springframework.beans.factory.aspectj.AbstractDependencyInjectionAspect.ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(AbstractDependencyInjectionAspect.aj:91)
at com.ahp.core.model.Session.<init>(Session.java:20)
at com.ahp.core.model.Session.entityManager_aroundBody0(Session.java:57)
at com.ahp.core.model.Session.entityManager(Session.java:1)
at com.ahp.core.processor.AccountProcessor.validateSession(AccountProcessor.java:545)
at com.ahp.core.processor.WarehouseProcessor.consume(WarehouseProcessor.java:93)
at com.ahp.core.processor.WarehouseProcessor.consume(WarehouseProcessor.java:1)
at com.ahp.messaging.processor.AbstractRPCConsumer.onMessage(AbstractRPCConsumer.java:32)
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:228)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:756)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:679)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:82)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:167)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1241)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:660)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1005)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:989)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:82)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1103)
at java.lang.Thread.run(Thread.java:744)
如果它有帮助,这里是Session类的片段,它是由Spring Roo生成的,然后使用Push-In重构,我摆脱了所有的AspectJ所以现在它只是一个Java类:
@Entity
@Configurable
//@RooJavaBean
//@RooToString
//@RooJpaActiveRecord
public class Session {
@PersistenceContext
transient EntityManager entityManager;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Version
@Column(name = "version")
private Integer version;
@ManyToOne
private Account account;
@ManyToOne
private Company company;
@Column(name = "qusername")
private String username;
....
如果我删除Configurable
注释,则实体管理器不会被注入,并且会抛出IllegalStateException
。
public static final EntityManager entityManager() {
EntityManager em = new Session().entityManager;
if (em == null)
throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
return em;
}
如何在aspect-configured
类上进行缓存?
如果不可能,那么在没有方面的情况下使其工作的程序是什么(假设它会使其可以缓存)?
工作解决方案
必须创建一个SessionService接口:
@RooService(domainTypes = {Session.class })
public interface SessionService {
public List<Session> findSessionByUserName(String username);
}
SessionServiceImpl:
@Component
public class SessionServiceImpl implements SessionService {
@Override
@Cacheable(value="defaultCache", key="#username")
public List<Session> findSessionByUserName(String username) {
return Session.findSessionByUserName(username);
}
}
必须将Session类修改为可序列化并返回Roo注释。
@Entity
@RooJavaBean
@RooToString
@Configurable
@RooJpaActiveRecord
public class Session implements Serializable {
...
}
然后使用我自动连接的SessionService:
@Autowired
SessionService sessionService;
我现在可以使用缓存:
sessionService.findSessionByUserName(...)
答案 0 :(得分:2)
问题是您正在尝试将缓存添加到静态方法(在Roo ActiveRecord模式中完成的方式)。您必须在bean方法(非静态)上配置缓存层。
一种方法可以是使用Roo生成在实体层和Web层之间创建服务层(查看Roo documentation)。该实用程序创建Spring bean,它调用Roo实体方法并修改Contollers方法以使用它。因此,您可以使用@Cacheable
注释此Service-Bean的方法,以便在需要的地方使用缓存。