Spring Boot Initialization Order
How can it be fixed to initialize (create) enities in the DB before the class is called SecurityConfiguration
?
1. Simple application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. Example entity
@Component
@Entity
@Table(name = "users")
public class Users {
...
}
3. SecurityConfiguration
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private DataSource datasource;
@Autowired
protected void setDatasource(DataSource datasource) {
this.datasource = datasource;
}
...
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
...
if (!userDetailsService.userExists("user")) {
...
}
}
}
I tried using something like @DependsOn({"users", "authorities"})
in SecurityConfiguration
, but it didn't help.
Getting an exception from the method configure
because the table was not found. If I don't touch db my tables will be created there.
EDIT: Possible solution
My first pom.xml
had dependencies like this:
<artifactId>spring-boot-starter-security</artifactId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Adding data dependencies-restececile solved my problem:
<artifactId>spring-boot-starter-data-rest</artifactId>
The tables are now created before the call SecurityConfiguration
. With no exceptions. But still I don't understand how (why) this data problem depends on this problem.
source to share
No one has answered this question yet
Check out similar questions: