Hi Olga it worked fine till I shutdown Server.
When I restarted it didn’t startup again.
This my implementation. My Config Interface:
package pro.sysonline.contacts.global;
import com.haulmont.cuba.core.config.Config;
import com.haulmont.cuba.core.config.Property;
import com.haulmont.cuba.core.config.Source;
import com.haulmont.cuba.core.config.SourceType;
@Source(type = SourceType.DATABASE)
public interface IContactsConfig extends Config {
@Property("googleApiKey")
String getGoogleApiKey();
}
Extract from my Service Bean (Middleware)
@CompileStatic
@Service(AddressGoogleCompleteService.NAME)
class AddressGoogleCompleteServiceBean implements AddressGoogleCompleteService {
...... some more ......
@Inject IContactsConfig configuration
First Try
@PostConstruct all Bean used by this one should be initialized, even if APP is’nt totally
@PostConstruct
private void initVariables() {
GoogleAPIKey = configuration.getGoogleApiKey()
}
2018-11-05 19:09:39.247 ERROR [localhost-startStop-1] com.haulmont.cuba.core.sys.AbstractWebAppContextLoader - Error initializing application
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contacts_AddressGoogleCompleteService': Invocation of init method failed; nested exception is java.lang.IllegalStateException: ApplicationContext is not initialized
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.haulmont.cuba.core.sys.CubaClassPathXmlApplicationContext.(CubaClassPathXmlApplicationContext.java:27) ~[cuba-global-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.CubaCoreApplicationContext.(CubaCoreApplicationContext.java:26) ~[cuba-core-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AppContextLoader.createApplicationContext(AppContextLoader.java:89) ~[cuba-core-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AppContextLoader.createApplicationContext(AppContextLoader.java:35) ~[cuba-core-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AbstractAppContextLoader.initAppContext(AbstractAppContextLoader.java:62) ~[cuba-global-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.contextInitialized(AbstractWebAppContextLoader.java:77) ~[cuba-global-6.10.3.jar:6.10.3]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4745) [catalina.jar:8.5.23]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207) [catalina.jar:8.5.23]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.5.23]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) [catalina.jar:8.5.23]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) [catalina.jar:8.5.23]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) [catalina.jar:8.5.23]
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1144) [catalina.jar:8.5.23]
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1878) [catalina.jar:8.5.23]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_191]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
Caused by: java.lang.IllegalStateException: ApplicationContext is not initialized
I Found This:
Hi,
You cannot access Configuration bean from another beans if your config has DATABASE source. You can do this only after context start:
https://doc.cuba-platform.com/manual-6.9/app_start_recipe.html?_ga=2.233700318.1178846495.1541433546-1676116619.1541177925
https://doc.cuba-platform.com/manual-6.9/app_lifecycle_events.html?_ga=2.233700318.1178846495.1541433546-1676116619.1541177925
So I added listener for Application LyfeCycle Events:
Second Try:
// The Elegant way.....
@Order(Events.LOWEST_PLATFORM_PRECEDENCE)
@EventListener
private void handleAppContextStartedEvent(AppContextStartedEvent event) {
GoogleAPIKey = configuration.getGoogleApiKey()
}
// ----- or in a Different way
class AddressGoogleCompleteServiceBean implements AddressGoogleCompleteService, ApplicationListener<AppContextStartedEvent> { <<<--- Implement one more interface
@Override <<<--- Implement one more interface
void onApplicationEvent(AppContextStartedEvent event) {
GoogleAPIKey = configuration.getGoogleApiKey()
}
I got this error:
-------APP LOG---------
2018-11-05 19:26:45.503 ERROR [localhost-startStop-1] com.haulmont.cuba.core.sys.AbstractWebAppContextLoader - Error initializing application
org.springframework.beans.factory.BeanInitializationException: Failed to process @EventListener annotation on bean with name 'contacts_AddressGoogleCompleteService'; nested exception is java.lang.IllegalStateException: Need to invoke method 'handleAppContextStartedEvent' declared on target class 'AddressGoogleCompleteServiceBean', but not found in any interface(s) of the exposed proxy type. Either pull the method up to an interface or switch to CGLIB proxies by enforcing proxy-target-class mode in your configuration.
at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:106) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781) ~[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.haulmont.cuba.core.sys.CubaClassPathXmlApplicationContext.(CubaClassPathXmlApplicationContext.java:27) ~[cuba-global-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.CubaCoreApplicationContext.(CubaCoreApplicationContext.java:26) ~[cuba-core-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AppContextLoader.createApplicationContext(AppContextLoader.java:89) ~[cuba-core-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AppContextLoader.createApplicationContext(AppContextLoader.java:35) ~[cuba-core-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AbstractAppContextLoader.initAppContext(AbstractAppContextLoader.java:62) ~[cuba-global-6.10.3.jar:6.10.3]
at com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.contextInitialized(AbstractWebAppContextLoader.java:77) ~[cuba-global-6.10.3.jar:6.10.3]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4745) [catalina.jar:8.5.23]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207) [catalina.jar:8.5.23]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.5.23]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) [catalina.jar:8.5.23]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) [catalina.jar:8.5.23]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) [catalina.jar:8.5.23]
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1144) [catalina.jar:8.5.23]
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1878) [catalina.jar:8.5.23]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_191]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
Caused by: java.lang.IllegalStateException: Need to invoke method 'handleAppContextStartedEvent' declared on target class 'AddressGoogleCompleteServiceBean', but not found in any interface(s) of the exposed proxy type. Either pull the method up to an interface or switch to CGLIB proxies by enforcing proxy-target-class mode in your configuration.
at org.springframework.core.MethodIntrospector.selectInvocableMethod(MethodIntrospector.java:135) ~[spring-core-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.support.AopUtils.selectInvocableMethod(AopUtils.java:130) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.event.EventListenerMethodProcessor.processBean(EventListenerMethodProcessor.java:155) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:103) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]
... 24 common frames omitted
-------APP LOG---------
Both are initialization/lifeCycle problems.
Whats wrong with Appdatacontext Listener?
Some more resources about Spring Events included in Cuba Platform:
https://doc.cuba-platform.com/manual-6.9/events.html
https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2
This is a sort of general Application BUS based on Observer Pattern.
Thank you in advance,
Fabrizio