问题描述
我正在Azure函数中编写@TimerTrigger函数。为此,我将Spring Cloud Function用于Azure。问题是,每次访问Service或JPARepository时,我都会得到一个空指针异常。另外@Value(“ $ {VARIABLE}”)给出null,但System.getenv(“ VARIABLE”)确实提供了值。
Main.Java代码为:
@SpringBootApplication
@EnableJpaRepositories("com.xyz.*")
@ComponentScan(basePackages = "com.xyz.*")
@EntityScan("com.xyz.*")
public class Scheduler {
public static void main(String[] args) throws Exception {
SpringApplication.run(Scheduler.class,args);
}
@Bean
public void hello() {}
}
@Component
public class HelloHandler extends AzureSpringBootRequestHandler<Void,Void> {
@Autowired
MyService myService;
@Value("${VARIABLE}")
private String variable;
@FunctionName("hello")
public void execute(@TimerTrigger(name = "timerInfo",schedule = "0 * * * * *") String timerInfo,final ExecutionContext context){
context.getLogger().info("Executing the function..."+variable); // this gives null.
context.getLogger().info(System.getenv("VARIABLE")); // this does provide value.
myService.hello(context); // null pointer exception here.
context.getLogger().info("Here I am...");
}
}
Service MyService.Java是:
@Service
public class MyServiceImpl implements MyService {
@Autowired
private MyRepository myRepository;
public void hello(ExecutionContext context) {
List<User> userList = myRepository.findAll();
for (User user : userList) {
context.getLogger().info("USER: "+user.getId());
}
context.getLogger().info("Process Finished.");
}
}
我正在使用以下依赖项:
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-function-adapter-azure -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-function-web -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
它给出例外:
Executed 'Functions.hello' (Failed,Id=90714488-b07a-41ac-ae37-d120a1a8e732,Duration=372ms)
System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result: Failure
Exception: NullPointerException:
Stack: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
at com.microsoft.azure.functions.worker.broker.JavaMethodExecutorImpl.execute(JavaMethodExecutorImpl.java:54)
at com.microsoft.azure.functions.worker.broker.JavaFunctionbroker.invokeMethod(JavaFunctionbroker.java:57)
at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at com.xyz.scheduler.HelloHandler.execute(HelloHandler.java:26)
... 16 more
.
我不明白为什么它不能按预期使用所有Spring功能。