60 s allows you to learn the dynamic proxy principle and 60 s the dynamic proxy Principle
I. Why dynamic proxy?
When an object or multiple objects implement the N method, the business needs to add the object and N methods of multiple objects to a common method, for example, there are three methods to add all methods of all objects to the transaction:
Method 1: objects are added by method. Obviously, this method is stupid.
Method 2: Add a static proxy object to implement the interface for adding the transaction object. Then add transactions to each method in the static proxy object.
Method 3: Use a dynamic proxy object to dynamically load transactions.
Dynamic proxy is used to enable the object to be opened and closed, open to expansion, and disable modification. Spring AOP is actually implemented using dynamic proxy + IoC containers.
Ii. Code implementation.
UserManger. java Interface
package com.tgb.spring;public interface UserManager {public void addUser(String username,String password);public void delUser(int userId);public String findUserById(int userId);public void modifyUser(int userId,String username,String password);}
Method 1:
UserMangerImpl. Java
Package com. tgb. spring; public class UserManagerImpl implements UserManager {public void addUser (String username, String password) {checkSecurity (); System. out. println ("UserManager. addUser ");} public void delUser (int userId) {checkSecurity (); System. out. println ("UserManager. delUser ");} public String findUserById (int userId) {checkSecurity (); System. out. println ("UserManager. findUserById "); return" Zhang San ";} public void modifyUser (int userId, String username, String password) {checkSecurity (); System. out. println ("UserManager. modifyUser ");} private void checkSecurity () {System. out. println ("checkSecurity ");}}
Client. java
package com.tgb.spring;public class Client {/** * @param args */public static void main(String[] args) {UserManager userManager=new UserManagerImpl();userManager.addUser("11", "1231");}}
Method 2 static Proxy:
UserManagerImpl. java
Package com. tgb. spring; public class UserManagerImpl implements UserManager {public void addUser (String username, String password) {// checkSecurity (); System. out. println ("UserManager. addUser ");} public void delUser (int userId) {// checkSecurity (); System. out. println ("UserManager. delUser ");} public String findUserById (int userId) {// checkSecurity (); System. out. println ("UserManager. findUserById "); return" Zhang San ";} public void modifyUser (int userId, String username, String password) {// checkSecurity (); System. out. println ("UserManager. modifyUser ");} // private void checkSecurity () {// System. out. println ("checkSecurity ");////}}
UserManagerImplProxy. java
package com.tgb.spring;public class UserManagerImplProxy implements UserManager {private UserManager userManager;public UserManagerImplProxy(UserManager userManager){this.userManager=userManager;}public void addUser(String username, String password) {// TODO Auto-generated method stubcheckSecurity();userManager.addUser(username, password);}public void delUser(int userId) {// TODO Auto-generated method stubcheckSecurity();userManager.delUser(userId);}public String findUserById(int userId) {// TODO Auto-generated method stubcheckSecurity();return userManager.findUserById(userId);}public void modifyUser(int userId, String username, String password) {// TODO Auto-generated method stubcheckSecurity();userManager.modifyUser(userId, username, password);}private void checkSecurity(){System.out.println("checkSecurity");}}
Client. java
package com.tgb.spring;public class Client {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubUserManagerImpl uesrMangerImpl=new UserManagerImpl();UserManager userManager=new UserManagerImplProxy(uesrMangerImpl);userManager.addUser("11", "1231");}}
Method 3: Dynamic proxy
UserManagerImpl. java and UserManagerImpl. java in method 2 delete UserManagerImplProxy. java
Add a new category:
SecurityHandler. java
Package com. tgb. spring; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; public class SecurityHandler implements InvocationHandler {private Object targetObject; public Object createProxyInstance (Object targetobject={this.tar getObject = targetObject; return Proxy. newProxyInstance (targetObject. getClass (). getClassLoader (), targetObject. getClass (). getInterfaces (), this);} public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {// TODO Auto-generated method stubcheckSecurity (); // call the target method Object ret = method. invoke (targetObject, args); return ret;} private void checkSecurity () {System. out. println ("checkSecurity ");}}
Client. java
package com.tgb.spring;public class Client {public static void main(String[] args) {SecurityHandler handler=new SecurityHandler();UserManager userManager=(UserManager)handler.createProxyInstance(new UserManagerImpl());userManager.addUser("zha", "123");}}
The effects of the three methods are the same.
Iii. Summary.
The use of dynamic proxy brings great convenience to our code and solves the dynamic solution for providing services for objects. The combination of dynamic proxy and IoC container makes it easier for us to provide services to the system, so we can implement the principle of opening and closing. In this way, you can withdraw from serving developers wholeheartedly when there is no need for the object.
How can I let a Tom learn how to use bilibili space CSS to teach me how to work?
I don't know much about CSS. However, getting started with div + css is the simplest on the Internet.
Div is a meaningless tag in the HTML language. It is meaningless, so you can define CSS for it at will,
CSS stands for Cascading Style Sheets. That is to say, the div label is defined in CSS: length, width, color, Font, position, and other styles.
That is to say, the page you see is actually composed of N multiple div labels, but CSS is given to him, so there are big and small ones.
If you do not understand it at all, we recommend that you watch the basic video of the LAMP brothers and talk about CSS.
Portal: www.lampbrother.net/phptrain/video.php
If you want to study CSS, you still need to practice various projects.
What is the magic that makes the water in the paper cup disappear instantly? (I promise not to reveal the secret after learning it)
The main principle of this magic-making water disappear
The key is how to disappear. The method is as follows:
1. put a good quality sponge in the quilt (you don't have to use a sponge to absorb water). After pouring the water into it, wait for a while, in this time, magicians generally read some spells or tricks, which are secondary. The key is to let the sponge absorb water.
2. This cup is a prop and has a interlayer (but it is unlikely)
Magic is not only about cracking its secrets, but also about getting happiness from it.