从数据库加载映像到应用程序上下文Spring。我用mybatis api

时间:2015-06-08 02:16:22

标签: java spring spring-mvc mybatis applicationcontext

我使用spring-mvc mybatis 3.2.8。我想从数据库中读取一些图像并存储在ApplicationContext上。我有一个dao类来读取图像

dao class

public Interface DaoImage {
   public List<byte[]> selectImage();
}

InitializationService.java

public interface InitializationiService {
public List<byte[]> loadImage();
}

InitializationServiceImpl.java

public class InitializationImpl implements InitializationService{
@Autowoired
private DaoImage img;

@Overrides 
public List<byte[]> loadImage(){
   return img.selectImage();
}

}

InitializeApplication.java

public class InitializationApplication extends ContextLoaderListener{
@Autowired
private InitializationService service;
    @Override
    public void contextInitialized(ServletContextEvent event) {

        List<byte[]> list = service.loadImage();        
        event.getServletContext().setAttribute("contextImage", list);


    }

}

当应用程序启动List list = service.loadImage();一片空白。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

似乎你错过了InitializationServiceImpl.java中的注释定义。

尝试:

@Component("initializationService")
public class InitializationImpl implements InitializationService {
...
}

InitializeApplication.java

@Autowired
@Qualifier("initializationService")
private InitializationService service;

答案 1 :(得分:0)

我现在找到了解决方案:

public class InitializationApplication extends ContextLoaderListener implements ServletContextAware{
@Autowired
private InitializationService service;
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext ctx = event.getServletContext();
        WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(ctx);
        InitializationService  service = (InitializationService)springContext.getBean("InitializationService");
        List<byte[]> list = service.loadImage();        
        event.getServletContext().setAttribute("contextImage", list);

    }

@Override
public void setServletContext(ServletContext servletContext) {
    this.servletContext = servletContext;
}

}