spring3.2+mybatis3.2+maven整合

用maven管理spring+mybatis的项目:

这里主要讲述的是maven中的pom.xml文件的配置,以及在maven构建过程中会碰到的几个问题(我用的是maven4.4的版本):

首先一步一步配置项目所需要的依赖:为了便于管理,最好是版本号和依赖分开来写:

如以下的配置方式:

<properties>
	<!-- spring版本号 -->
	<spring.version>3.2.4.RELEASE</spring.version>
	<!-- mybatis版本号 -->
	<mybatis.version>3.2.4</mybatis.version>
	<!-- log4j日志文件管理包版本 -->
	<slf4j.version>1.6.6</slf4j.version>
	<log4j.version>1.2.9</log4j.version>
	<!--  mysql 版本 -->
	<mysql.version>5.1.29</mysql.version>
	<!-- google json 版本 -->
	<gson.version>2.5</gson.version>
	<!-- junit 版本 -->
	<junit.version>4.11</junit.version>
	<!-- com.google.guava version 版本 -->
	<guava.version>19.0</guava.version>
	<!-- commons-codec 版本 -->
	<commons-codec.version>1.10</commons-codec.version>
  </properties>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>${spring.version}</version>
</dependency>

  关于jar包依赖的配置没什么可说的,下面主要是说说项目打包过程中出现的几个问题:

1:打包后在target目录下找不到项目的spring和mybatis的xml文件;

默认情况下,maven打包时,只会编译.java的文件,而spring.xml的文件管理这所有的类的依赖,所以这个错误就是找不到相应的类;

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userController‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userService‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [bz.sunlight.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userDAO‘ defined in file [D:\DMCworkspace\MavenTest\target\classes\bz\sunlight\dao\UserDAO.class]: Cannot resolve reference to bean ‘sqlSessionFactory‘ while setting bean property ‘sqlSessionFactory‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘class path resource [bz/sunlight/daoMapping/UserDao.xml]‘; nested exception is java.io.FileNotFoundException: class path resource [bz/sunlight/daoMapping/UserDao.xml] cannot be opened because it does not exist
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userService‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [bz.sunlight.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
	... 22 more

  当然项目中不仅只有spring和mybatis的xml文件,还包括sql的映射文件,在pom文件下应该加上以下配置,让maven打包的时候xml文件也能够打包:

<resource>
          <directory>src/main/resources</directory>
          <includes>
              <include>**/*.xml</include>
          </includes>
          <filtering>true</filtering>
        </resource>
        <resource>
          <directory>src/main/java</directory>
          <includes>
              <include>**/*.xml</include>
          </includes>
          <filtering>true</filtering>
        </resource>

  这样就能在打包时包,src/main/resources和src/main/java下的xml文件都打包;

2:问题2:MalformedByteSequenceException:3 字节的 UTF-8 序列的字节 3 无效

如果没有配置编码插件,在打包时是会包这个错误的,只需在pom.xml文件下如下配置就可以了:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
            <encoding>UTF-8</encoding>
        </configuration>
       </plugin>

  这两个问题是maven打包时遇到的问题,也困扰了我很久,终于解决了;

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userController‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userService‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [bz.sunlight.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userDAO‘ defined in file [D:\DMCworkspace\MavenTest\target\classes\bz\sunlight\dao\UserDAO.class]: Cannot resolve reference to bean ‘sqlSessionFactory‘ while setting bean property ‘sqlSessionFactory‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘class path resource [bz/sunlight/daoMapping/UserDao.xml]‘; nested exception is java.io.FileNotFoundException: class path resource [bz/sunlight/daoMapping/UserDao.xml] cannot be opened because it does not existat org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)at java.util.concurrent.FutureTask.run(FutureTask.java:262)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)at java.lang.Thread.run(Thread.java:745)Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userService‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [bz.sunlight.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)... 22 more

时间: 2024-10-12 07:56:32

spring3.2+mybatis3.2+maven整合的相关文章

Mybatis3.2.1整合Spring3.1

Mybatis3.2.1整合Spring3.1 根 据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对 Mybatis3的支持.因此由Mybatis社区自己开发了一个Mybatis-Spring用来满足Mybatis用户整合Spring的需求.下面 就将通过Mybatis-Spring来整合Mybatis跟Spring的用法做一个简单的介绍. MapperFactoryBean 首先,我们需要从Mybatis官网上

struts2.3+spring3.2+mybatis3.2整合及示例代码

原文:struts2.3+spring3.2+mybatis3.2整合及示例代码 源代码下载地址:http://www.zuidaima.com/share/1550463649778688.htm struts2.3+spring3.2+mybatis3.2整合及示例代码 lib包没放里面 lib包下载地址 https://code.google.com/p/searchengineone/downloads/list  源码截图:

Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

[0]README 0)本文旨在 review Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合过程: 1)项目整合所涉及的源代码,please visit  https://github.com/pacosonTang/MyBatis/tree/master/spring4mvc_mybatis3 2)由于晚辈我还不怎么熟悉maven,所以没有用maven依赖各种jar 依赖包来构建项目(编译项目),抱歉:(不过有兴趣的朋友,吧上述的源码down 下来,里面

用Maven整合SpringMVC+Spring+Hibernate 框架

用Maven整合SpringMVC+Spring+Hibernate 框架, 实现简单的插入数据库数据 一.搭建开始前的准备 1.打开MyEclipse新建Maven项目.File>New>Other(或Ctrl+N)>Maven Project:然后我们用default Workspace就行了(注意Location的路径,区分目录名和项目名):然后再Filter中输入webapp,我们选择org.apache.maven.archetypes 下的maven-archetype-we

struts2+hibernate-jpa+Spring+maven 整合(2)

之前的一篇已经讲到了 spring 与struts2 的整合, 其实对于struts2+hibernate-jpa+Spring 之间的整合的文章已经相当多了, 也相当成熟了,只要不是各自的版本不兼容之外,其他的几乎没啥问题, 不行mybatis那样是不是的冒点让人头疼的事情来 下面修改pom.xml ,把hibernate 的jar 关联进来; <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="

【Java EE 学习第82天】【MAVEN整合Eclipse】【MAVEN的一些高级概念】

一.MAVEN整合Eclipse MAVEN是非常优秀,但是总是要开命令行敲命令是比较不爽的,我们已经习惯了使用IDE,所以还有一种将MAVEN整合到Eclipse的方法. 详情查看:http://www.eclipse.org/m2e/,http://download.eclipse.org/technology/m2e/releases 其中,后者也是该插件的更新地址:安装完成之后要重启Eclipse,然后对几项MAVEN的插件进行设置: 找到window->preferences->Ma

struts2+hibernate-jpa+Spring+maven 整合(1)

1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添加如下代码: <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.16

用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能(二)

前一篇写的有些多,大家先看前一篇,传送门 具体的资源已将上传到资源了. 附地址:MySQL.zip启动 用Maven整合SpringMVC+Spring+Hibernate 框架 上文我们直接搭建前的准备和资源配置都写好了,下面进入具体代码编写.承接上文的小3 3.我习惯建立接口,这样对整个项目感觉更合理. (1.)建立IBaseService(业务逻辑层,有的习惯写成BaseServiceI)都可以,都是标注接口的,我只是简单的贴下代码 package com.jesus.sshframewo

Maven 整合 Spring + MyBatis

Maven 整合 Spring + MyBatis 存记录备忘,根据网上搜的孙宇老师的一个视频整理而成 一.搭建工程 使用Intellij搭建工程(由于eclipse对maven支持不如Intellij,所以选用Intellij,eclipse搭建工程相对复杂,可以百度相关文章). 搭建工程十分简单,建好之后直接进入mybatis 与 Spring整合. 二.整合Mybatis 工程搭建好之后已经包含以下jar包,版本为4.1.1.RELEASE 1 <dependency> 2 <gr