Skip to content

Commit 6497224

Browse files
committed
logging aspect
1 parent 2092dfa commit 6497224

File tree

7 files changed

+59
-45
lines changed

7 files changed

+59
-45
lines changed

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/autoconfigure/BeanAggregateAutoConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.github.lvyahui8.spring.aggregate.model.*;
77
import io.github.lvyahui8.spring.aggregate.repository.DataProviderRepository;
88
import io.github.lvyahui8.spring.aggregate.repository.impl.DataProviderRepositoryImpl;
9+
import io.github.lvyahui8.spring.aggregate.service.DataBeanAggregateQueryService;
910
import io.github.lvyahui8.spring.aggregate.service.impl.DataBeanAggregateQueryServiceImpl;
1011
import io.github.lvyahui8.spring.annotation.DataConsumer;
1112
import io.github.lvyahui8.spring.annotation.DataProvider;
@@ -59,13 +60,19 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
5960
@Bean
6061
@ConditionalOnMissingBean
6162
public DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade() {
63+
return new DataBeanAggregateQueryFacadeImpl(dataBeanAggregateQueryService());
64+
}
65+
66+
@Bean
67+
@ConditionalOnMissingBean
68+
public DataBeanAggregateQueryService dataBeanAggregateQueryService () {
6269
DataProviderRepository repository = new DataProviderRepositoryImpl();
6370
scanProviders(repository);
6471
DataBeanAggregateQueryServiceImpl service = new DataBeanAggregateQueryServiceImpl(repository);
6572
service.setRuntimeSettings(createRuntimeSettings());
6673
service.setExecutorService(aggregateExecutorService());
6774
service.setApplicationContext(applicationContext);
68-
return new DataBeanAggregateQueryFacadeImpl(service);
75+
return service;
6976
}
7077

7178
@Bean(name = "aggregateExecutorService")

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/service/impl/DataBeanAggregateQueryServiceImpl.java

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public DataBeanAggregateQueryServiceImpl(DataProviderRepository repository) {
4141
public <T> T get(String id, Map<String, Object> invokeParams, Class<T> resultType,final Map<InvokeSignature,Object> queryCache)
4242
throws InterruptedException, InvocationTargetException, IllegalAccessException {
4343
Assert.isTrue(repository.contains(id),"id not exist");
44-
long startTime = System.currentTimeMillis();
4544
DataProvideDefinition provider = repository.get(id);
4645
Map<String,Object> dependObjectMap = new HashMap<>(provider.getDepends().size());
4746
if(provider.getDepends() != null && ! provider.getDepends().isEmpty()) {
@@ -92,24 +91,21 @@ public <T> T get(String id, Map<String, Object> invokeParams, Class<T> resultTyp
9291
+ methodArg.getParameter().getName());
9392
}
9493
}
95-
try {
96-
/* 如果调用方法是幂等的, 那么当方法签名和方法参数完全一致时, 可以直接使用缓存结果 */
97-
InvokeSignature invokeSignature = new InvokeSignature(provider.getMethod(),args);
98-
Object resultModel;
99-
if(queryCache.containsKey(invokeSignature)) {
100-
resultModel = queryCache.get(invokeSignature);
101-
}
102-
else {
103-
resultModel = provider.getMethod()
104-
.invoke(applicationContext.getBean(provider.getMethod().getDeclaringClass()), args);
105-
/* Map 中可能不能放空value */
106-
queryCache.put(invokeSignature,resultModel != null ? resultModel : AggregatorConstant.EMPTY_MODEL);
107-
}
10894

109-
return resultType.cast(resultModel != AggregatorConstant.EMPTY_MODEL ? resultModel : null);
110-
} finally {
111-
logging(id, startTime, provider);
95+
/* 如果调用方法是幂等的, 那么当方法签名和方法参数完全一致时, 可以直接使用缓存结果 */
96+
InvokeSignature invokeSignature = new InvokeSignature(provider.getMethod(),args);
97+
Object resultModel;
98+
if(queryCache.containsKey(invokeSignature)) {
99+
resultModel = queryCache.get(invokeSignature);
112100
}
101+
else {
102+
resultModel = provider.getMethod()
103+
.invoke(applicationContext.getBean(provider.getMethod().getDeclaringClass()), args);
104+
/* Map 中可能不能放空value */
105+
queryCache.put(invokeSignature,resultModel != null ? resultModel : AggregatorConstant.EMPTY_MODEL);
106+
}
107+
108+
return resultType.cast(resultModel != AggregatorConstant.EMPTY_MODEL ? resultModel : null);
113109
}
114110

115111
private void throwException(ExecutionException e) throws InterruptedException,
@@ -125,18 +121,4 @@ private void throwException(ExecutionException e) throws InterruptedException,
125121
}
126122
}
127123

128-
private void logging(String id, long startTime, DataProvideDefinition provider) {
129-
if (runtimeSettings.isEnableLogging() && log.isInfoEnabled()) {
130-
log.info("query id: {}, " +
131-
"costTime: {}ms, " +
132-
"resultType: {}, " +
133-
"invokeMethod: {}",
134-
id,
135-
System.currentTimeMillis() - startTime,
136-
provider.getMethod().getReturnType().getSimpleName(),
137-
provider.getMethod().getDeclaringClass().getSimpleName() + "#" + provider.getMethod().getName()
138-
);
139-
}
140-
}
141-
142124
}

spring-boot-data-aggregator-example/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<artifactId>spring-boot-starter-test</artifactId>
3737
<scope>test</scope>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-configuration-processor</artifactId>
42+
<optional>true</optional>
43+
</dependency>
3944
<dependency>
4045
<groupId>io.github.lvyahui8</groupId>
4146
<artifactId>spring-boot-data-aggregator-starter</artifactId>

spring-boot-data-aggregator-example/src/main/java/io/github/lvyahui8/spring/example/ExampleApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @since 2019/6/1 0:13
1818
*/
1919

20-
@SpringBootApplication(scanBasePackages = "io.github.lvyahui8.spring.example")
20+
@SpringBootApplication
2121
@Slf4j
2222
public class ExampleApplication {
2323

spring-boot-data-aggregator-example/src/main/java/io/github/lvyahui8/spring/example/aspect/AggregateQueryLoggingAspect.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package io.github.lvyahui8.spring.example.aspect;
22

3+
import io.github.lvyahui8.spring.example.configuration.ExampleProperties;
34
import lombok.extern.slf4j.Slf4j;
45
import org.aspectj.lang.ProceedingJoinPoint;
56
import org.aspectj.lang.annotation.Around;
67
import org.aspectj.lang.annotation.Aspect;
7-
import org.aspectj.lang.annotation.Pointcut;
8+
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.stereotype.Component;
910

1011
/**
@@ -16,20 +17,24 @@
1617
@Slf4j
1718
public class AggregateQueryLoggingAspect {
1819

19-
public AggregateQueryLoggingAspect() {
20-
log.info("init aspect bean");
21-
}
22-
23-
@Pointcut("execution(* io.github.lvyahui8.spring.aggregate.service.impl.DataBeanAggregateQueryServiceImpl.*(..))")
24-
public void aggregateQuery() {}
20+
@Autowired
21+
private ExampleProperties exampleProperties;
2522

26-
@Around("execution(* io.github.lvyahui8.spring.aggregate.service.impl.DataBeanAggregateQueryServiceImpl.*(..))")
23+
@Around("execution(* io.github.lvyahui8.spring.aggregate.service.impl.DataBeanAggregateQueryServiceImpl.get(..))")
2724
public Object doLogging(ProceedingJoinPoint joinPoint) throws Throwable {
25+
long startTime = System.currentTimeMillis();
2826
Object retVal ;
2927
try {
3028
retVal = joinPoint.proceed();
3129
} finally {
32-
log.info("x");
30+
Object[] args = joinPoint.getArgs();
31+
if(log.isInfoEnabled() && exampleProperties.isLogging()) {
32+
log.info("query id: {}, " +
33+
"costTime: {}ms, " ,
34+
args[0],
35+
System.currentTimeMillis() - startTime
36+
);
37+
}
3338
}
3439
return retVal;
3540
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.lvyahui8.spring.example.configuration;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
9+
* @since 2019/7/8 22:04
10+
*/
11+
@Component
12+
@ConfigurationProperties(prefix = "example")
13+
@Data
14+
public class ExampleProperties
15+
{
16+
private boolean logging = false;
17+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
spring.main.banner-mode=off
22
io.github.lvyahui8.spring.base-packages=io.github.lvyahui8.spring.example
33
io.github.lvyahui8.spring.ignore-exception=false
4-
io.github.lvyahui8.spring.enable-logging=false
5-
spring.aop.auto=true
6-
spring.aop.proxy-target-class=true
4+
example.logging=true

0 commit comments

Comments
 (0)