简体   繁体   English

在添加Spring AOP之后@Autowire返回null

[英]@Autowire return null after adding Spring AOP

I have spring boot project , that is working very fine . 我有spring boot项目,它工作得很好。 But when i add the spring AOP it causes me nullpointer when i used the @Autowired variable . 但是当我添加spring AOP时,当我使用@Autowired变量时,它会导致我成为nullpointer。

Here is my code for the AOP . 这是我的AOP代码。

@Autowired
private Service service;

private final org.apache.commons.logging.Log log = LogFactory.getLog(this.getClass());

@Around("execution(* com.kpi.ninja..*.*(..))")
public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
    //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    System.out.println("***********************************************");

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        System.out.println("Entering in Method :  " + joinPoint.getSignature().getName());
        System.out.println("Class Name :  " + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("Target class : " + joinPoint.getTarget().getClass().getName());
        Object retVal = joinPoint.proceed();

        stopWatch.stop();


        StringBuffer logMessage = new StringBuffer();
        logMessage.append(joinPoint.getTarget().getClass().getName());
        logMessage.append(".");
        logMessage.append(joinPoint.getSignature().getName());
        logMessage.append("(");
        // append args
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            logMessage.append(args[i]).append(",");
        }
        if (args.length > 0) {
            logMessage.deleteCharAt(logMessage.length() - 1);
        }

        logMessage.append(")");
        logMessage.append(" execution time: ");
        logMessage.append(stopWatch.getTotalTimeMillis());
        logMessage.append(" ms");
        log.info(logMessage.toString());

        System.out.println("***********************************************");

        return retVal;
}

这是显示空指针错误的图像,其中我使用了自动装配变量

You're advising all of your methods: @Around("execution(* com.kpi.ninja..*.*(..))") 您正在建议所有方法: @Around("execution(* com.kpi.ninja..*.*(..))")

So I guess Spring AOP excludes your MyAspect class to avoid infinite recursion. 所以我想Spring AOP排除了MyAspect类以避免无限递归。

Try to narrow down the @Around poincut to a single method first to see if it works. 尽量缩小范围@Around poincut到一个单一的方法先来看看它是否工作。

After that use Point cut annotation to exclude your logTimeMethod from being advised. 之后,使用logTimeMethod注释从建议中排除logTimeMethod

You are having nullpointer because spring cannot initialize the Service class, you need to add the service in spring context xml, so declare a new bean: 您正在使用nullpointer,因为spring无法初始化Service类,您需要在spring上下文xml中添加服务,因此声明一个新bean:

<bean id="service" class="[package].Service"></bean>

If you don't have the context, you should create one and add the annotation in class declaration to set the path location of context.xml: 如果没有上下文,则应创建一个上下文,并在类声明中添加批注以设置context.xml的路径位置:

   @ContextConfiguration(locations = { "classpath:/context.xml" })
   public class YourClass{
       @Autowired
       Service service

      ...methods
   }

Please Make sure that the class in which you are using the @Autowired is annotated with @Component/@Service/@Bean.The code you have posted does not include the initial class definition nor the spring XML so am not sure you have added the annotation in it or not. 请确保使用@Autowired的类已用@ Component / @ Service / @ Bean注释。您发布的代码不包含初始类定义或spring XML,因此不确定您是否已添加是否添加注释。

@Component or @Controller or @Bean or @Service/@Repository 
Class ACB{

@Autowired
private Service service;

.....

}

Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. 通过将一个bean的实例放置到另一个bean的实例的所需字段中来进行自动装配。 Both classes should be beans, ie they should be defined to live in the application context. 这两个类都应为bean,即应将它们定义为存在于应用程序上下文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM