ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events

后端 未结 2 1172
时光取名叫无心
时光取名叫无心 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.

     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.3.RELEASE</version>
        </parent>
    
         <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-context-support</artifactId>
            </dependency>
            <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-context-support</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
      </dependencies>
    

    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.

    0 讨论(0)
  • 2021-02-13 21:10

    This is kinda stupid but my problem was that I had placed the @SpringBootApplication class (namely App, Application, WebApplication in most projects) inside the default package. Hence, everything was in total disarray.

    I'm not sure why this exact error showed up, but moving the application class to a package sure fixed it.

    0 讨论(0)
提交回复
热议问题