Skip to content

Commit cbe556d

Browse files
1、spring-boot-with-druid 实现aspect切面拦截,
2、增加spring-boot 集成jpa
1 parent c4be878 commit cbe556d

File tree

17 files changed

+696
-8
lines changed

17 files changed

+696
-8
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<module>spring-boot-with-dynamic-datasource</module>
1212
<module>spring-boot-with-druid</module>
1313
<module>spring-boot-with-dubbo</module>
14+
<module>spring-boot-with-jpa</module>
1415
</modules>
1516
<packaging>pom</packaging>
1617

spring-boot-with-druid/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<artifactId>spring-boot-starter-log4j</artifactId>
5151
<version>1.3.7.RELEASE</version>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-aop</artifactId>
56+
</dependency>
5357
<dependency>
5458
<groupId>mysql</groupId>
5559
<artifactId>mysql-connector-java</artifactId>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package cn.kiiwii.framework.aop;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.*;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Component;
10+
import org.springframework.web.context.request.RequestContextHolder;
11+
import org.springframework.web.context.request.ServletRequestAttributes;
12+
import org.springframework.web.servlet.HandlerMapping;
13+
14+
import javax.servlet.http.HttpServletRequest;
15+
import java.util.Map;
16+
17+
/**
18+
* Created by zhong on 2016/11/24.
19+
*/
20+
@Aspect
21+
@Component
22+
public class ServiceAop {
23+
private static Logger logger = LoggerFactory.getLogger(ServiceAop.class);
24+
25+
/* private ThreadLocal<OperatorLog> tlocal = new ThreadLocal<OperatorLog>();
26+
27+
@Autowired
28+
private OptLogService optLogService;*/
29+
30+
@Pointcut("execution(public * cn.kiiwii.framework.service.impl.*.*(..))")
31+
public void webRequestLog() {}
32+
33+
@Before("webRequestLog()")
34+
public void doBefore(JoinPoint joinPoint) {
35+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
36+
HttpServletRequest request = attributes.getRequest();
37+
String beanName = joinPoint.getSignature().getDeclaringTypeName();
38+
String methodName = joinPoint.getSignature().getName();
39+
String uri = request.getRequestURI();
40+
String remoteAddr = getIpAddr(request);
41+
String sessionId = request.getSession().getId();
42+
String user = (String) request.getSession().getAttribute("user");
43+
String method = request.getMethod();
44+
String params = "";
45+
/*try {
46+
47+
long beginTime = System.currentTimeMillis();
48+
49+
// 接收到请求,记录请求内容
50+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
51+
HttpServletRequest request = attributes.getRequest();
52+
String beanName = joinPoint.getSignature().getDeclaringTypeName();
53+
String methodName = joinPoint.getSignature().getName();
54+
String uri = request.getRequestURI();
55+
String remoteAddr = getIpAddr(request);
56+
String sessionId = request.getSession().getId();
57+
String user = (String) request.getSession().getAttribute("user");
58+
String method = request.getMethod();
59+
String params = "";
60+
if ("POST".equals(method)) {
61+
Object[] paramsArray = joinPoint.getArgs();
62+
params = argsArrayToString(paramsArray);
63+
} else {
64+
Map<?, ?> paramsMap = (Map<?, ?>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
65+
params = paramsMap.toString();
66+
}
67+
68+
logger.debug("uri=" + uri + "; beanName=" + beanName + "; remoteAddr=" + remoteAddr + "; user=" + user
69+
+ "; methodName=" + methodName + "; params=" + params);
70+
71+
OperatorLog optLog = new OperatorLog();
72+
optLog.setBeanName(beanName);
73+
optLog.setCurUser(user);
74+
optLog.setMethodName(methodName);
75+
optLog.setParams(params != null ? params.toString() : "");
76+
optLog.setRemoteAddr(remoteAddr);
77+
optLog.setSessionId(sessionId);
78+
optLog.setUri(uri);
79+
optLog.setRequestTime(beginTime);
80+
tlocal.set(optLog);
81+
82+
} catch (Exception e) {
83+
logger.error("***操作请求日志记录失败doBefore()***", e);
84+
}*/
85+
}
86+
87+
@AfterReturning(returning = "result", pointcut = "webRequestLog()")
88+
public void doAfterReturning(Object result) {
89+
/*try {
90+
// 处理完请求,返回内容
91+
OperatorLog optLog = tlocal.get();
92+
optLog.setResult(result.toString());
93+
long beginTime = optLog.getRequestTime();
94+
long requestTime = (System.currentTimeMillis() - beginTime) / 1000;
95+
optLog.setRequestTime(requestTime);
96+
97+
System.out.println("请求耗时:" + requestTime + optLog.getUri() + " ** " + optLog.getParams() + " ** "
98+
+ optLog.getMethodName());
99+
System.out.println("RESPONSE : " + result);
100+
101+
optLogService.saveLog(optLog);
102+
} catch (Exception e) {
103+
logger.error("***操作请求日志记录失败doAfterReturning()***", e);
104+
}*/
105+
}
106+
107+
@Around("webRequestLog()")
108+
public Object around(ProceedingJoinPoint pjp){
109+
System.out.println("方法环绕start.....");
110+
Object o = null;
111+
try {
112+
System.out.println("before---------------------");
113+
o = pjp.proceed();
114+
System.out.println("after---------------------");
115+
} catch (Throwable e) {
116+
e.printStackTrace();
117+
}
118+
return o;
119+
}
120+
121+
/**
122+
* 获取登录用户远程主机ip地址
123+
*
124+
* @param request
125+
* @return
126+
*/
127+
private String getIpAddr(HttpServletRequest request) {
128+
String ip = request.getHeader("x-forwarded-for");
129+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
130+
ip = request.getHeader("Proxy-Client-IP");
131+
}
132+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
133+
ip = request.getHeader("WL-Proxy-Client-IP");
134+
}
135+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
136+
ip = request.getRemoteAddr();
137+
}
138+
return ip;
139+
}
140+
141+
/**
142+
* 请求参数拼装
143+
*
144+
* @param paramsArray
145+
* @return
146+
*/
147+
private String argsArrayToString(Object[] paramsArray) {
148+
String params = "";
149+
if (paramsArray != null && paramsArray.length > 0) {
150+
for (int i = 0; i < paramsArray.length; i++) {
151+
Object jsonObj = paramsArray[i];
152+
params += jsonObj.toString() + " ";
153+
}
154+
}
155+
return params.trim();
156+
}
157+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cn.kiiwii.framework.aop;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.*;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.web.context.request.RequestContextHolder;
10+
import org.springframework.web.context.request.ServletRequestAttributes;
11+
12+
import javax.servlet.http.HttpServletRequest;
13+
14+
/**
15+
* Created by zhong on 2016/11/24.
16+
*/
17+
@Aspect
18+
@Component
19+
public class WebAspect {
20+
private static Logger logger = LoggerFactory.getLogger(WebAspect.class);
21+
22+
@Pointcut("execution(public * cn.kiiwii.framework.controller.*.*(..))")
23+
public void webAspect() {}
24+
25+
@Before("webAspect()")
26+
public void doBefore(JoinPoint joinPoint) {
27+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
28+
HttpServletRequest request = attributes.getRequest();
29+
String beanName = joinPoint.getSignature().getDeclaringTypeName();
30+
String methodName = joinPoint.getSignature().getName();
31+
String uri = request.getRequestURI();
32+
String remoteAddr = getIpAddr(request);
33+
String sessionId = request.getSession().getId();
34+
String user = (String) request.getSession().getAttribute("user");
35+
String method = request.getMethod();
36+
String params = "";
37+
System.out.println(sessionId);
38+
}
39+
40+
@AfterReturning(returning = "result", pointcut = "webAspect()")
41+
public void doAfterReturning(Object result) {
42+
}
43+
44+
@Around("webAspect()")
45+
public Object around(ProceedingJoinPoint pjp){
46+
System.out.println("方法环绕start.....");
47+
Object o = null;
48+
try {
49+
System.out.println("before---------------------");
50+
o = pjp.proceed();
51+
System.out.println("after---------------------");
52+
} catch (Throwable e) {
53+
e.printStackTrace();
54+
}
55+
return o;
56+
}
57+
58+
/**
59+
* 获取登录用户远程主机ip地址
60+
*
61+
* @param request
62+
* @return
63+
*/
64+
private String getIpAddr(HttpServletRequest request) {
65+
String ip = request.getHeader("x-forwarded-for");
66+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
67+
ip = request.getHeader("Proxy-Client-IP");
68+
}
69+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
70+
ip = request.getHeader("WL-Proxy-Client-IP");
71+
}
72+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
73+
ip = request.getRemoteAddr();
74+
}
75+
return ip;
76+
}
77+
}

spring-boot-with-druid/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
server.port=8081
2+
spring.aop.auto=true
3+
spring.aop.proxy-target-class=false
24

35
# 数据库访问配置
46
# 主数据源,默认的

spring-boot-with-dynamic-datasource/src/main/java/cn/kiiwii/framework/dao/impl/TestDAOImpl.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package cn.kiiwii.framework.dao.impl;
22

3-
import cn.kiiwii.framework.dao.ITestDAO;
43
import cn.kiiwii.framework.druid.DynamicDataSource.TargetDataSource;
4+
import cn.kiiwii.framework.dao.ITestDAO;
55
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.beans.factory.annotation.Qualifier;
7-
import org.springframework.context.annotation.Bean;
86
import org.springframework.jdbc.core.JdbcTemplate;
97
import org.springframework.stereotype.Repository;
108

11-
import javax.sql.DataSource;
129
import java.util.List;
1310
import java.util.Map;
1411

spring-boot-with-jpa/pom.xml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-boot-sample</artifactId>
7+
<groupId>cn.kiiwii.framework</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-boot-with-jpa</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.12</version>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
<exclusions>
29+
<exclusion>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-logging</artifactId>
32+
</exclusion>
33+
</exclusions>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-configuration-processor</artifactId>
38+
<optional>true</optional>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-jdbc</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-jdbc</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-log4j</artifactId>
51+
<version>1.3.7.RELEASE</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>mysql</groupId>
55+
<artifactId>mysql-connector-java</artifactId>
56+
<version>5.1.38</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.alibaba</groupId>
60+
<artifactId>druid</artifactId>
61+
<version>1.0.25</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework</groupId>
65+
<artifactId>spring-context</artifactId>
66+
<version>4.3.2.RELEASE</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework</groupId>
70+
<artifactId>spring-orm</artifactId>
71+
<version>4.3.2.RELEASE</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-data-jpa</artifactId>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.hibernate</groupId>
79+
<artifactId>hibernate-core</artifactId>
80+
<version>4.3.7.Final</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.hibernate</groupId>
84+
<artifactId>hibernate-core</artifactId>
85+
<version>5.0.6.Final</version>
86+
</dependency>
87+
</dependencies>
88+
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-compiler-plugin</artifactId>
94+
<version>3.2</version>
95+
<configuration>
96+
<source>1.7</source>
97+
<target>1.7</target>
98+
</configuration>
99+
</plugin>
100+
<plugin>
101+
<groupId>org.springframework.boot</groupId>
102+
<artifactId>spring-boot-maven-plugin</artifactId>
103+
<executions>
104+
<execution>
105+
<goals>
106+
<goal>repackage</goal>
107+
</goals>
108+
</execution>
109+
</executions>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
</project>

0 commit comments

Comments
 (0)