@Component
public class Mydemo {
@Value("${key.something}")
public String injectedProperty;
public class innerDemo implements Condition{
public boolean matches(ConditionContext context, AnnotedTypeMetaData metaData){
System.out.println(injectedProperty) // null
return true
}
}
}
application.yml
key:
something: Hello
** Tried without inner class but still getting null. The query is couldn't able to get application.yml properties inside the inner class **
I think you are incorrectly describing the problem. It has no relevance that you are trying to access it from inner class - what matters is that for Spring to autowire any property (in this case
key.something
) the class in which the value should be autowired (in this caseMydemo
) HAS TO BE CREATED BY SPRING.That means if you anywhere in your appication call
new Mydemo()
then for that instance that you created yourself nothing will get autowired. For Spring to autowire the property, theMydemo
class has to also be autowired in the place you are getting null.In other words Spring 101 and you should read the documentation to understand how dependency injection works on basic level.