0

I have a property set to "true", that I want to bind to my checkStatus field.

@Value("${prop.checkstatus}")
private boolean checkStatus;

I am able to access the value from inside an @Service class. However, if I use @Value inside a class that I instantiate or retrieve using AppliationContext.getBean("classA"), the value is always "false" since it cannot get the correct value from the property file. Please advise as I need to access this value from an instance and from a bean.

In some parts of the code, I create the object using new and try to access @Value. I understand that this is not possible since the new object is not managed by spring. But there are parts of the code where the object is retrieved using applicationcontext. Still I cannot access the @Value. Is there a better approach in retrieving @Value both for objects created using new and applicationcontext.getbean?

iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78
  • Do you use Spring-Boot? How is the class using this property configured? – Nikolas Charalambidis Aug 21 '18 at 15:40
  • use `application.properties` – John Joe Aug 21 '18 at 15:40
  • I am using properties file. Otherwise, I won't be getting the value from within my service class. I don't use spring-boot. – iPhoneJavaDev Aug 21 '18 at 15:50
  • It will not be in the class you instantiate because that instance will not be managed by spring. For `AppliationContext.getBean("classA")` we need to see your spring config and how you get the `AppliationContext`. – Rohit Aug 21 '18 at 16:00
  • *if I use @Value inside a class that I instantiate or retrieve using AppliationContext.getBean("classA")* - you always have to use application context. Creating with `new` wont work. – Antoniossss Aug 21 '18 at 16:25
  • I just inject the ApplicationContext @Autowired private ApplicationContext appContext; – iPhoneJavaDev Aug 22 '18 at 02:06
  • I edited the question with the last paragraph – iPhoneJavaDev Aug 22 '18 at 02:11
  • For completeness - Spring has supported for a little while the notion of @Configurable classes, for which it will do dependency injection when instances are allocated through plain new expressions. But this is recommended only when one cannot control instantiation (like Servlets) - otherwise, getting beans from appContext would be best/easiest... – moilejter Aug 22 '18 at 03:04
  • Possible duplicate of [How to inject dependencies into a self-instantiated object in Spring?](https://stackoverflow.com/questions/3813588/how-to-inject-dependencies-into-a-self-instantiated-object-in-spring) – Andy Brown Aug 22 '18 at 10:31
  • if I go with the Configurable, will Value also works? I see in the answer that it is so that Autowired will work inside the new instance. I'm not sure if I use the other approach of AutowireCapableBean that i will still have a different instance everytime I do new(). Also how to solve the ApplicationContext.getBean("classA"), is it the same that I annotate classA with Configurable? – iPhoneJavaDev Aug 22 '18 at 11:04

0 Answers0