|
1
|
|
|
package io.github.ro4.annotation; |
|
2
|
|
|
|
|
3
|
|
|
import io.github.ro4.CheckTemplate; |
|
4
|
|
|
import io.github.ro4.check.DefaultExceptionProvider; |
|
5
|
|
|
import io.github.ro4.check.ExceptionProvider; |
|
6
|
|
|
import io.github.ro4.check.SpELChecker; |
|
7
|
|
|
import org.aopalliance.aop.Advice; |
|
8
|
|
|
import org.springframework.aop.Pointcut; |
|
9
|
|
|
import org.springframework.aop.support.AbstractPointcutAdvisor; |
|
10
|
|
|
import org.springframework.aop.support.ComposablePointcut; |
|
11
|
|
|
import org.springframework.beans.BeansException; |
|
12
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
13
|
|
|
import org.springframework.beans.factory.BeanFactoryAware; |
|
14
|
|
|
import org.springframework.beans.factory.InitializingBean; |
|
15
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
16
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
17
|
|
|
import org.springframework.context.annotation.Role; |
|
18
|
|
|
import org.springframework.stereotype.Component; |
|
19
|
|
|
|
|
20
|
|
|
@Role(BeanDefinition.ROLE_SUPPORT) |
|
21
|
|
|
@Component |
|
22
|
1 |
|
public class CheckConfiguration extends AbstractPointcutAdvisor implements InitializingBean, BeanFactoryAware { |
|
23
|
|
|
|
|
24
|
|
|
private transient Advice advice; |
|
25
|
|
|
|
|
26
|
|
|
private transient Pointcut pointcut; |
|
27
|
|
|
|
|
28
|
|
|
private transient BeanFactory beanFactory; |
|
29
|
|
|
|
|
30
|
|
|
@Override |
|
31
|
|
|
public Pointcut getPointcut() { |
|
32
|
1 |
|
return pointcut; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
@Override |
|
36
|
|
|
public Advice getAdvice() { |
|
37
|
1 |
|
return advice; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
@Override |
|
41
|
|
|
public void afterPropertiesSet() { |
|
42
|
1 |
|
pointcut = new ComposablePointcut(new CheckableMethodMatcher()); |
|
43
|
1 |
|
CheckableMethodInterceptor checkableMethodInterceptor = new CheckableMethodInterceptor(); |
|
44
|
1 |
|
checkableMethodInterceptor.setCheckTemplate(buildCheckTemplate()); |
|
45
|
1 |
|
checkableMethodInterceptor.setExceptionProvider(buildExceptionProvider()); |
|
46
|
1 |
|
advice = checkableMethodInterceptor; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected CheckTemplate buildCheckTemplate() { |
|
50
|
1 |
|
CheckTemplate checkTemplate = new CheckTemplate(); |
|
51
|
1 |
|
checkTemplate.setChecker(new SpELChecker(beanFactory)); |
|
52
|
1 |
|
return checkTemplate; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected ExceptionProvider<?> buildExceptionProvider() { |
|
56
|
|
|
try { |
|
57
|
|
|
return beanFactory.getBean(ExceptionProvider.class); |
|
58
|
1 |
|
} catch (NoSuchBeanDefinitionException e) { |
|
59
|
1 |
|
return new DefaultExceptionProvider(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
@Override |
|
64
|
|
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
|
65
|
1 |
|
this.beanFactory = beanFactory; |
|
66
|
1 |
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|