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).