Adding spring-boot-starter-security to Spring Boot application causes error 'entityManagerFactory' or 'persistenceUnitName' is required

Question!

I created a Spring Boot (0.5.0.BUILD-SNAPSHOT) application using Spring Initializr (http://start.spring.io/), and I added a single @RestController, a single CrudRepository interface, and a single @Entity class - nothing complicated. My Maven POM contains the following dependencies:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</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-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

The Application class contains the default:

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

The simple application runs without errors, but I decided to add Spring Security to the POM to secure the management endpoints:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

Now the application won't start, and I get the following:

java.lang.IllegalArgumentException: 'entityManagerFactory' or 'persistenceUnitName' is required

Caused by: java.lang.IllegalArgumentException: 'entityManagerFactory' or 'persistenceUnitName' is required
    at org.springframework.orm.jpa.JpaTransactionManager.afterPropertiesSet(JpaTransactionManager.java:304)
    at org.springframework.orm.jpa.JpaTransactionManager.<init>(JpaTransactionManager.java:141)
    at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.transactionManager(JpaBaseConfiguration.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 18 more

When I remove the spring-boot-starter-security dependency, the application runs fine but without security enabled. What does the error mean? The application already uses JPA and Hibernate without Spring Security enabled.



Answers

There is a bug there. The cause is really deeply technical and related to the internals in a Spring BeanFactory. Look at the Github issue if you want to get some more understanding, but probably you should be able to just refresh your snapshot dependencies and get the fix.

By : Dave Syer


This video can help you solving your question :)
By: admin