Skip to content

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

Closed
@benneq

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

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