spring design ideas

When learning the Spring framework, Spring first thing is to analyze design ideas

Spring is the time to learn, we need to understand the concept of coupling and decoupling

Coupling: In simple terms, in which software engineering, coupling means interdependence between objects

Coupled disadvantages: coupling enhance the complexity of the code, is not conducive to the development and maintenance, low coupling system is one of the principles of software architecture design

Why Spring?

Spring can be unified management bean object, what objects when you need to, we'll go get the corresponding object from Spring, eliminating the need to create a new out new, greatly simplifies the management of objects (create, load and configuration files to read ) and so on! 

Spring for resolving the problem, reduce the interdependencies between objects

Spring solve the problem?

Simply put, the process of creating an object, and the object dependencies!

Spring's basic idea is to achieve what? 

1. The first step, the most primitive, using the new () method to create an object

   Inadequate: This leads to high coupling procedures, if required the new object has not been achieved, then the error will not follow-up development, and the program at compile time error is contrary to the principle of low coupling software architecture design. , and single responsibility principle. 

  // 1. Procedure strong coupling relationship: use mysql driven when the object using the Driver class 
    @Test
     public  void test1 () {
         the try {
             // create a Driver objects
             // used to add registerDriver driven to drive manager mysql in 
            Driver = Driver new new com.mysql.jdbc.Driver (); 
            DriverManager.registerDriver (Driver); 
        } the catch (SQLException E) { 
            e.printStackTrace (); 
        } 
    }

 

2. Second, for example reflection:. Class.ForName ( "com.mysql.jdbc.Driver") newInstance ()

  Objects through reflection, when the program is running, you will need to load the class and create class

   // 2. The strong relationship code, the conversion relation for weak 
        String = Driver "com.mysql.jdbc.Driver" ;
         the try { 
            the Class.forName (Driver); 
        } the catch (a ClassNotFoundException E) { 
            e.printStackTrace (); 
        }

 

3. The third step, the configuration example: db.properties and web.xml

 By the configuration file, when the program is running, read the contents of the configuration file to load the class through the configuration file, and create an instance of the class

 / ** 
     * decoupling is achieved through the configuration file 
     * / 
    @Test 
    public  void Test3 () {
         // the db.properties file location in the src
         // After compilation, the configuration file src directory will copy the classes directory 
        classLoader = testmysql ClassLoader. class .getClassLoader (); 
        the InputStream resourceAsStream = ClassLoader.getResourceAsStream ( "the db.properties" ); 
        the Properties Properties = new new the Properties ();
         the try { 
            Properties.load (resourceAsStream); 
            System.out.println (Properties. GET ( "Driver" )); 
            String Driver= (String)properties.get("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

4. The fourth step is to load the configuration file by Bundle, simplifying the process of loading configuration file, and then create an object of

  // use bundle 
    @Test
     public  void Test4 () {
         // ResourceBundle is designed to read the configuration file of utility classes
         // bundle can only read properties file types, need time to read only the file name, no suffix
         // the bundle is also provided a method of iterative read all configuration 
        ResourceBundle the ResourceBundle.getBundle DB = ( "DB" ); 
        db.getString ( "Driver" ); 
        the Enumeration <String> Keys = db.getKeys ();
         the while (Keys. the hasMoreElements ()) { 
            String Key = keys.nextElement (); 
            System.out.println (Key + ":" + db.getString (Key)); 
        } 
    }

5. The fifth step, the object to manage all of the classes managed by Bean plants, get the object from Bean plants

public  class the BeanFactory { 

    // prepare bean plants in a container, add the bean are reflected back in, and taken as 
    Private  static the Map <String, Object> beanMap = null ;
     static { 
        beanMap = new new the HashMap <> (); 
        the bean ResourceBundle = the ResourceBundle.getBundle ( "the bean" ); 
        the Enumeration <String> Keys = bean.getKeys ();
         the while (keys.hasMoreElements ()) { 
            String Key = keys.nextElement (); 
            String the classPath = bean.getString (Key );
             the try {
                Object Object = the Class.forName (the classPath) .newInstance (); 
                beanMap.put (Key, Object); 
            } the catch (an InstantiationException is | IllegalAccessException | a ClassNotFoundException E) { 
                e.printStackTrace (); 
            } 
        } 
    } 
    / ** 
     * Object of use acceptable base class to obtain the object 
     * @param Key 
     * @return 
     * / 
    public  static Object the getBean (String Key) { 
        Object O = null ; 
        ResourceBundle the bean = the ResourceBundle.getBundle ( "the bean"); 
        String the classPath = bean.getString (Key);
         the try { 
            O = the Class.forName (the classPath) .newInstance (); 
        } the catch (an InstantiationException is | IllegalAccessException | a ClassNotFoundException E) { 
            e.printStackTrace (); 
        } 
        return O; 
    } 

 / ** 
     * The method used to obtain generic objects (recommended) 
     * @param Key 
     * @param clazz 
     * @param <T> 
     * @return 
     * / 
    public  static<T> T getBean(String key, Class<T> clazz) {
      return (T) beanMap.get(key);
    }

So then the program calls for the new object becomes the following code

        UserService userService = BeanFactory.getBean("userService", UserService.class);
        userService.register(user);

Spring's core idea:

  It is through a BeanFactory factory to unify management of all objects

Preparing bean plants in a container, the bean are added back reflector in, and taken as

Reference article:  https://www.cnblogs.com/hello-liyb/p/8252029.html

https://blog.csdn.net/weixin_37243717/article/details/79673502

https://www.cnblogs.com/poilk/p/7015929.html

https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/

https://www.cnblogs.com/wade-luffy/p/6000277.html

Guess you like

Origin www.cnblogs.com/zjulanjian/p/10929976.html