Implementation of spring _ aop_proxy AOP static proxy and Spring AOP dynamic proxy

Source: Internet
Author: User

Implementation of spring _ aop_proxy AOP static proxy and Spring AOP dynamic proxy

Static proxy Implementation of AOP in Spring:

In our project, there will be log records and permission management and other content unrelated to the business logic. We need to extract these content to a separate module to process them!

The static proxy Implementation of AOP is shown below;


The log example is used to describe how to create a log logging class:

Package com. spring. log; import java. util. date;/*** log * @ author sunlight **/public class Logger {public static void info (String info) {// edit the log output content System here. out. println (new Date () + "============>>" + info );}}

We add a log record when performing database operations, write a User's static proxy Dao class and implement the IUserDao interface. The Code is as follows:
Package com. spring. dao; import javax. annotation. resource; import org. springframework. stereotype. component; import com. spring. log. logger; import com. spring. model. user;/*** in the absence of source code or permission control and some open proxy classes ** @ author sunlight **/@ Component ("userProxyDao ") public class UserProxyDao implements IUserDao {private IUserDao userDao; public IUserDao getUserDao () {return userDao;} @ Resourcepublic void setUserDao (IUserDao userDao) {this. userDao = userDao;} @ Overridepublic void add (User user) {// do not destroy the original code Logger.info ("added User"); userDao. add (user) ;}@ Overridepublic void delete (int id) {Logger.info ("deleted user"); userDao. delete (id) ;}@ Overridepublic User load (int id) {return null ;}}

During Service injection, we use the static proxy's UserProxyDao injection. The Code is as follows:
Package com. spring. service; import javax. annotation. resource; import org. springframework. stereotype. component; import org. springframework. stereotype. service; import com. spring. dao. IUserDao; import com. spring. model. user; // @ Component (value = "userService") @ Service ("userService") // The business layer uses @ Service to inject public class UserService implements IUserService {private IUserDao userDao; public IUserDao getUserDao () {return userDao;} // by default, @ Inject is provided in JSR330 to Inject @ Resource (name = "userProxyDao ") // inject public void setUserDao (IUserDao userDao) {this. userDao = userDao;} @ Overridepublic void add (User user) {userDao. add (user) ;}@ Overridepublic void delete (int id) {userDao. delete (id) ;}@ Overridepublic User load (int id) {return userDao. load (id );}}

For other code, see Annotation Implementation of IOC in Spring.

Execution result:





Principles of declarative transactions using Spring AOP proxy

First, inject sessionFactory into HibernateTransactionManager, and then inject the HibernateTransactionManager object into the TransactionProxyFactoryBean object ~ There should be another definition of sessionFactory.

AOP is a proxy-oriented model. Originally, an application must operate on an object, but this object contains a lot of business logic unrelated to itself, not as good as logs and locks. So we need to find a way to extract all these irrelevant things out. The extracted object is the proxy object.

When the application calls the proxy object again, it will call the proxy object, which contains the call to the proxy object and the unrelated business logic method that is extracted. Proxy objects are purely business logic.

As for the trigger, when the container is up, for example, tomcat or jboss, the specified xml file will be defined and the injected content will be automatically identified.

Which of the following methods can be used to implement AOP?

There are three common implementation methods:
1. Use the Agent Mode to dynamically implement AOP. The specific technical details can be divided into static proxy, dynamic proxy, and CGLIB to generate subclass proxy.
2. Use the pre-compiled Method for static proxy.
3. Use the custom loader Method for Dynamic proxy.
The latter two proxies can proxy more content (such as constructors, static methods, static blocks, final methods, and private methods ).
 

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.