applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--组件扫描,排除controller-->
<context:component-scan base-package="com.xiangyun">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!--读取properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--创建连接池注入容器-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
</bean>
<!--spring整合mybatis后控制的创建获取SqlSessionFactory的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
<!--配置连接池-->
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis配置文件的路径-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--mapper扫描配置,扫描到的mapper对象会被注入Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
<property name="basePackage" value="com.xiangyun.dao"></property>
</bean>
<!--开启aop注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--声明式事务相关配置-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
service实现类
package com.xiangyun.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangyun.common.PageResult;
import com.xiangyun.dao.UserDao;
import com.xiangyun.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ImplUserService implements UserService{
@Autowired
private UserDao userDao;
@Override
public User findById(Integer id) {
return userDao.findById(id);
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public PageResult findByPage(Integer pageSize, Integer pageNum) {
PageHelper.startPage(pageNum,pageSize);
List<User> list = userDao.findAll();
PageInfo pageInfo = new PageInfo(list);
PageResult pageResult = new PageResult(pageInfo.getPageNum(),pageInfo.getPageSize(), (int) pageInfo.getTotal(),list);
return pageResult;
}
@Override
public Integer deleteUser(Integer id) {
return userDao.deleteUser(id);
}
@Override
public Integer insertUser(User user) {
return userDao.insertUser(user);
}
@Override
public Integer updateUser(User user) {
return userDao.updateUser(user);
}
}
部分错误信息
C:\Users\月向安.jdks\corretto-1.8.0_312\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\studentManageFind\studentManagerFind -Dmaven.home=E:\ideaIU-2021.3.1.win\plugins\maven\lib\maven3 -Dclassworlds.conf=E:\ideaIU-2021.3.1.win\plugins\maven\lib\maven3\bin\m2.conf -Dmaven.ext.class.path=E:\ideaIU-2021.3.1.win\plugins\maven\lib\maven-event-listener.jar -javaagent:E:\ideaIU-2021.3.1.win\lib\idea_rt.jar=53469:E:\ideaIU-2021.3.1.win\bin -Dfile.encoding=UTF-8 -classpath E:\ideaIU-2021.3.1.win\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;E:\ideaIU-2021.3.1.win\plugins\maven\lib\maven3\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2021.3.1 -s D:.m2\settings.xml -Dmaven.repo.local=D:.m2\repository org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< org.example:studentManagerFind >-------------------
[INFO] Building studentManagerFind 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ studentManagerFind >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ studentManagerFind ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 7 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ studentManagerFind ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 12 source files to D:\studentManageFind\studentManagerFind\target\classes
[WARNING] /D:/studentManageFind/studentManagerFind/src/main/java/com/xiangyun/service/ImplUserService.java: 某些输入文件使用了未经检查或不安全的操作。
[WARNING] /D:/studentManageFind/studentManagerFind/src/main/java/com/xiangyun/service/ImplUserService.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ studentManagerFind <<<
[INFO]
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ studentManagerFind ---
[INFO] Running war on http://localhost:81/test
[INFO] Using existing Tomcat server configuration at D:\studentManageFind\studentManagerFind\target\tomcat
[INFO] create webapp with contextPath: /test
二月 28, 2022 7:35:27 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-81"]
二月 28, 2022 7:35:27 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
二月 28, 2022 7:35:27 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
二月 28, 2022 7:35:29 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
二月 28, 2022 7:35:29 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
[INFO] Root WebApplicationContext: initialization started
[WARNING] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'implUserService': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDao' defined in file [D:\studentManageFind\studentManagerFind\target\classes\com\xiangyun\dao\UserDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 10; 已经看到 doctype。
19:35:30,543 INFO DruidDataSource:1885 - {dataSource-0} closing ...
[ERROR] Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'implUserService': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDao' defined in file [D:\studentManageFind\studentManagerFind\target\classes\com\xiangyun\dao\UserDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 10; 已经看到 doctype。
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject (AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject (InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties (AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean (AbstractAutowireCapableBeanFactory.java:1411)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0 (AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons (DefaultListableBeanFactory.java:845)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization (AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:549)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (ContextLoader.java:400)
at org.springframework.web.context.ContextLoader.initWebApplicationContext (ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized (ContextLoaderListener.java:103)
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:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
at java.lang.Thread.run (Thread.java:748)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDao' defined in file [D:\studentManageFind\studentManagerFind\target\classes\com\xiangyun\dao\UserDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 10; 已经看到 doctype。
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType (AbstractAutowireCapableBeanFactory.java:1515)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean (AbstractAutowireCapableBeanFactory.java:1395)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0 (AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate (DependencyDescriptor.java:277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency (DefaultListableBeanFactory.java:1251)
网上用错误信息搜索了很多帖子说是@Service注解没加还有说tomcat版本和spring版本不兼容的,但我的pom.xml文件和其他配置文件是另一个ssm项目copy过来的,另外Service注解也加了。
本人刚开始学习ssm框架,这个error已经抓头一整天,望大佬救救孩子/(ㄒoㄒ)/~~
Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 10; 已经看到 doctype。
仔细检查一下配置文件的标签是不是错误了。
把dao层和mapping映射代码发出来,一般这样的错在dao和mapper上
clean一下,重新打包试试
开启注解扫描,也就是你配置文件的第一行 <context:component-scan base-package="com.xiangyun.controller"/>
你service难道不用注入dao吗?这个一般是src下面的扫描