Spring AOP切面类依赖注入失败问题解决
来源:互联网 发布:奥尼尔nba数据 编辑:程序博客网 时间:2024/05/17 02:22
最近在项目中使用到了Spring AOP结合AspectJ注解为项目增加系统操作日志记录功能,遇到的问题是在切面类中使用注解的方式注入Service对象失败,导致日志记录功能无法使用。报如下空指针异常:
第一行是日志切面类中的方法,第二行是Controller中的方法。
我觉得应该是spring加载配置文件时是有顺序引起的,但是不知道是什么顺序。。
待解决,至今没找到解决方法。。。
update 2017-5-25 22:11:43
目前的解决办法是通过实现一个ApplicationContext工具类进行手动注入,不知道还有没有更好的办法。
package com.june.util; import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; @Componentpublic class ApplicationContextUtils implements ApplicationContextAware { private static ApplicationContext applicationContext; private ApplicationContextUtils(){} @Override public void setApplicationContext(ApplicationContext context) throws BeansException { applicationContext = context; } public static <T> T getBean(Class<T> beanClass) { return applicationContext.getBean(beanClass); } public static <T> T getBean(String beanName, Class<T> beanClass) { return applicationContext.getBean(beanName, beanClass); } }
在切面类中使用:
@Aspect@EnableAspectJAutoProxy(proxyTargetClass = true)public class SysOperateLogAspect { private SysOperateLogService sysOperateLogService; public SysOperateLogAspect() { this.sysOperateLogService = ApplicationContextUtils.getBean(SysOperateLogService.class); }}
希望能找到更好的办法。
阅读全文
0 0
- Spring AOP切面类依赖注入失败问题解决
- Spring框架运行机制(控制反转,依赖注入,面向切面AOP)
- 【Spring in action】依赖注入DI和AOP切面
- IOC (依赖注入) 与 AOP (面向切面)
- Spring学习,依赖注入(DI)、控制反转(IoC)和面向切面(AOP)
- Spring框架总结,控制反转(IOC),依赖注入(DI),面向切面编程(AOP)
- Spring + SpringMVC其他类依赖注入失败
- Spring 的切面编程AOP如何实现 注入切面程序
- Spring 依赖注入失败原因
- spring依赖注入失败原因
- 切面(aop)控制反转和依赖注入(IOC,DI)和spring的事务隔离和传播行为
- spring依赖注入和Aop的理解
- Spring IOC 依赖倒置 AOP面向切面编程
- Spring IOC注入和AOP切面(1)
- Spring依赖注入与面向切面编程以及注解
- Spring 使用AOP导致IOC注入失败
- Spring Bean注入失败的问题解决过程
- 【Spring】AOP - 面向切面
- USACO 3.2 E Magic Squares 题解
- IDEA入门级使用教程
- Java泛型:泛型类、泛型接口和泛型方法
- LeetCode 198 House Robber (dp)
- 冒泡排序法(JAVA版)
- Spring AOP切面类依赖注入失败问题解决
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- Corn Fields POJ
- Spring+MyBatis
- 1002. 写出这个数 (java)
- Unity3d5.6 SpriteRenderer high SpriteMeshGenerator.TraceShape app Crash
- Python计算大文件crc32值
- 【转】ubuntu 安装指定版本的docker
- loushang5关于DataSet通过后台查询出的数据如何映射到前台JSP页面的下拉列表中