You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
spring is not loaded before the first request. Can we add static { initialize(); } inside the class AzureFunctionInstanceInjector
/* * Copyright 2021-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */packageorg.springframework.cloud.function.adapter.azure;
importjava.util.Map;
importcom.microsoft.azure.functions.spi.inject.FunctionInstanceInjector;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.springframework.boot.ResourceBanner;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.WebApplicationType;
importorg.springframework.cloud.function.utils.FunctionClassUtils;
importorg.springframework.context.ConfigurableApplicationContext;
importorg.springframework.core.io.DefaultResourceLoader;
importorg.springframework.util.ClassUtils;
importorg.springframework.util.CollectionUtils;
/** * The instance factory used by the Spring framework to initialize Azure function instance. It is waived with the Azure * Java Worker through the META-INFO/services/com.microsoft.azure.functions.spi.inject.FunctionInstanceInjector service * hook. The Azure Java Worker delegates scans the classpath for service definition and delegates the function class * creation to this instance factory. * @author Christian Tzolov * @since 3.2.9 */publicclassAzureFunctionInstanceInjectorimplementsFunctionInstanceInjector {
privatestaticLoglogger = LogFactory.getLog(AzureFunctionInstanceInjector.class);
privatestaticConfigurableApplicationContextAPPLICATION_CONTEXT;
static {
initialize();
}
/** * This method is called by the Azure Java Worker on every function invocation. The Worker sends in the classes * annotated with @FunctionName annotations and allows the Spring framework to initialize the function instance as a * Spring Bean and return the instance. Then the Worker uses the created instance to invoke the function. * @param functionClass the class that contains customer Azure functions (e.g. @FunctionName annotated ) * @param <T> customer Azure functions class type * @return the instance that will be invoked on by azure functions java worker * @throws Exception any exception that is thrown by the Spring framework during instance creation */@Overridepublic <T> TgetInstance(Class<T> functionClass) throwsException {
try {
// Backward compatibility workaround. If the function class is of type FunctionInvoker then create plain// Java instance and delegate to FunctionInvoker adaptor approach.if (ClassUtils.isAssignable(FunctionInvoker.class, functionClass)) {
returnfunctionClass.getDeclaredConstructor().newInstance();
}
initialize();
Map<String, T> azureFunctionBean = APPLICATION_CONTEXT.getBeansOfType(functionClass);
if (CollectionUtils.isEmpty(azureFunctionBean)) {
thrownewIllegalStateException(
"Failed to retrieve Bean instance for: " + functionClass
+ ". The class should be annotated with @Component to let the Spring framework initialize it!");
}
returnazureFunctionBean.entrySet().iterator().next().getValue();
}
catch (Exceptione) {
if (APPLICATION_CONTEXT != null) {
APPLICATION_CONTEXT.close();
}
thrownewIllegalStateException("Failed to initialize", e);
}
}
/** * Create a static Application Context instance shared between multiple function invocations. */privatestaticvoidinitialize() {
synchronized (AzureFunctionInstanceInjector.class.getName()) {
if (APPLICATION_CONTEXT == null) {
Class<?> springConfigurationClass = FunctionClassUtils.getStartClass();
logger.info("Initializing: " + springConfigurationClass);
APPLICATION_CONTEXT = springApplication(springConfigurationClass).run();
}
}
}
privatestaticSpringApplicationspringApplication(Class<?> configurationClass) {
SpringApplicationapplication = neworg.springframework.cloud.function.context.FunctionalSpringApplication(
configurationClass);
application.setWebApplicationType(WebApplicationType.NONE);
application.setBanner(newResourceBanner(
newDefaultResourceLoader().getResource("classpath:/spring-azure-function-banner.txt")));
returnapplication;
}
}
The text was updated successfully, but these errors were encountered:
spring is not loaded before the first request. Can we add static { initialize(); } inside the class AzureFunctionInstanceInjector
The text was updated successfully, but these errors were encountered: