简体   繁体   中英

@Autowire return null after adding Spring AOP

I have spring boot project , that is working very fine . But when i add the spring AOP it causes me nullpointer when i used the @Autowired variable .

Here is my code for the 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..*.*(..))")

So I guess Spring AOP excludes your MyAspect class to avoid infinite recursion.

Try to narrow down the @Around poincut to a single method first to see if it works.

After that use Point cut annotation to exclude your logTimeMethod from being advised.

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:

<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:

   @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.

@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. Both classes should be beans, ie they should be defined to live in the application context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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