为什么我的Spring Boot应用程序在不创建新上下文的情况下不能执行任何Crud功能?

时间:2018-07-27 19:46:30

标签: spring spring-boot jdbc jdbctemplate

我有一个Spring Boot应用程序,我在其中读取多个rfid阅读器,然后使用标签的位置更新数据库。由于某种原因,使它起作用的唯一方法是为每个数据库事务创建一个新的上下文。我通过单击控制器中的链接开始读取标签。然后在读取标签的代码中,我想更新数据库。可以是我的配置吗?

Webconfig类

print(t, m.groups()[0], sep="\n", end="\n\n")

这是我的WebapplicationInitializer:

@Configuration
@ComponentScan(basePackages = {"com.rfidreader","com.rfidreader.config", "com.rfidreader.rfidbusiness.model", "com.rfidreader.rfidbusiness","com.rfidreader.rfidbusiness.service", "com.rfidreader.rfidreaderbusiness.rfidinterface"})
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        // views are in the folder "/WEB-INF/views/"
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setViewClass(JstlView.class);
        // all view files are HTML documents
        viewResolver.setSuffix(".jsp");
        viewResolver.setExposeContextBeansAsAttributes(true);
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/webapp/WEB-INF/views/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
           registry.addViewController("/readersRun/**").setViewName("readersRun");
    }

    @Override
    // configure static file handling
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // DispatcherServlet: forward requests for static resources to the default servlet
        // requests for static resources are forwarded to the default servlet
        configurer.enable();
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/DB");
        dataSource.setUsername("");
        dataSource.setPassword("");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }

    @Bean
    public MyService getMyService(){
        return new MyServiceImpl();
    }
}

0 个答案:

没有答案