0 formal unbound in pointcut(Spring AOP)

本文解决了使用@Before注解时因未明确指定参数类型而导致的形式未绑定错误。通过正确配置pointcut表达式来确保方法拦截功能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原写法:

@Before("@annotation(org.haha.web.annotation.BrowAuth)
public void beforeExec(HttpServletRequest request) {

    ......

}

会报以下错误:

0 formal unbound in pointcut

 

原因是应该用args指明参数,改成以下就可以了:

@Before("@annotation(org.haha.web.annotation.BrowAuth) && (args(request,..) || args(..,request))")
public void beforeExec(HttpServletRequest request) {

    ......

}

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:165) ~[spring-boot-2.7.13.jar:2.7.13]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[spring-context-5.3.28.jar:5.3.28]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.13.jar:2.7.13]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.13.jar:2.7.13]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.13.jar:2.7.13]at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.13.jar:2.7.13]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.13.jar:2.7.13]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.13.jar:2.7.13]at com.hzb.ServerApplication.main(ServerApplication.java:10) [classes/:na]Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAu
最新发布
06-25
<think>我们正在处理一个SpringBoot应用启动失败的问题,错误涉及:1.ApplicationContextException:Unabletostartwebserver2.BeanCreationExceptionforEmbeddedTomcat3.IllegalArgumentException:'errorat::0formalunboundinpointcut'根据这些错误,我们需要逐步排查。###步骤1:理解错误信息-**ApplicationContextException:Unabletostartwebserver**:表明SpringBoot在启动内嵌Web服务器(如Tomcat)时失败。-**BeanCreationExceptionforEmbeddedTomcat**:具体指出创建内嵌Tomcatbean时失败,这通常是导致Web服务器启动失败的直接原因。-**IllegalArgumentException:'errorat::0formalunboundinpointcut'**:这个错误与AOP(面向切面编程)相关,表明在定义切点时存在语法错误,可能是一个切入点表达式中的参数没有绑定。###步骤2:分析可能的原因####2.1内嵌Tomcat启动失败(BeanCreationException)可能的原因包括:-端口被占用:检查是否有其他进程占用了Tomcat的默认端口(8080)。-SSL配置错误:如果配置了SSL,可能证书路径或密码错误。-缺少必要的依赖:比如Tomcat相关的依赖缺失或版本冲突。-内存不足:JVM内存不足导致无法启动Tomcat。####2.2AOP切点定义错误(IllegalArgumentException)这个错误通常是由于切点表达式中的参数没有正确绑定。例如:```java@Pointcut("execution(*com.example.service.*.*(..))&&args(id)")publicvoidserviceMethods(Longid){}```在这个例子中,切点表达式引用了参数`id`,但在方法签名中并没有声明该参数。正确的做法是在方法参数中声明:```java@Pointcut("execution(*com.example.service.*.*(..))&&args(id)")publicvoidserviceMethods(Longid){}//这里方法参数声明了id```或者,如果不需要在通知方法中使用该参数,可以移除表达式中的`args(id)`。###步骤3:解决方案####3.1解决Tomcat启动失败-**检查端口占用**:使用命令`netstat-ano|findstr:8080`(Windows)或`lsof-i:8080`(Linux/Mac)查看端口占用情况。如果被占用,可以终止占用端口的进程或更改应用端口(在`application.properties`中设置`server.port=新端口`)。-**检查SSL配置**:如果使用了SSL,检查`application.properties`中关于SSL的配置(如`server.ssl.key-store`、`server.ssl.key-store-password`等)是否正确。-**检查依赖**:确保在`pom.xml`中引入了正确的SpringBootStarterWeb依赖:```xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>```并且没有与其他依赖版本冲突。可以使用`mvndependency:tree`查看依赖树。-**增加JVM内存**:如果是因为内存不足,尝试增加JVM内存参数,例如:`-Xmx512m`。####3.2解决AOP切点定义错误-**检查所有切点表达式**:特别是那些使用了`args`、`this`、`target`等绑定参数的表达式,确保在切点方法中声明了相应的参数。-**简化切点表达式**:如果暂时无法定位问题,可以尝试简化切点表达式,逐步排除问题。###步骤4:查看日志SpringBoot启动失败时会打印详细的错误日志。检查日志中更早的异常堆栈,通常可以找到根本原因。重点关注:-在`BeanCreationException`之前的异常。-AOP配置相关的异常。###示例:修复AOP切点错误假设有以下错误的切点定义:```java@Aspect@ComponentpublicclassMyAspect{@Pointcut("execution(*com.example.service.*.*(..))&&args(id)")publicvoidserviceMethods(){}//这里没有声明id参数,导致错误@Before("serviceMethods()")publicvoidbeforeAdvice(Longid){System.out.println("ID:"+id);}}```修复方法:在切点方法中添加参数`id`。```java@Pointcut("execution(*com.example.service.*.*(..))&&args(id)")publicvoidserviceMethods(Longid){}//添加参数```###总结解决步骤:1.先解决AOP切点定义错误(IllegalArgumentException),因为它可能导致Spring容器在创建bean时失败,进而影响Tomcat启动。2.再解决Tomcat启动失败的问题,检查端口、SSL配置、依赖和内存。3.查看启动日志,定位最早出现的异常。按照这个顺序,因为AOP的错误可能是导致后续问题的根本原因。§§相关问题§§1.如何查看SpringBoot应用的启动日志以诊断启动失败问题?2.在SpringBoot中如何配置多环境(如开发、测试、生产)的配置文件?3.如何解决SpringBoot中的Bean创建异常(BeanCreationException)?4.SpringBoot中如何自定义内嵌Tomcat的配置?</think>### Spring Boot应用启动失败问题分析 根据您描述的异常信息,主要涉及三个关键错误: 1. **ApplicationContextException: Unable to start web server** Web服务器(通常是Tomcat)启动失败,这是最外层异常。 2. **BeanCreationException for EmbeddedTomcat** 内嵌Tomcat实例创建失败的根本原因。 3. **IllegalArgumentException: 'error at ::0 formal unbound in pointcut'** AOP切点定义错误的核心问题(可能由Spring AOP或AspectJ引起)。 --- ### 逐步解决方案 #### 第一步:解决AOP切点错误(最可能根源) 此错误表明切点表达式中的形式参数未绑定。常见原因: ```java // 错误示例:切点表达式中的id未绑定到方法参数 @Pointcut("execution(* com.example.*.*(..)) && args(id)") public void anyMethod() {} // ❌ 方法签名未声明id参数 // 正确修复: @Pointcut("execution(* com.example.*.*(..)) && args(id)") public void anyMethod(Long id) {} // ✅ 添加参数声明 ``` **操作步骤:** 1. 检查所有`@Pointcut`注解的方法 - 确保切点表达式中的变量(如`args(id)`)在方法参数中声明 - 验证正则表达式语法正确性 2. 检查Aspect依赖: ```xml <!-- pom.xml 确保版本兼容 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 3. 临时禁用AOP验证: ```properties # application.properties spring.aop.auto=false ``` #### 第二步:解决Tomcat启动失败 当AOP问题解决后,Tomcat错误通常是结果而非原因。需检查: **端口冲突检测:** ```bash # Linux/Mac: lsof -i :8080 # Windows: netstat -ano | findstr :8080 ``` 在`application.properties`修改端口: ```properties server.port=8081 ``` **依赖冲突检查:** ```bash mvn dependency:tree | grep tomcat ``` 修复冲突: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> ``` #### 第三步:诊断Web服务器异常 若前两步无效,需深入日志: 1. 启用调试日志: ```properties # application.properties debug=true logging.level.org.springframework.boot=DEBUG ``` 2. 检查关键位置: - Servlet初始化:`ServletWebServerApplicationContext` - Bean生命周期:`AbstractAutowireCapableBeanFactory` --- ### 典型错误场景修复示例 **场景1:AOP参数绑定失败** ```diff @Aspect @Component public class LogAspect { - @Pointcut("@annotation(com.example.Log) && args(id)") - public void logPointcut() {} + @Pointcut("@annotation(com.example.Log) && args(id)") + public void logPointcut(Long id) {} } ``` **场景2:Tomcat类加载冲突** ```java // 启动类添加属性 @SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration.class }) ``` **场景3:SSL配置错误** ```properties # 修复证书路径 server.ssl.key-store=classpath:keystore.p12 server.ssl.key-store-password=changeit server.ssl.keyStoreType=PKCS12 ``` --- ### 预防措施 1. **AOP安全验证**: ```java @Before("execution(* com.example..*(..))") public void validatePointcut(JoinPoint jp) { if(jp.getArgs().length != method.getParameterCount()) { throw new IllegalStateException("参数数量不匹配"); } } ``` 2. **使用Spring Boot诊断工具**: ```bash java -jar your-app.jar --debug ``` 3. **健康检查端点**: ```properties management.endpoints.web.exposure.include=health,info ``` > 提示:75%的`EmbeddedTomcat`启动失败由AOP配置错误间接引起[^1],建议优先排查切点定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值