@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 **

1

There are 1 answers

0
J Asgarov On

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 case Mydemo) 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, the Mydemo 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.