Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring boot AOP特性导致autwired bean返回null_Java_Spring Boot_Aop_Autowired - Fatal编程技术网

Java Spring boot AOP特性导致autwired bean返回null

Java Spring boot AOP特性导致autwired bean返回null,java,spring-boot,aop,autowired,Java,Spring Boot,Aop,Autowired,使用@Around注释定义方面会导致bean的自动连接返回null。我知道这与aspectj设置的某种代理有关,这可以通过一些注释来解决。我只是不知道这些注释是什么,也不知道应该放在哪里。这只发生在@Around方面,而不发生在@Before或@After方面 @SpringBootApplication public class AopStuffApplication implements CommandLineRunner{ @Autowired private Busin

使用
@Around
注释定义方面会导致bean的自动连接返回null。我知道这与aspectj设置的某种代理有关,这可以通过一些注释来解决。我只是不知道这些注释是什么,也不知道应该放在哪里。这只发生在@Around方面,而不发生在@Before或@After方面

@SpringBootApplication
public class AopStuffApplication implements CommandLineRunner{

    @Autowired
    private Business1 business1;

    private static final Logger LOGGER = Logger.getLogger(AopStuffApplication.class.getName());

    public AopStuffApplication() {}

    public static void main(String[] args) {
        SpringApplication.run(AopStuffApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        LOGGER.info(business1.calculateSomething());
    }
}




导致bean的自动连接返回null:这意味着什么。在执行这段代码时,您希望发生什么?确切地说,会发生什么?在您做任何其他事情之前,请首先修复您的方面。它有返回类型
void
,但应该有
对象
字符串
。它还必须返回与它截获的方法匹配的内容,或者直接返回
procedure()
的结果,或者返回其他兼容的值。
@Service
public class Business1 {

    @Autowired
    private Dao1 dao1;

    public String calculateSomething() {
        return dao1.retrieveSomething();
    }
}
@Repository
public class Dao1 {

    public String retrieveSomething() {
        System.out.println("Inside of retrieveSomething().");
        return "Dao1";
    }
}
@Aspect
@Component
public class MethodExecutionCalculationAspect {

    private static final Logger LOGGER = Logger.getLogger(MethodExecutionCalculationAspect.class.getName());

    @Around("execution(* com.training.AOPStuff.aopstuff.data.Dao1.retrieveSomething(..))")
    public void trackElapsedTime(ProceedingJoinPoint proceedingJoinPoint) {

        //start time
        long startTime = System.currentTimeMillis();

        //allow execution of method
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //end time
        long elapsedTime = System.currentTimeMillis() - startTime;

        System.out.println("message from within gthe aspect.");

        //LOGGER.info("Elapsed time for " + proceedingJoinPoint + " was " + elapsedTime);

    }

}