You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring MVC项目中启用@RequiresPermissions注解?

兄弟,我太懂这种卡了好几天摸不着头脑的感觉!研究Spring AOP确实容易在配置细节上栽跟头,先帮你把代码格式化清楚,再给你几个核心排查方向参考:

先看你贴的dispatcher-servlet.xml代码(已格式化,注意原内容有截断):

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?xml version=".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/ao...

几个关键排查点:

  • AOP命名空间是否完整:你贴的代码里xmlns:aop明显截断了,完整的命名空间应该是xmlns:aop="http://www.springframework.org/schema/aop",同时必须在xsi:schemaLocation里加上对应的XSD地址:
    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"
    
    这是Spring识别AOP配置的基础,少了直接会导致AOP不生效。
  • 切面类的配置是否正确:
    • 如果用XML配置:有没有定义切面类的Bean,有没有用<aop:config>包裹切面和切点,比如:
      <bean id="myAspect" class="com.yourpackage.MyAspect"/>
      <aop:config>
          <aop:aspect ref="myAspect">
              <aop:pointcut id="myPointcut" expression="execution(* com.yourpackage.service.*.*(..))"/>
              <aop:before pointcut-ref="myPointcut" method="beforeAdvice"/>
          </aop:aspect>
      </aop:config>
      
    • 如果用注解方式:切面类有没有加@Aspect和@Component注解,Spring容器有没有扫描到这个类(比如配置了<context:component-scan base-package="com.yourpackage.aspect"/>)。
  • 切点表达式是否准确:检查execution表达式有没有匹配到你要增强的方法,比如方法权限、包路径、类名、方法名、参数有没有写错,这是AOP能不能命中目标方法的关键。
  • 通知方法是否符合要求:比如@Before注解的方法参数有没有正确绑定(如果需要获取目标方法信息的话),XML配置的通知方法名有没有和切面类里的方法名一致。

如果能把完整的XML代码和你的切面类代码贴出来,能更精准地定位问题哦!

内容的提问来源于stack exchange,提问作者Bibek Khadka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:08:41