I tried to create and initialize automatically a database into MySQL called "spring" to store users and authorities, so I created the files schema.sql and data.sql in the "resources" folder (see the scripts below). When I run the program, it throws an Exception declaring that "datasource" bean could not be instantiated as the "spring" database, which I tried to create, is not found !!
PS: The program works fine, when I switch into the in-memory database H2, but when I switch to MySQL the problem fails as described above.
It seems to me that spring boot is trying to create the "dataSource" bean before executing the SQL scripts for the database for the case of MySQL.
Could anyone explain to me what's really happening ?
Here's program involving the datasource bean :
package com.javamaster.springsecurityjdbc.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.JdbcUserDetailsManager;
import javax.sql.DataSource;
@Configuration
public class SecurityConfig {
@Bean
public UserDetailsService userDetailsService(DataSource dataSource){
return new JdbcUserDetailsManager(dataSource);
}
@Bean
public PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
}
schema.sql
create database spring;
CREATE TABLE IF NOT EXISTS `spring`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
`enabled` INT NOT NULL,
PRIMARY KEY (`id`));
CREATE TABLE IF NOT EXISTS `spring`.`authorities` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NOT NULL,
`authority` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`));
data.sql
INSERT IGNORE INTO `spring`.`authorities` VALUES (NULL, 'tom', 'write');
INSERT IGNORE INTO `spring`.`users` VALUES (NULL, 'tom', '123456', '1');
and application.properties
spring.datasource.url=jdbc:mysql://localhost/spring?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.initialization-mode=always
spring.jpa.generate-ddl=true?