Commit 1eb72cce37206063bbc206b9f91aff3bc715e3e0
1 parent
1fadabe078
Exists in
master
and in
6 other branches
公共配置抽取
Showing 6 changed files with 79 additions and 9 deletions
- regional-etl/src/main/java/com/lyms/etl/ApplicationRunner.java
- regional-etl/src/main/java/com/lyms/etl/service/impl/CouponServiceImpl.java
- regional-etl/src/main/java/com/lyms/etl/util/AopTargetUtil.java
- regional-etl/src/main/resources/application-prod.yml
- regional-etl/src/main/resources/application.properties
- regional-etl/src/main/resources/application.yml
regional-etl/src/main/java/com/lyms/etl/ApplicationRunner.java
View file @
1eb72cc
| 1 | 1 | package com.lyms.etl; |
| 2 | 2 | |
| 3 | 3 | import com.lyms.etl.service.IInvokeHandler; |
| 4 | +import com.lyms.etl.util.AopTargetUtil; | |
| 4 | 5 | import org.mybatis.spring.annotation.MapperScan; |
| 5 | 6 | import org.slf4j.Logger; |
| 6 | 7 | import org.slf4j.LoggerFactory; |
| 7 | 8 | |
| ... | ... | @@ -30,14 +31,13 @@ |
| 30 | 31 | |
| 31 | 32 | public static void main(String[] args) { |
| 32 | 33 | SpringApplication.run(ApplicationRunner.class, args); |
| 33 | - log.info("etl started......"); | |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | @Override |
| 37 | 37 | public void run(String... strings) throws Exception { |
| 38 | 38 | for (IInvokeHandler handler : invokeHandlers) { |
| 39 | 39 | Date start = new Date(); |
| 40 | - log.info("start ................ {}", handler.getClass().getName()); | |
| 40 | + log.info("start ................ {}", AopTargetUtil.getTarget(handler).getClass().getName()); | |
| 41 | 41 | handler.invoke(); |
| 42 | 42 | log.info("end ................ used: {} ms", System.currentTimeMillis() - start.getTime()); |
| 43 | 43 | } |
regional-etl/src/main/java/com/lyms/etl/service/impl/CouponServiceImpl.java
View file @
1eb72cc
regional-etl/src/main/java/com/lyms/etl/util/AopTargetUtil.java
View file @
1eb72cc
| 1 | +package com.lyms.etl.util; | |
| 2 | + | |
| 3 | +import org.springframework.aop.framework.AdvisedSupport; | |
| 4 | +import org.springframework.aop.framework.AopProxy; | |
| 5 | +import org.springframework.aop.support.AopUtils; | |
| 6 | + | |
| 7 | +import java.lang.reflect.Field; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * spring自动注入的对象 通过getClass()获取到的是proxy对象 | |
| 11 | + * 通过此类获取到真实对象 | |
| 12 | + * @Author: litao | |
| 13 | + * @Date: 2017/5/6 0006 11:21 | |
| 14 | + * @Version: V1.0 | |
| 15 | + */ | |
| 16 | +public class AopTargetUtil { | |
| 17 | + /** | |
| 18 | + * 获取 目标对象 | |
| 19 | + * @param proxy 代理对象 | |
| 20 | + * @return | |
| 21 | + * @throws Exception | |
| 22 | + */ | |
| 23 | + public static Object getTarget(Object proxy) { | |
| 24 | + if(!AopUtils.isAopProxy(proxy)) { | |
| 25 | + return proxy;//不是代理对象 | |
| 26 | + } | |
| 27 | + | |
| 28 | + if(AopUtils.isJdkDynamicProxy(proxy)) { | |
| 29 | + return getJdkDynamicProxyTargetObject(proxy); | |
| 30 | + } else { //cglib | |
| 31 | + return getCglibProxyTargetObject(proxy); | |
| 32 | + } | |
| 33 | + } | |
| 34 | + | |
| 35 | + | |
| 36 | + private static Object getCglibProxyTargetObject(Object proxy) { | |
| 37 | + Object target = null; | |
| 38 | + try { | |
| 39 | + Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0"); | |
| 40 | + h.setAccessible(true); | |
| 41 | + Object dynamicAdvisedInterceptor = h.get(proxy); | |
| 42 | + | |
| 43 | + Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised"); | |
| 44 | + advised.setAccessible(true); | |
| 45 | + | |
| 46 | + target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget(); | |
| 47 | + } catch (Exception e) { | |
| 48 | + | |
| 49 | + } | |
| 50 | + return target; | |
| 51 | + } | |
| 52 | + | |
| 53 | + | |
| 54 | + private static Object getJdkDynamicProxyTargetObject(Object proxy) { | |
| 55 | + Object target = null; | |
| 56 | + try { | |
| 57 | + Field h = proxy.getClass().getSuperclass().getDeclaredField("h"); | |
| 58 | + h.setAccessible(true); | |
| 59 | + AopProxy aopProxy = (AopProxy) h.get(proxy); | |
| 60 | + Field advised = aopProxy.getClass().getDeclaredField("advised"); | |
| 61 | + advised.setAccessible(true); | |
| 62 | + target = ((AdvisedSupport)advised.get(aopProxy)).getTargetSource().getTarget(); | |
| 63 | + } catch (Exception e) { | |
| 64 | + } | |
| 65 | + return target; | |
| 66 | + } | |
| 67 | + | |
| 68 | +} |
regional-etl/src/main/resources/application-prod.yml
View file @
1eb72cc
regional-etl/src/main/resources/application.properties
View file @
1eb72cc
| 1 | -server.port=8089 | |
| 2 | - | |
| 3 | 1 | # druid |
| 4 | 2 | spring.datasource.type=com.alibaba.druid.pool.DruidDataSource |
| 5 | 3 | spring.datasource.initialSize=5 |
| ... | ... | @@ -23,5 +21,5 @@ |
| 23 | 21 | mybatis.typeAliasesPackage=com.lyms.etl.model |
| 24 | 22 | mybatis.mapperLocations=classpath:mappers/*.xml |
| 25 | 23 | |
| 26 | -spring.profiles.active=prod,etl | |
| 24 | +spring.profiles.active=prod, etl |