spring AOP代理错误

编程入门 行业动态 更新时间:2024-10-19 22:23:16

spring AOP代理<a href=https://www.elefans.com/category/jswz/34/1771449.html style=错误"/>

spring AOP代理错误

1 报错信息

Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userService' is expected to be of type 'com.yang.service.UserServiceImpl' but was actually of type 'com.sun.proxy.$Proxy5'at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:392)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)at com.yang.service.MyTest.main(MyTest.java:19)

其中,IOC容器内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:context=""xmlns:aop=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans.xsd://www.springframework/schema/context/spring-context.xsd://www.springframework/schema/aop/spring-aop.xsd"><context:annotation-config/><!--第一步,先注册bean--><bean id="userService" class="com.yang.service.UserServiceImpl"></bean><bean id="log" class="com.yang.log.Log"></bean><bean id="afterlog" class="com.yang.log.AfterLog"></bean><!--第二步,配置aop:需要在文件头导入支持--><aop:config>
<!--切入点,然后写表达式,表面在哪里类的方法进行切入,需要表面方法,参数等等情况--><aop:pointcut id="pointcut" expression="execution(* com.yang.service.UserServiceImpl.*(..))"/><!--    执行环绕增加--><aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor><aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"></aop:advisor></aop:config></beans>

2 分析

spring AOP只能代理接口,不能代理具体的实现类。故需要修改下面的代码:

package com.yang.service;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @ClassName MyTest* @Author yang* @Description //TODO* @Date 22/07/23 16:36* @Version v1.0**/public class MyTest {public static void main(String[] args) {ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");// 动态代理代理的是接口,不能写成实现类,故下面这个不行UserServiceImpl userService = classPathXmlApplicationContext.getBean("userService", UserServiceImpl.class);userService.add();System.out.println();}
}

正确的写法为:

package com.yang.service;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @ClassName MyTest* @Author yang* @Description //TODO* @Date 22/07/23 16:36* @Version v1.0**/public class MyTest {public static void main(String[] args) {ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");// 动态代理代理的是接口,不能写成实现类,故下面这个不行UserService userService = classPathXmlApplicationContext.getBean("userService", UserService.class);userService.add();System.out.println();}
}

更多推荐

spring AOP代理错误

本文发布于:2024-02-06 16:42:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1750369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   spring   AOP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!