When I execute following code
AppConfigurationCacheService appCache = getAppContext().getBean("appConfigurationCacheService");
appCache.getAppConfig("LANDING_PAGE");
I get a following exception:
java.lang.String cannot be cast to my.daomodel.AppConfiguration: java.lang.ClassCastException: java.lang.String cannot be cast to my.daomodel.AppConfiguration
at my.service.cache.AppConfigurationCacheService$$EnhancerBySpringCGLI$$8a364268.getAppConfig(<generated>)
However, if I make the getAppConfig return String, then I no longer get the exception. So I'm not sure why getAppConfig method is trying to return String instead of AppConfiguration object. Did I configure the cache incorrectly? Please help.
Below are classes in detail
Thank you in advance.
Entity class
@Component
@Entity
@Table(name="APP_CONFIGURATION")
@SuppressWarnings("serial")
public class AppConfiguration implements Serializable{
@Id
@SequenceGenerator(name"AppConfigIdSeq" sequenceName="APP_CONFIG_ID_SEQ", initialValue=1, allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AppConfigIdSeq")
@Column(name="APP_CONFIG_ID", nullable=false)
private long appConfigId;
@Column(name="CONFIG_KEY", nullable=false)
@Type(type="string")
private String configKey;
@Column(name="CONFIG_VALUE")
@Type(type="string")
private String configValue;
public AppConfiguration(){
}
public long getAppConfigId(){
return appConfigId;
}
public void setAppConfigId(long appConfigId){
this.appConfigId = appConfigId;
}
public String getConfigKey(){
return configKey;
}
public void setConfigKey(String configKey){
this.configKey = configKey;
}
public String getConfigValue(){
return configValue;
}
public void setConfigValue(String configValue) {
this.configValue = configValue;
}
}
Cache manager class
@Configuration
@EnableCaching
public class CaffeineCacheManager extends CachingConfigurerSupport{
@Bean
public CacheManager cacheManager(){
CaffeineCacheManager cacheManager = new CaffeinieCacheManager("appConfigCache");
cacheManager.setCaffeine(
Caffeine.newBuilder()
.initialCapacity(300)
.maximumSize(1000)
.recordStats()
);
return cacheManager;
}
}
Cache service Class
@Service
@CacheConfig(cacheName={"appConfigCache"})
public class AppConfigurationCacheService{
@Autowired
private AppConfigurationDao appConfigurationDao;
@Cacheable(key="#configKey")
public AppConfiguration getAppConfig(String configKey){
AppConfiguration appConfig = appConfigurationDao.getAppConfigurationByKey(configKey);
return appConfig;
}
}