Spring Learning (iv) Spring example analysis

Source: Internet
Author: User
Tags aop

Last posting in simple and simple. Spring (ii) IOC detailed understanding and simple Spring (iii) AOP detailed, we introduced the spring framework of the two core one is the IOC, one is AOP. Next we'll do a spring example.

In order to better explain the content of spring, this blog post will be for a "add user" example, the step-by-Step anatomy and optimization, and in this process, the details are not to be considered, only need to deepen the understanding of spring.

1. Example One

First, let's look at a simple instance of adding a user without using any of the spring framework 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 us
        ER) {System.out.println ("Userdao4oracleimpl.adduser (), username=" +user.getusername ()); The public class Userdao4mysqlimpl implements Userdao {@Override public void Adduse R (user user) {System.out.println ("Userdao4mysqlimpl.adduser (), UsernaMe= "+user.getusername ());
    } public interface Usermanager {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 User ();
            User.setusername ("John");    
                
            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 is inconsistent with IOC mode, where the control of the concrete implementation class of the interface is separated from the calling class and transferred to a third party decision, so we need to modify it here. (Refer to our previous IOC article).

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.