求助sprinig+mybatis配置完web app启动tomcat报错?

发布于 2022-09-12 22:57:56 字数 10796 浏览 22 评论 0

sprinig+mybatis配置完web app启动tomcat报错
org.apache.tomcat.util.modeler.BaseModelMBean.invoke 调用方法[manageApp]时发生异常

网上都是说可能url-pattern路径前没加/
但经过我自己一点点定位发现是在RegisterServlet里,只要我一加上
StudentServiceImpl studentService = (StudentServiceImpl) ctx.getBean("studentService");
再运行tomcat就会报错,注释掉就没问题,另外自己写了个测试类这句代码是能正常获取对象的,也能正确修改数据库,求助

RegisterServlet

package org.yamlapkei.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yamlapkei.domain.Student;
import org.yamlapkei.service.impl.StudentServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//@WebServlet(name = "MyServlet", urlPatterns = "/wtf")
public class RegisterServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    String id = request.getParameter("id");

String name = request.getParameter("name");
String email = request.getParameter("email");
String strAge = request.getParameter("age");
// 创建spring的容器对象
String config="Spring.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
System.out.println("容器对象的信息======="+ctx);
// 获取service
//@@@@@@@@@@@@@@@@@错误在此@@@@@@@@@@@@@@@@@@@@
//StudentServiceImpl studentService = (StudentServiceImpl)ctx.getBean("studentService");
// Student student = new Student();
// student.setId(Integer.parseInt(id));
// student.setName(name);
// student.setEmail(email);
// student.setAge(Integer.valueOf(strAge));
// studentService.addStudent(student);
// 给一个页面
request.getRequestDispatcher("/result.jsp").forward(request,response);
}

protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{
}

}

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress InjectionValueTypeInspection -->
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.or... http://www.springframework.or... https://www.springframework.o... http://www.springframework.or... https://www.springframework.o...">
<!--声明数据源,连接数据库-->
<!-- https://github.com/alibaba/dr...
<context:property-placeholder location="jdbc.properties"/>
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pwd}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="6000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="org.yamlapkei.dao"/>
</bean> <bean id="studentService" class="org.yamlapkei.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings> <typeAliases> <package name="org.yamlapkei.domain"/>
</typeAliases>
<mappers>
<package name="org.yamlapkei.dao"/>
</mappers></configuration>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/j..."
version="4.0">
<servlet> <servlet-name>RegisterServlet</servlet-name>
<servlet-class>org.yamlapkei.controller.RegisterServlet</servlet-class>
</servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name>
<url-pattern>/wtf</url-pattern>
</servlet-mapping></web-app>

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m...">
<modelVersion>4.0.0</modelVersion>
<groupId>org.yamlapkei</groupId>
<artifactId>springweb</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-web</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies> <dependency> <groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <dependency> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency><!-- jsp 依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency> <!-- 做spring事务用到的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency> <dependency> <groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency> <dependency> <groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency> <dependency> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency> <!-- 阿里公司数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency> </dependencies>
<build> <resources> <resource> <directory>src/main/java</directory>
<includes> <include>*/.properties</include>
<include>*/.xml</include>
</includes> <filtering>false</filtering>
</resource> <resource> <directory>src/main/resources</directory>
<includes> <include>*/.properties</include>
<include>*/.xml</include>
</includes> <filtering>false</filtering>
</resource> </resources> <plugins> <!-- 指定jdk版本-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration> <source>1.8</source>
<target>1.8</target>
</configuration> </plugin> </plugins> </build></project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文