ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events

后端 未结 2 1233
时光取名叫无心
时光取名叫无心 2021-02-13 20:02

I am trying to implement ehcache for my application but when try invoking the server, getting the following error -

java.lang.IllegalStateException         


        
2条回答
  •  轮回少年
    2021-02-13 20:46

    The idea behind spring-boot is that all the configurations are done automatically by spring.

    In you case use the following POM.

     
            org.springframework.boot
            spring-boot-starter-parent
            1.2.3.RELEASE
        
    
         
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
      
        
          junit
          junit
          3.8.1
          test
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
               org.springframework
               spring-context-support
            
            
               org.springframework
               spring-context-support
            
            
                mysql
                mysql-connector-java
                runtime
            
            
            net.sf.ehcache
            ehcache
            2.9.0
        
        
                org.springframework.boot
                spring-boot-starter-data-jpa
            
      
    

    Add the following application.properties in your src/main/resources.

    spring.datasource.url=jdbc:mysql://localhost:3306/soschema
    spring.datasource.username=root
    spring.datasource.password=root123
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    # Specify the DBMS
    spring.jpa.database = MYSQL
    
    # Show or not log for each sql query
    spring.jpa.show-sql = true
    
    # Hibernate settings are prefixed with spring.jpa.hibernate.*
    spring.jpa.hibernate.ddl-auto = update
    spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
    

    Add the Appconfig file,

    package org.arunm.ehcacheconfig;
    
    
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.ehcache.EhCacheCacheManager;
    import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.ClassPathResource;
    
    
    @Configuration
    public class AppConfig {
    
        @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;
        }
    
    }
    

    Add a top level class with the following code,

    package org.ehcachetest;
    
    import java.util.Arrays;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    
    /**
     * Hello world!
     *
     */
    
    @EnableAutoConfiguration
    @ComponentScan
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext ctx = SpringApplication.run(App.class, args);
    
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        }
    }
    

    If you run App the main method you will find that spring has automatically created the sessionFactory, TransactionManager etc for you. There is no need for any manual configuration which means there is no need to add the dispatcherservlet, your hibernate config etc. Everything is managed by spring.

提交回复
热议问题