Spring boot 带Ehcache的SpringBoot

Spring boot 带Ehcache的SpringBoot,spring-boot,ehcache,Spring Boot,Ehcache,我使用的是spring boot,我已经在程序中配置了ehcache。我的应用程序类看起来像 SprinbootApplication.Class @SpringBootApplication @EnableJpaRepositories(basePackages = { "in.secondlevelcache.persistance"}) @EntityScan(basePackages = { "in.secondlevelcache.entity"}) @ComponentScan(ba

我使用的是spring boot,我已经在程序中配置了ehcache。我的应用程序类看起来像

SprinbootApplication.Class

@SpringBootApplication
@EnableJpaRepositories(basePackages = { "in.secondlevelcache.persistance"})
@EntityScan(basePackages = { "in.secondlevelcache.entity"})
@ComponentScan(basePackages = { "in.secondlevelcache.*"})
@EnableCaching
public class SpringApplicationClass  extends SpringBootServletInitializer{

@Autowired
StudentInter studentInterfaceService;

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

    return application.sources(SpringApplicationClass.class);
}

public static void main(String args[]) throws Exception{

    SpringApplication.run(SpringApplicationClass.class, args);  
}

@Bean
public ServletRegistrationBean jerseyServlet(){
    ServletRegistrationBean register = new ServletRegistrationBean(new ServletContainer(),"/*");
    register.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialize.class.getName());
    return register;
}

@Bean
public CacheManager getEhCacheManager(){
    return  new EhCacheCacheManager(getEhCacheFactory().getObject());
}


@Bean
public EhCacheManagerFactoryBean getEhCacheFactory(){
    EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
    factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
    factoryBean.setShared(true);
    return factoryBean;
}

@Bean
public List<Student> getAllStudentData(){
    System.out.println("Inside the GetAllStudentData of Spring Initalize");
    return studentInterfaceService.getUser();
}
我使用存储库调用实体类

在服务器启动期间,我调用了get user()方法,我可以在日志中看到查询的执行情况,然后我尝试使用rest服务调用相同的方法,我可以在日志中看到相同的查询,但是在这之后,我使用rest服务调用相同的方法,我看不到任何查询

我的问题是
1) 在服务器启动期间,我将数据从db加载到二级缓存,那么为什么在我使用rest服务调用相同的方法时执行查询呢

首先,您可以删除所有ehcache设置,因为从Spring Boot 1.3开始,它是自动配置的。由于您使用的是默认的“ehcache.xml”位置,SpringBoot将自动检测到该位置并为您创建缓存管理器。如果您正在为配置使用不同的位置,那么有一个属性可以教Spring Boot在哪里找到缓存的配置(
Spring.cache.ehcache.config

其次,您不能依赖于
@PostConstruct
中的该功能,因为无法确保所有的后期处理都已经完成(几乎每个拦截器都是如此,)

尝试将该代码移动到安装后的
(从
智能初始化Singleton
界面)。如果您不想这样做,另一个想法是将代码移动到事件侦听器,在应用程序完全加载时调用该事件侦听器:

@EventListener(ContextRefreshedEvent.class)
public void yourCacheStuff() {
   ///
}

SpringApplication.run()之后调用
yourCacheStuff()
内部
main()
,怎么样?
@Service
 @Cacheable("empcache")
public interface StudentInter {


public List<Student> getUser();

public List<LabelMaster> getAllLabelValue(String language);

public List<LabelMaster> getAllLocaleLabelValue();
}
@Entity
@Table(name="Student")
@Access(AccessType.FIELD)
@Cacheable(value = true)
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)

public class Student {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="roll")
private int rollId;

@Column(name="name")
private String name;

@Column(name="address")
private String address;

@Column(name="telephone")
private long phone;

public int getRollId() {
    return rollId;
}

public void setRollId(int rollId) {
    this.rollId = rollId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public long getPhone() {
    return phone;
}

public void setPhone(long phone) {
    this.phone = phone;
}


}
@EventListener(ContextRefreshedEvent.class)
public void yourCacheStuff() {
   ///
}