Spring4.2.4 使用XML配置方式实现AOP切入时出现错误:error at ::0 formal unbound in pointcut
测试代码如下:
public interface IUserService { public void login(String name, String pwd); public void setAdministratorUserName(String name); } public interface IStockService { public long getStockLevel(String level); public void applyDiscounts(Date date, BigDecimal bigDecimal); } public class DefaultUserService implements IUserService { private String administratorUserName; public DefaultUserService() { } public void login() { } public void login(String name) { SecurityContext.setUserName(name); } public void login(String name, String pwd) { SecurityContext.setUserName(name); } public String getAdministratorUserName() { return this.administratorUserName; } public void setAdministratorUserName(String name) { this.administratorUserName = name; } } public class DefaultStockService implements IStockService { public long getStockLevel(String level) { try { if(level == null) { throw new IOException("stock level not null"); } } catch (IOException e) { e.printStackTrace(); } return 1; } public void applyDiscounts(Date date, BigDecimal bigDecimal) { System.out.println(date + " >> " + bigDecimal.byteValue()); } }
public class SecurityContext { private static SecurityContextHolder holder = new SecurityContextHolder(); public static String getUserName() { return holder.getUserName(); } public static void setUserName(String userName) { holder.setUserName(userName); } private static class SecurityContextHolder { private ThreadLocal<String> currentUser = new ThreadLocal<String>(); public String getUserName() { return currentUser.get(); } public void setUserName(String userName) { currentUser.set(userName); } } }
public class AspectBean { public void logCall(Joinpoint joinpoint) { System.out.println("--------------------"); System.out.println(joinpoint); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/ch6/aspectBean.xml"); IUserService userService = (DefaultUserService) ctx.getBean("userService"); IStockService stockServic = (DefaultStockService) ctx.getBean("stockService"); userService.login("admin", "****"); stockServic.getStockLevel("a"); } }
<?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:p="http://www.springframework.org/schema/p" 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 id="userService" class="com.ycx.ch6.section3.service.DefaultUserService"/> <bean id="stockService" class="com.ycx.ch6.section3.service.DefaultStockService"/> <bean id="aspectBean" class="com.ycx.ch6.section3.AspectBean"/> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceCall" expression="execution(* com.ycx.ch6.section3.service.*Service.*(..))"/> <!-- <aop:pointcut id="serviceCall" expression="execution(* com.ycx.ch6.section3.*Service.*.*(..))"/> --> <!-- <aop:pointcut id="serviceCall" expression="execution(void com.ycx.ch6.section3.*Service.*.*(..))"/> --> <aop:aspect id="firstAspect" ref="aspectBean"> <aop:before method="logCall" pointcut-ref="serviceCall"/> </aop:aspect> </aop:config> </beans>
错误日志:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [conf/ch6/aspectBean.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [conf/ch6/aspectBean.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:478)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.ycx.ch6.section3.AspectBean.main(AspectBean.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:275)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:248)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:988)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:472)
... 10 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:267)
... 25 more
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:193)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getMethodMatcher(AspectJExpressionPointcut.java:176)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.buildSafePointcut(AbstractAspectJAdvice.java:187)
at org.springframework.aop.aspectj.AspectJPointcutAdvisor.<init>(AspectJPointcutAdvisor.java:51)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
注:经过debug Spring使用annotation注解时使用的是aopalliance.jar中的Joinpoint接口声明
org.aopalliance.intercept.Joinpoint;
而xml配置时是用的aspectjrt.jar中的Joinpoint接口声明
org.aspectj.lang.JoinPoint;
修改后的代码:
public class AspectBean { // import org.aopalliance.intercept.Joinpoint; // wrong interface import import org.aspectj.lang.JoinPoint // right interface import public void logCall(org.aspectj.lang.JoinPoint joinpoint) { System.out.println("--------------------"); System.out.println(joinpoint); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/ch6/aspectBean.xml"); IUserService userService = (DefaultUserService) ctx.getBean("userService"); IStockService stockServic = (DefaultStockService) ctx.getBean("stockService"); userService.login("admin", "****"); stockServic.getStockLevel("a"); } }
相关推荐
### Spring AOP 入门详解 #### 一、Spring AOP 概述 Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个关键特性,它为开发者提供了在运行时动态添加代码(即横切关注点或切面)到已有...
在实际应用中,ProxyFactoryBean常与Spring AOP的其他概念结合使用,如通知(advice)、切点(pointcut)和切面(aspect)。通知定义了拦截器的具体行为,切点定义了何时应用通知,而切面则是通知和切点的组合。通过...
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。这个"spring aop jar 包"包含了实现这一功能所需的类和接口,...
### Spring AOP面向方面编程原理:AOP概念详解 #### 一、引言 随着软件系统的日益复杂,传统的面向对象编程(OOP)逐渐暴露出难以应对某些横切关注点(cross-cutting concerns)的问题。为了解决这一挑战,面向方面编程...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点问题,如日志、事务管理、安全性等。本示例将简要介绍如何在Spring应用中实现AOP,通过实际的...
Spring AOP,即Spring的面向切面编程模块,是Spring框架的重要组成部分,它提供了一种在不修改源代码的情况下对程序进行扩展和增强的方法。在Java应用中,AOP主要用于日志记录、性能统计、安全控制、事务管理等方面...
3. **基于XML配置的AOP**:在Spring的配置文件中,可以通过`<aop:config>`、`<aop:advisor>`、`<aop:pointcut>`等标签定义切面、通知和切入点。 ### 三、Spring AOP实战 1. **经典代理模式**:通过JDK动态代理或...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,它为Java应用程序提供了声明式的企业级服务,如事务管理、性能监控等。在Spring AOP中,我们可以通过定义切面(Aspect...
**Spring AOP 配置实现详解** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它允许我们通过分离关注点来简化应用程序的开发。在传统的面向对象编程中,业务逻辑与日志记录...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要模块,它通过提供声明式的方式来实现面向切面编程,从而简化了应用程序的开发和维护。在Spring AOP中,我们无需深入到每个...
在这个过程中,Spring AOP 使用 pointcutParser 来解析 Pointcut 配置,并生成相应的 Bean 定义对象。 7. 创建代理:在所有的 Bean 定义对象生成后,Spring AOP 会创建代理对象,以便在后续的流程中使用。在这个...
Spring AOP 和 Spring IOC 是 Spring 框架的两个核心组件,它们对于任何基于 Java 的企业级应用开发都至关重要。Spring AOP(面向切面编程)允许开发者在不修改源代码的情况下,通过“切面”来插入新的行为或增强已...
Spring AOP,全称为Aspect Oriented Programming,是面向切面编程的一种编程范式,它是对传统的面向对象编程(OOP)的一种补充。在OOP中,核心是对象,而在AOP中,核心则是切面。切面是关注点的模块化,即程序中的...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种强大的方式来实现横切关注点,如日志、事务管理、性能监控等,而无需侵入业务代码。下面将详细介绍Spring AOP的注解方式和XML配置方式。 ### ...
**Spring AOP 入门教程** 在编程领域,Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架中的一个重要组成部分,它为开发者提供了一种强大的方式来管理横切关注点,如日志、事务管理、安全性等...
**Spring AOP 实现机制详解** Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许程序员在不修改源代码的情况下,通过“切面”来插入额外的业务逻辑,如日志、事务管理等。AOP的引入极大地提高了代码的...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它允许程序员在不修改源代码的情况下,通过在运行时插入额外的行为(如日志记录、性能监控等)来增强对象的功能。动态代理则是Spring AOP实现的核心技术之一...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点,如日志、事务管理、性能监控等。在Java应用中,AOP通过代理模式实现,使得我们可以将横切关注...
Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许开发者在不修改源代码的情况下,通过插入切面来增强或改变程序的行为。在本教程中,我们将深入探讨Spring AOP的不同使用方法,包括定义切点、通知类型...