Spring (four) example analysis of Spring

Source: Internet
Author: User

The last blog post in the Spring (ii) IOC details and the spring (c) AOP in detail, we introduced the spring framework of the two core one is the IOC, one is AOP. Now let's do a spring example.

In order to better explain the relevant content of spring, this blog post will be for a "add user" instance, for the gradual dissection and optimization, in this process, the details of the people do not need to consider, only need to deepen the understanding of spring.

1. Example One

First, let's look at an instance of adding a user without using any spring frame content. First look at the relevant class diagram and implementation code, as follows:

public class User {private String username;                private String password;        Public String GetUserName () {return username;        } public void Setusername (String username) {this.username = username;        } public String GetPassword () {return password;        } public void SetPassword (String password) {this.password = password;    }} public interface Userdao {public void AddUser (user user);                    } public class Userdao4oracleimpl implements Userdao {@Override public void AddUser (user user) {        System.out.println ("Userdao4oracleimpl.adduser (), username=" +user.getusername ());                     }} public class Userdao4mysqlimpl implements Userdao {@Override public void AddUser (user user) {        System.out.println ("Userdao4mysqlimpl.adduser (), username=" +user.getusername ()); }} public interfaceUsermanager {public void AddUser (user user);                    } public class Usermanagerimpl implements Usermanager {@Override public void AddUser (user user) {                    Userdao Userdao = new Userdao4mysqlimpl ();                Userdao.adduser (user); }} public class Client {public static void main (string[] args) {User user = new use                    R ();            User.setusername ("Zhang San");                            User.setpassword ("123456");                        Usermanager Usermanager = new Usermanagerimpl ();                    Usermanager.adduser (user); }    }

Analysis of the above class diagram and code, we can clearly see: In the Usermanagerimpl class method AddUser, Usermanagerimpl called Userdao and its specific implementation class Userdao4mysqlimpl, This does not conform to the idea of separating the control of the specific implementation class of an interface from the calling class to the decision of the third party, so we need to modify it here. (Refer to our previous IOC article).

2. Example Two

For example one of the parts that do not conform to the IOC thought, we make relevant changes, the relevant class diagram and code after the specific changes are as follows:

    public class Usermanagerimpl implements Usermanager {        private Userdao Userdao;         Public Usermanagerimpl (Userdao Userdao) {this            . Userdao = Userdao;        }                @Override public        void AddUser (user user) {                            this.userDao.addUser (user);                }    }        public class Client {public        static void Main (string[] args) {                        user user = new User ();                    User.setusername ("Zhang San");            User.setpassword ("123456");                            Userdao Userdao =  new Userdao4mysqlimpl ();                            Usermanager Usermanager = new Usermanagerimpl (Userdao);                        Usermanager.adduser (user);                    }        }

After analyzing the class diagram and code modified above, we found that although we gave the Userdao control to the client, there was a dependency on the Userdao and its related implementation classes in the client, so for the client, The coupling between the client class and the Userdao class is less than the code we wrote earlier. How to optimize it? We don't think much about it here, just use the relevant content of spring.

3. Example Three

For example two, let's take a look at the related class diagram that we used after the spring framework was modified, as follows:

After reading the class diagram above, we can see from the first class diagram that there is less interdependence between classes and classes, which can make our code less coupled. But how is the code of this class diagram implemented? Next, focus on the relevant code for the target client class that we need to modify, as follows:

    public class Client {public            static void Main (string[] args) {                        user user = new User ();                    User.setusername ("Zhang San");            User.setpassword ("123456");                        Beanfactory factory = new Classpathxmlapplicationcontext ("Applicationcontext.xml");            Usermanager Usermanager = (usermanager) factory.getbean ("Usermanager");                            Usermanager.adduser (user);                }    }

By comparing the code above with the code for the client class in our second instance, the code above uses a Beanfactory class and XML file, which is the core class and implementation of the spring framework. Knowing the difference between the two, let's look at what is written in the XML file, as follows:

 <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schem A/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "xsi:schemalocation=" HTTP://WWW.SPRINGFR Amework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGF RAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframew Ork.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <bean id=" Userdao 4MySqlImpl "class=" Com.zs.spring.dao.UserDao4MySqlImpl "/> <bean id=" Usermanager "class=" com.zs.spring.m Anager. Usermanagerimpl "> <property name=" Userdao "ref=" Userdao4mysqlimpl "/> </bean> < ;/beans> 

I'll focus on this XML file in the next article, just to let you know that its role is to take the dependencies between objects in the spring container and avoid excessive program coupling caused by hard coding. This is equivalent to the following two lines of code:

Usermanager Usermanager = new Usermanagerimpl (Userdao); Userdao Userdao =  new Userdao4mysqlimpl ();

Spring (four) example analysis of Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.