我正在 Azure Functions 中编写 @TimerTrigger 函数。为此,我使用了 Spring Cloud Function for Azure。问题是每次访问服务或 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() {}
}
函数 HelloHandler.java 文件是:
@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...");
}
}
服务 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 功能。