spring cache expire using bean property - spring

Is there a way of indicating to expire/evict the cache object based on a property of the POJO cached.
In below code, it caches Foo instance. Foo class has a expiresIn property
class Foo {
Date expiresIn;
}
I want to hint to spring to expire the cache based on the value of expiresIn property of cached element. Is this feasible?
#Cacheable("my-cache-key")
Foo getCachedToken(String userName, String password) throws AuthException
My Cache.xml is below:
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<list>
<ref bean="mapCacheManager"/>
</list>
</property>
</bean>
<bean id="mapCacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
<property name="cacheNames">
<list>
<value>my-cache-key</value>
</list>
</property>
</bean>

I guess you missed the section name How can I set the TTL/TTI/Eviction policy/XXX feature? from the reference guide.
This is a cache infrastructure abstraction; It's not a cache provider. ConcurrentMapCacheManager is a very simple implementation we provide for testing purposes or for super-simple use cases. If you need an eviction policy, choose a caching library that does support that. Ehcache, Guava or Hazelcast come to mind. All of them have a CacheManager implementation.

Related

hibernate annotatedClasses in external file

I'm using spring + hibernate in my application.
I need to map the entities that are annoted by hibernate annotations.
I have this configuartion.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.example.repositoryComment.Model1</value>
<value>com.example.repositoryControlUpload.Model2</value>
<value>com.example.repositoryCycleTicketSummary.Model3</value>
</list>
</property>
I'd like that the entities configuration stay in another file.
Exemplo:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
filesThatContainsModels
</list>
</property>
This classes (Model1, Model2, Model3) are annoted by hibernate.
I don't use packagesToScan, because my warmup need to be fast.
There is way for configuration only the class that annoted, but not using packagesToScan?
Thanks
One option at build-time would be to take advantage of annotation processing.
Basically a custom annotation processor will scan your source files at build time and generate a list of all files found to be annotated with #Entity. It takes this list of classes along with an external property file that describes your static SessionFactory configuration and it generates your spring XML file as applicationContext-persistence.xml.
You then just make sure your main applicationContext.xml imports that file for runtime.
Another alternative would actually to use the packagesToScan property. But rather than do what a lot of developers do and point it to the root package of your application, provide the property with a more restrictive list of packages that represent exactly where it should look, helping it avoid inspecting unnecessary classes. For example:
<property name="packagesToScan">
<array>
<value>com.company.application.feature1.persistence</value>
<value>com.company.application.feature2.persistence</value>
...
</array>
</property>
But I honestly think you're over optimizing. If you have this type of bootstrap performance issues, there has to be something else going wrong here to give you cause for concern.
I have worked on a monolithic application with tens of thousands of class files where the scan pointed to the package root of the application and it didn't take any more than a few seconds to bootstrap the Hibernate persistence classes.

How to use AfterAdvice Interface in java

I am new to spring.I know that AfterAdvice will cause the after method to execute whether target method completes or exits with exception, but i am not able to find out any example for it.
As AfterAdvice is a marker interface, I don't know which method i need to define in it's implementation class.
Thanks,
You do not have to implement those interfaces directly. Instead, you use either
Use #After annotation to mark the method you want to it to be called.
Use spring xml bean configuration aop:advice to declare an after advice method
However, if you choose to use ProxyFactoryBean
As you indicate that you want to use ProxyFactoryBean, you can declare the xml like this
<bean id="interceptor"
class="yourimplementation">
</bean>
<bean id="setterAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="interceptor"/>
</property>
<property name="patterns">
<list>
<value>.*set.*</value>
</list>
</property>
</bean>
<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.mycompany.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>setterAdvisor</value>
</list>
</property>
</bean>
For the java implementation, there is no use to implement Advice interface. You should either implement ThrowingAdvice or AfterReturningAdvice. Refer to this for more info.
For more information, you can refer to a couple tutorial guides on Spring AOP and play around with it to get a sense.

How to configure flushMode property of OpenSessionInViewInterceptor of spring 3.1.4

As I am planning to update from "hibernate3" to "hibernate4" & "spring 3.0.5" to "spring 3.1.4".
I have configured OpenSessionInViewInterceptor in spring 3.0.5 so want to configure same in 3.1.4.
But I am not able to configure flushMode in OpenSessionInViewInterceptor of Spring 3.1.4;
My Previous setting for spring 3.0.5 was:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode">
<bean
id="org.springframework.orm.hibernate3.HibernateAccessor.FLUSH_NEVER"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
Now tried to configure same for spring 3.1.4 as below:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode">
<bean
id="org.springframework.orm.hibernate3.HibernateAccessor.FLUSH_NEVER"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
then it throws below exception:
org.springframework.beans.NotWritablePropertyException: Invalid property 'flushMode' of bean class [org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor]: Bean property 'flushMode' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
And there is no similar class found in alternate to org.springframework.orm.hibernate3.HibernateAccessor in spring 3.1.4
So my question is how to set flushMode property of OpenSessionInViewInterceptor of spring 3.1.4 ?
It looks like a mess, with unbound links to property accessors. I'd guess that a copy-paste job was done without much thinking about cleaning things up given the different inheritance hierarchies. I hate it when that happens…
Can you use the Hibernate 3 version instead? Yes, it really does appear to be there; here's the link: org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
Longer term, look more carefully whether the Hibernate 4 code does what you want without specifying the flag at all. Unfortunately, you'll have to ignore the documentation (at least for now) and study the source itself.

Ehcache-Spring-Annotations: How to get cache manager reference?

I am using ehcache-spring-annotations to cache my application data. For this, I have below configuration:
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<ehcache:annotation-driven cache-manager="ehCacheManager" />
I want to get the reference of 'net.sf.ehcache.CacheManager' out of configured bean 'ehCacheManager'. So that, I can manually perform put or remove operations directly in cache using 'CacheManager'.
Any way to get reference of 'net.sf.ehcache.CacheManager' out of 'org.springframework.cache.ehcache.EhCacheManagerFactoryBean'?
I used below code to get the cache manager reference in my class:
#Resource
private CacheManager ehCacheManager;

Dynamic Spring Message Source

I'm about to extend org.springframework.context.support.AbstractMessageSource that will allow me to dynamically add and edit messages in Spring. I'm planning on storing these values in a database. Is there something out there that does this already? Is there a different approach I should think of?
Here are the requirements:
I have to be able to add messages
I have to be able to edit messages
These adds and edits should take place immediately
Sure.
Develop custom MessageSource and set it as parent to existing (for example based on property files).
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message/messages</value>
</list>
</property>
<property name="parentMessageSource">
<bean class=com.example.DatabaseMessageSource"/>
</property>
</bean>

Resources