Skip to content

Boot + Security + Data Rest + Data MongoDB = ApplicationEventMulticaster not initialized #1195

Closed

Description

@benneq

I faced an issue when using Spring Boot 1.1.3 + Spring Security + Spring Data REST + Spring Data MongoDB:

Caused by: java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2c066c9f: startup date [Mon Jun 30 14:37:37 CEST 2014]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:346)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333)
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:307)

I've written a sample Application which only has 5 files, which are all mandatory to reproduce the exception. Here's the maven application: http://we.tl/ei6sHfrDkT

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Import(RepositoryRestMvcConfiguration.class)
public class Application extends SpringBootServletInitializer {
    private static Class<Application> applicationClass = Application.class;
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }
}
// can't start the app with default Spring Boot settings in properties file,
// because it tries to read the password as char[] and has no registered
// Converter for that
@Configuration
@EnableMongoRepositories
public class MongoConfig {
    @Autowired
    Environment env;

    @Bean
    @Primary
    public Mongo mongo() throws Exception {
        return new MongoClient(
                new ServerAddress(
                        env.getRequiredProperty("mongodb.host"),
                        env.getRequiredProperty("mongodb.port", Integer.class)
                        ),
                Arrays.asList(MongoCredential.createMongoCRCredential(
                        env.getRequiredProperty("mongodb.username"),
                        env.getRequiredProperty("mongodb.database"),
                        env.getRequiredProperty("mongodb.password").toCharArray()
                        ))
                );
    }
}
@Document(collection="asset")
public class Asset implements Identifiable<ObjectId> {
    @Id
    private ObjectId id;
    @Override
    public ObjectId getId() { return id; }
    private String filename;
    public String getFilename() { return filename; }
}
public interface AssetRepo extends Repository<Asset, ObjectId> {}
@Component
public class AssetResourceProcessor implements ResourceProcessor<Resource<Asset>> {
    @Autowired
    EntityLinks entityLinks;

    @Override
    public Resource<Asset> process(Resource<Asset> resource) {
        return resource;
    }
}

If you remove the @Autowired EntityLinks and/or the implements ResourceProcessor<...> the exception disappears.

(Additionally of course you can remove Spring Security and/or Spring Data REST from pom.xml and it works, too)

I also tested it with JPA / Hibernate / HikariCP: No problem. Here the I can use my ResourceProcessor with @Autowired EntityLinks.

pom.xml contains:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.3.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
    <jackson.version>2.4.0</jackson.version>
    <servlet-api.version>3.1.0</servlet-api.version>
</properties>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
</dependencies>

EDIT:
It also happens with 1.1.4.BUILD-SNAPSHOT and/or Spring Security 4.0.M1

Activity

  1. dsyer commented on Jun 30, 2014

    @dsyer
    Member

    It might be worth trying a snapshot of Spring Data REST and Spring HATEOAS.

  2. benneq commented on Jun 30, 2014

    @benneq
    Author

    Thank you!

    The problem is Spring Data REST!

    Snapshot 2.1.2 and Snaphop 2.2.0 work.

    Version of Hateoas / Security / Boot don't matter at all.

    Any information when 2.1.2 or 2.2.0 will go stable?

  3. philwebb commented on Jun 30, 2014

    @philwebb
    Member

    Check with @olivergierke. Dijkstra just released SR1 so I imaging that 2.1.2 will be a little while.

  4. odrotbohm commented on Jun 30, 2014

    @odrotbohm
    Member

    Spring Data REST 2.1.2 snapshots are effectively what has been released as 2.1.1 a few hours ago. There's been a lot of improvements in the startup order in this release so you should see this working with Spring Data REST 2.1.1 already. If not, feel free to open up a ticket with SD REST directly.

  5. benneq commented on Jun 30, 2014

    @benneq
    Author

    Yeah I just saw that spring-boot-starter-data-rest depends on SDR 2.1.0.
    Now I told maven to use 2.1.1 and the Exception ist gone!

    Thank you! :)

  6. wilkinsona commented on Jul 1, 2014

    @wilkinsona
    Member

    In the Boot 1.1.4 snapshots, spring-boot-starter-data-rest depends on Spring Data REST 2.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions