0

I'm working on Springboot application deploying on GCP environment,

A singleton object has created in spring, and am updating that object values in other component and that update is not reflecting in any other places where ever I autowired that component.

The same logic works in Java environment but not Appengine environment.

Sample:

Updating the spring initialized bean.

@autowired Test test;

test.setNewData("temp");

Getting the same bean in another component:

@autowired Test test;

test.getNewData(); // which is updated by above component and here I have to receive the updated value. But getting the old value.

Please help me out. thanks in advance.

Sankar C
  • 41
  • 1
  • 3

1 Answers1

0

As you said you are trying to use Autowiring on Singleton object, which is static. This should not work anyway. You cannot autowire static fields in Spring and you will need to write your own logic for that. This was discussed in some other posts, too: post 1, post 2.

Anyhow, for an optimal way to develop your app in App Engine, using Autowiring is not recommended at all as you can see in the Google App Engine Documentation

Andrei Tigau
  • 2,010
  • 1
  • 6
  • 17
  • public void loadPropertiesFromDB() { MutablePropertySources existingPropertySources = ((ConfigurableEnvironment) environment).getPropertySources(); PropertiesPropertySource dbPropertySource = new PropertiesPropertySource("dbPropertySource", getPropertiesFromDB()); existingPropertySources.addFirst(dbPropertySource); } Here, I'm trying to reload the database properties at runtime using an endpoint (localhost:8080/reloadProp). – Sankar C Sep 11 '19 at 08:49
  • update: public void loadFromDB() { MutablePropertySources existingProps = ((ConfigurableEnvironment) environment).getPropertySources(); PropertiesPropertySource dbProp= new PropertiesPropertySource("dbProp", getPropertiesFromDB()); existingProps.addFirst(dbProp); } calling this method using an endpoint localhost:8080/reloadProp, to access the latest db prop at runtime. This will fetch the latest db prop and update in the existing prop sources. Then I will autowire Environment and get use environment.getproperty(''key") working in JVM but not in Appengine (GCP) – Sankar C Sep 11 '19 at 09:01
  • I see that you want to Autowire an Environment object. There may be plenty reasons for that not to work. Maybe you can try to be more specific while trying to Autowirie and use Qualifiers, too. Maybe there are some more Environment objects in your Spring Context. If you could Autowire it on local it probably means that the object it is not from a Singleton class. Here is an example on how to use qualifiers: https://www.mkyong.com/spring/spring-autowiring-qualifier-example/ . Here you can find another post with an issue similar ( not the same ) with yours. – Andrei Tigau Sep 11 '19 at 09:53
  • Keep in mind that Autowired IS NOT optimal and it is not recommended on AppEngine as I said previously. – Andrei Tigau Sep 11 '19 at 09:53
  • Could you please tell me, how beans are handled in GCP env ? For eg in Spring terms : it will be created at onload and maintained in container and if anyone updates that bean, It will reflect wherever that bean used. This behavior is not working in GCP env. – Sankar C Sep 11 '19 at 11:25
  • There is no difference regarding the way beans are handled. It should work the same. I find your situation a bit strange. Maybe if you could provide me a github repository with you whole application, in order to be able to reproduce it and see the whole image, I could help you more. – Andrei Tigau Sep 11 '19 at 12:01