前言:
- 如果想直接得到解决办法的话,请直接跳转到解决。
报错:
- java.lang.ClassCastException: class com.sun.proxy.$Proxy16 cannot be cast to class xxxx
17-Apr-2022 01:00:07.007 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [com.zer.controller.LoginServlet] in web application [/account] threw load() exception
java.lang.ClassCastException: class com.sun.proxy.$Proxy16 cannot be cast to class com.zer.service.impl.LoginServiceImpl (com.sun.proxy.$Proxy16 and com.zer.service.impl.LoginServiceImpl are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @12b1cdd2)
at com.zer.controller.LoginServlet.init(LoginServlet.java:36)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4978)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5290)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809)
at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809)
at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1406)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:827)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
代码:
配置文件 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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--配置 数据源 bean-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置 工厂 bean-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置 mapper扫描 bean-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="factory"/>
<property name="basePackage" value="com.zer.mapper"/>
</bean>
<!--配置 业务层 bean-->
<bean id="loginService" class="com.zer.service.impl.LoginServiceImpl">
<!--配置用户登录mapper-->
<property name="loginMapper" ref="loginMapper"/>
</bean>
<!--配置 通知 bean-->
<bean id="before" class="com.zer.advice.MyBefore"/>
<bean id="after" class="com.zer.advice.MyAfter"/>
<!--配置 AOP组装规则-->
<aop:config>
<aop:pointcut id="my" expression="execution(* com.zer.service.impl.LoginServiceImpl.userLoginService(String,String))"/>
<aop:advisor advice-ref="before" pointcut-ref="my"/>
<aop:advisor advice-ref="after" pointcut-ref="my"/>
</aop:config>
</beans>
LoginServlet.java
package com.zer.controller;
import com.zer.pojo.User;
import com.zer.service.LoginService;
import com.zer.service.impl.LoginServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* ClassName: LoginServlet
* Time: 2022-04-15 16:41
* Description:
*
* @author YZ
*/
@WebServlet(value = "/userLogin",loadOnStartup = 1)
public class LoginServlet extends HttpServlet {
private LoginServiceImpl loginService;
//声明init方法,完成Spring资源的初始化加载
@Override
public void init() throws ServletException {
//获取Spring容器对象
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
System.out.println(ac);
//获取业务层对象
loginService = (LoginServiceImpl) ac.getBean("loginService");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//业务代码
}
}
原因:
- 出现这个异常的原因是什么呢?
- 在我们使用 SpringAOP 进行功能扩展的时候,Spring 是通过动态代理的方式来进行扩展的。
- 动态代理的方式有两种:
- 第一种是,jdk 代理
- 第二种是,cglib 代理
- Spring 默认使用的代理方式是 JDK 代理方式。
解决:
重点:我们都知道,jdk 代理的方式是基于接口的,也就是说,当创建的一个虚拟对象去扩展的时候,这个虚拟对象是属于我们真实对象所实现的接口的。所以我们在获取代理对象的时候,不应该使用这个接口的实现类来接,也就是真实对象。而是用真实对象所实现的接口来接。
如果上面的不是很理解的话,我简单的说一下该理解:
我们创建一个接口 MyInterface ,这个接口被两个类 TestA 和 TestB 给实现了,此时上面的那种情况,就相当于,我们把 TestA 强行转为 TestB。那肯定就是会报错的,这样会比较好理解。
方案:
方案一:修改 Servlet
- 把接收对象得到类型该为真实对象所实现的接口
package com.zer.controller;
import com.zer.pojo.User;
import com.zer.service.LoginService;
import com.zer.service.impl.LoginServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* ClassName: LoginServlet
* Time: 2022-04-15 16:41
* Description:
*
* @author YZ
*/
@WebServlet(value = "/userLogin",loadOnStartup = 1)
public class LoginServlet extends HttpServlet {
private LoginService loginService;
//声明init方法,完成Spring资源的初始化加载
@Override
public void init() throws ServletException {
//获取Spring容器对象
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
System.out.println(ac);
//获取业务层对象
loginService = (LoginService) ac.getBean("loginService");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//业务代码
}
}
方案二:修改 applicationcontext.xml
- 使用 cglib 的代理方式
- 因为,cglib 的代理形式是继承制的。
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--配置 数据源 bean-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置 工厂 bean-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置 mapper扫描 bean-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="factory"/>
<property name="basePackage" value="com.zer.mapper"/>
</bean>
<!--配置 业务层 bean-->
<bean id="loginService" class="com.zer.service.impl.LoginServiceImpl">
<!--配置用户登录mapper-->
<property name="loginMapper" ref="loginMapper"/>
</bean>
<bean id="checkAccountService" class="com.zer.service.impl.CheckAccountServiceImpl">
<!--配置信息检验mapper-->
<property name="checkAccountMapper" ref="checkAccountMapper"/>
</bean>
<!--配置 通知 bean-->
<bean id="before" class="com.zer.advice.MyBefore"/>
<bean id="after" class="com.zer.advice.MyAfter"/>
<!--配置 AOP组装规则-->
<aop:config>
<aop:pointcut id="my" expression="execution(* com.zer.service.impl.LoginServiceImpl.userLoginService(String,String))"/>
<aop:advisor advice-ref="before" pointcut-ref="my"/>
<aop:advisor advice-ref="after" pointcut-ref="my"/>
</aop:config>
<!--spring默认使用jdk代理方式,这里是开启cglib代理模式-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>