Springboot中使用aop导致@Autowired全部注入失败

记仇

下文略啰嗦,直接讲重点:

aop只能适用于 protect 和public 修饰符修饰的方法


我们使用的Springboot版本号是1.5.9.RELEASE,因为同事写代码不规范导致的BUG

因为系统新的需求,所以需要做AOP日志啥也不说,一顿疯狂的引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

额。。。倒数第二个就是AOP的引用,然后一顿疯狂的切面,大致代码就如下,其他业务代码就不贴了,反正就是切了

    @Pointcut("execution(public * net.wdm.jeeshopmanage.controller..*.*(..))")
    public void webLog() {

    }

然后就是各种注入失败,各种空指针,传说中价值几百万的错误 NullPointerException,非常郁闷,各种检查,各种查询,最后发现大神写的博客

https://blog.csdn.net/wangh92/article/details/79581129

大神博客写到,aop只能适用于 protect 和public 修饰符修饰的方法,因为个人习惯的原因,我的对外的所有controller都是public 的,所以理论上讲,是不会存在这个问题的

但是

防火防盗防同事,嗯,就是这样,部分代码如下

    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    private String selectList(String billNo, HttpServletRequest request) {
        //。。。。。
    }
    @RequestMapping(value = "/selectStatusN", method = RequestMethod.POST)
    private String selectStatusN(String billno, HttpSession session) {
        //。。。。。
    }

抱着试一试的心态,改成了public,然后,然后就好了,这特么,记一笔。

没错,就是记仇

超级记仇

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Spring Boot 提供了很好的支持来使用 AOP。你可以按照以下步骤来使用 AOP: 1. 添加 AOP 依赖 在 pom.xml 文件添加 Spring AOP 依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 创建切面类 切面类是一个普通的 Java 类,其包含了定义切点和通知的代码。你可以使用 @Aspect 注解来标记一个切面类,使用 @Pointcut 注解来定义一个切点,使用 @Before、@After、@Around 等注解来定义通知。 这里是一个简单的示例: ``` @Aspect @Component public class LoggingAspect { @Pointcut("execution(public * com.example.demo.*.*(..))") public void logPointcut() {} @Before("logPointcut()") public void beforeAdvice() { System.out.println("Before advice is executed."); } @After("logPointcut()") public void afterAdvice() { System.out.println("After advice is executed."); } @Around("logPointcut()") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Around advice is executed before method."); Object result = joinPoint.proceed(); System.out.println("Around advice is executed after method."); return result; } } ``` 在上面的代码,@Pointcut 注解定义了一个切点,它匹配了 com.example.demo 包下的所有公共方法。@Before、@After、@Around 注解分别定义了前置通知、后置通知和环绕通知。 3. 启用 AOP 在 Spring Boot 应用启用 AOP 很简单,只需要在启动类上添加 @EnableAspectJAutoProxy 注解即可: ``` @SpringBootApplication @EnableAspectJAutoProxy public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 这样,AOP 就启用了。你可以在应用使用 @Autowired 注解来注入切面类,然后它就会自动执行切点和通知。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值