0

I have project using Spring framework which set container AnnotaionConfigWebApplicationContext and MyBatis framework as SQL mapper.

When I wrote test code in src/test/java for integration test and execute the execute test case, the initialization process for SqlSessionFactoryBean has been failed because it can not read class path resources.

I set config info like the below. in my thought, context read mapper resources through classpath so that web container should be generated nomally. But, spring container can't build beans and inject dependencies to other classes which need dependency for access db using mybatis because sqlSessionFactoryBean can't read mappers.

how can I set spring container normally?

// test setup
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConifg.class, ServletConfig.class})
public class TempServiceTest {


...

in ApplicationConifg.class, I imported MybatisConfig.class

@Configuration
@MapperScan(basePackages = "prj.personal.shop", annotationClass = org.springframework.stereotype.Repository.class)
public class MybatisConfig
{

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource, WebApplicationContext context) throws Exception
    {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("prj.personal.shop");
        factoryBean.setMapperLocations(context.getResources("classpath:/mapper/**/*.xml"));

        return factoryBean;
    }

}

0 Answers0