Spring drive Eleventh Lecture notes - reference Spring underlying components

In the daily development, custom components you want to use some of the components of the underlying Spring container then the custom components to achieve *** Aware; method when creating the object, it calls the interface specified injection-related components; for example:

ApplicationContextAware implement interface can be custom components obtained in the Spring ioc container, as follows:

public  class Color the implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware { 
    
    Private the ApplicationContext applicationContext; 

    public  void setApplicationContext (the ApplicationContext applicationContext) throws BeansException {
         // then implement the ApplicationContextAware interfaces, the method may be used to add and then come Spring container, using saved. 
        the this .applicationContext = applicationContext; 
    } 

    public  void setBeanName (String name) {
         // after BeanNameAware implemented interfaces, which method can give the name of the current bean ioc container; 
        System.out.println ( "current bean container name is:" + name); 
    }
    
    public  void setEmbeddedValueResolver (StringValueResolver the Resolver) {
         // After the realization EmbeddedValueResolverAware interface, you can get to a String value resolver 
        System.out.println ( "Hello $ {os.name}, I was # 90 * {20}" ) ; 
        
    } 
    
}

Run the test class, the results are as follows:

The current bean name in the container as: color 
hello Mac OS X, I'm 1800

Expansion: If you want to inject other underlying components, to achieve *** Aware Interface.

Guess you like

Origin www.cnblogs.com/xingjia/p/11264214.html