首页 > 技术文章 > Spring main方法中怎么调用Dao层和Service层的方法

charlottepl 原文

在web环境中,一般serviceImpl中的dao之类的数据库连接都由容器启动的时候创建好了,不会报错。但是在main中,没有这个环境,所以需要获取环境:

ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
        PianoServiceImpl pianoService = (PianoServiceImpl) ctx.getBean("pianoServiceImpl");
//然后再调用方法
        return pianoService.getPriceByBrand(brand);

推荐阅读