在web環境中,一般serviceImpl中的dao之類的數據庫連接都由容器啟動的時候創建好了,不會報錯。但是在main中,沒有這個環境,所以需要獲取環境:
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
PianoServiceImpl pianoService = (PianoServiceImpl) ctx.getBean("pianoServiceImpl");
//然后再調用方法
return pianoService.getPriceByBrand(brand);