DataSourceInitializer and Spring MVC

Viewed 185

I am studying java and spring and try to make my first spring mvc app. At first I made console app without spring mvc I used just only dao and service layers with JdbcTemplate. I have config file where I make Beans for DataSource and DataSourceInitializer for initializing database.

@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("ru.stepev")
@PropertySource("classpath:config.properties")
public class UniversityConfig implements WebMvcConfigurer {

    @Value("${driver}")
    private String driver;

    @Value("${url}")
    private String url;

    @Value("${user}")
    private String user;

    @Value("${pass}")
    private String pass;

    @Value("${schema}")
    private Resource schema;

    @Value("${data}")
    private Resource data;

    @Bean
    public JdbcTemplate jdbcTamplate(DataSource dateSourse) {
        return new JdbcTemplate(dateSourse);
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(pass);
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(schema);
        resourceDatabasePopulator.addScript(data);
        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(dataSource);
        dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
        return dataSourceInitializer;
    }
    
    private final ApplicationContext applicationContext;

    public UniversityConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    
     @Bean
        public SpringResourceTemplateResolver templateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setApplicationContext(applicationContext);
            templateResolver.setPrefix("/WEB-INF/view/");
            templateResolver.setSuffix(".jsp");
            return templateResolver;
        }
    
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

All shemes and data I stores in resourse folder. Without mvc everything were be ok. But when I have added MVC then I got java.io.FileNotFoundException org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/schema.sql]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/schema.sql]. DataSourceInitializer can't find schema.sql when I create my datebase. I have tried to solve this problem but I couldn't. I have next structure of project.enter image description here

I wil be very thaks for help me with this problem and sorry for my bad english.

1 Answers

I've found my mistake. I had an exception because I didn't put the word classpath before the file name in the properties file.

schema=classpath:schema.sql

data=classpath:data.sql
Related