springboot get the actual bean Tools

  The cause of that experience in the development, I want to use the configuration file variable values ​​in the tool class. Usually @value notes, this can only be managed in the spring bean total acquisition. Before I am wondering why the previous developer will add @Component comment on SpringUtil class, and today this is the case, the reasons fully understood.

@Component
 public  class SpringUtil the implements EnvironmentAware {
     Private  static Environment the env;
     public  static String getProperty (String Key) {
         return env.getProperty (Key); 
    } 
    @Override 
    public  void setEnvironment (Environment Environment) { 
        injectEnvironment (the env); // because spring this will create a class of objects that implement the interface, so the instance method call static methods, but at present we can not see this kind of 
    } 
    public   static  void injectEnvironment (Environment env) { 
        SpringUtil.env = env; // this is actually an example method calls the static method 
    } 
}

  The principle is very simple, it is an instance method calls the static method, they still generated objects, produced only a utility class object, not a lot, it's java, it is spring. Appropriate, @ Service annotation is a single case (which should be based on natural methods are parallel, each method call will generate its own data stack), you can also see that this spring is very suitable for use objects, you can create ignored overhead objects.

  Learning spring, we all know that spring is an important function of the object manager. So an important question is: How do we get what we want Bean? spirng Is there a static class, get the object directly by name. Sorry, this is not the right way to spring open, how to use a static class is straightforward though, but spring did not realize this. The usual practice is as above, is injected through spirng ApplicationContext object, which is a bean container, then you can get the class we want to get. It reported Qian, you can not own a new ApplicationContext object, which is perhaps the only operating environment to keep the ApplicationContext it.

  Singleton is a kind of simple beauty. As for why the use of a single case without the use of a static class, I think there are two reasons: 1. The creation and destruction all over the side of a single case, can save resources when not in use 2. We inherited class, most examples of methods and instance variables (As is staggering instance methods and instance variables, should be very vast majority of the class to be instantiated, and there may be multiple objects), then it is the inherited instance methods and instance variables, we can say this is the object-oriented programming style Effect in the single example of a case. Singleton drawback is probably generated objects overhead, and now this has not what it is.

  Object class generally higher than plump, ah, this is the experience.

Guess you like

Origin www.cnblogs.com/Robin008/p/11595212.html
Recommended