最近在搭spring项目框架的时候,遇到一个很伤的问题,翻了很多帖,都报告说什么少spring-context包啊之类的,但实际上spring的那些依赖我根本没漏,下面是我的pom:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
先试了用srping-test组件去测试spring-mybatis是否有配置上的问题:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-mybatis.xml") public class MyBatisTest { @Autowired private MarketReportServiceI marketReportService; @Test public void test() { //从Spring容器中根据bean的id取出我们要使用的userService对象 MarketActivityReportWeek report = marketReportService.getReportById("88f8589b-d6fd-11e6-8da9-005056af50a8"); System.out.println(report.getMarketActivityName() + " : " + report.getDealerName()); } }
这样跑下来确实能在控制台打印出数据库中的结果,证明spring配置应该是没有问题的,但项目在tomcat死活启动不了,搞了老半天,一直在web.xml和jar包上纠结,后来直接到发布路径下面去直接找到它,发现jar包没有部署上去(WEB-INF/lib不存在),然后重新修改了一下项目的发布设置:
这时候发现jar包上去了,项目也启动起来了,总算把这茬给搞定了,伤脑筋啊,一开始找的方向不对,始终查不出问题的根源所在,整个项目都差点被推翻重搭了,记下来希望对朋友们有帮助!