Copyright: Reprinted please note the name of the source https://blog.csdn.net/meism5/article/details/91043752
As we all know, the spring aop module implementation, with a dynamic proxy. Then we look at how to use spring aop module, complete
- Print each request takes from start to finish
- Check whether the current user to log certain requests
1, increasing the reliance on spring aop pom module
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
2, to achieve service class remain unchanged
package constxiong.cxproxy.chapter6.service;
import java.util.HashMap;
import java.util.Map;
/**
* 服务接口实现
* @author ConstXiong
* @date 2019-05-29 11:02:15
*/
public class ServiceImpl implements Service {
/**
* 登录
*/
@Override
public boolean login(String username, String password) {
simulateDaOperation(100);
System.out.println("用户名:" + username + ", 密码:" + password + " 登录成功");
return true;
}
/**
* 根据用户名获取用户信息
*/
@Override
public Map<String, Object> getUserInfo(String username) {
Map<String, Object> userInfo = new HashMap<String, Object>();
simulateDaOperation(150);
userInfo.put("username", username);
userInfo.put("sex", "男");
userInfo.put("age", 18);
System.out.println("用户名:" + username + ", 获取用户信息:" + userInfo);
return userInfo;
}
/**
* 模拟数据库操作,休眠
* @param millis 毫秒数
*/
private void simulateDaOperation(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3, with spring aop aop Union and its integrated aopalliance, complete surround proxy service method.
- MethodInterceptor implement interfaces override the invoke method
package constxiong.cxproxy.chapter6.proxy;
import java.util.Random;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 使用spring aop 完成环绕代理
* @author ConstXiong
* @date 2019-06-06 14:49:36
*/
public class ServiceAroundAdvice implements MethodInterceptor {
//获取用户信息的方法名
private static final String METHOD_GET_USERINFO = "getUserInfo";
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = null;
long start = System.currentTimeMillis();//计时开始
if (METHOD_GET_USERINFO.equals(invocation.getMethod().getName())) {//获取用户信息方法
if (checkIsLogined()) {//校验是否登录
result = invocation.proceed();//目标类的方法调用,与结果获取
}
} else {//非获取用户信息方法,不校验是否登录
result = invocation.proceed();//目标类的方法调用,与结果获取
}
long end = System.currentTimeMillis();//计时结束
System.out.println("耗时:" + (end - start) + "毫秒");//打印耗时
return result;
}
/**
* 模拟 当前用户是否登录
*/
private boolean checkIsLogined() {
Random r = new Random();
int i = r.nextInt(10);
if (i % 2 == 0) {
System.out.println("已登录");
return true;
}
System.out.println("未登录");
return false;
}
}
4, the test class
Create a proxy class factory new ProxyFactory ()
Set the target class proxyFactory.setTarget ()
Add booster proxyFactory.addAdvice ()
Gets the proxy class proxyFactory.getProxy ()
package constxiong.cxproxy.chapter6;
import org.springframework.aop.framework.ProxyFactory;
import constxiong.cxproxy.chapter6.proxy.ServiceAroundAdvice;
import constxiong.cxproxy.chapter6.service.Service;
import constxiong.cxproxy.chapter6.service.ServiceImpl;
/**
* 测试类
* @author ConstXiong
* @date 2019-06-06 14:08:01
*/
public class Test {
public static void main(String[] args) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(new ServiceImpl());
proxyFactory.addAdvice(new ServiceAroundAdvice());
Service service = (Service) proxyFactory.getProxy();
service.login("ConstXiong", "123456");
service.getUserInfo("ConstXiong");
}
}
The results of printing with in Chapter 2, do not use a proxy consistent results.
to sum up:
- spring aop to complete the proxy more intuitive to set target class and proxy classes, access to the proxy, call the service
- May be integrated into the frame of the spring, and by configuring the proxy to complete and annotated functional section