1. FastJson 泛型转换踩坑,缓存问题 : http://blog.csdn.net/ykdsg/article/details/50432494
2. Spring中properties配置文件参数读取问题,Spring 父子容器,参见:http://blog.csdn.net/fouy_yun/article/details/48437505 https://www.cnblogs.com/hafiz/p/5875740.html
AOP扫描也需要注意 父子容器问题,另外,要启用AspectJ对Annotation的支持:<aop:aspectj-autoproxy/>
3. jar找不到:双击tomcat看配置文件,Server Location中通过 Server path 可以查看eclipse中临时项目位置(tomcat部署的实际项目)
4. tomcat启动项目,一直找不到某些jar:可能是缓存问题,mvn clean package 清除缓存,成功重新打包试试
5. 引入mvn仓库没有的jar包,首先将jar包拷贝到lib目录下,然后pom.xml配置:
<dependency> <groupId>com.tencent.xinge</groupId> <artifactId>PushJavaSDK</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/PushJavaSDK.jar</systemPath> </dependency> <!-- 打war包时,将jar包拷贝进去 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>${project.basedir}/../erp-service/lib</directory> <targetPath>WEB-INF/lib</targetPath> <filtering>false</filtering> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> <version>2.1.1</version> </plugin>
6. 分布式锁:
String lockKey = LockConstants.LOCK_KEY_ACTIVE_LEAGUE_PREFIX + activateCarUnionDTO.getDealerId(); if (!redissonLockService.tryLock(lockKey)) { return RespMsg.fail("重复请求"); //直接返回 } @Slf4j @Service("redissonLockService") public class RedissonLockServiceImpl implements LockService { @Autowired @Qualifier("redisson") private RedissonClient redisson; @Override public boolean tryLock(String key) { return tryLock(key, 0L, TimeUnit.SECONDS); } @Override public boolean tryLock(String key, long timeout, TimeUnit unit) { return tryLock(key,timeout, LockService.DEFAULT_EXPIRE_TIME,unit); } @Override public boolean tryLock(String key,long waitTime, long leaseTime, TimeUnit unit) { try { RLock lock = redisson.getLock(key); boolean res = lock.tryLock(waitTime, leaseTime, unit); return res; } catch (InterruptedException e) { RedissonLockServiceImpl.log.error("获取redis锁异常, key is {}, timeout is {},leaseTime is {}, unit is {}.", key, waitTime,leaseTime, unit, e); throw new LockException(RespStatusEnum.FAIL,"缓存锁定失败"); } } @Override public void lock(String key) { RLock lock = redisson.getLock(key); lock.lock(); } @Override public void unLock(String key) { RLock lock = redisson.getLock(key); lock.unlock(); } }
7. 校验返回值工具类:
public class RespUtils { private static Logger logger= LoggerFactory.getLogger(RespUtils.class); public static <T>T resultWithCheck(RespDTO<T> respDTO, String serviceName) throws ServiceException { if (respDTO.getStatus() != RespStatusEnum.SUCCESS.getStatus()) { logger.error("调用{}服务异常,返回结果,status:{},msg:{}",serviceName, respDTO.getStatus(), respDTO.getMsg()); throw new ServiceException(respDTO.getMsg()); } return respDTO.getData(); } public static <T>T resultWithCheck(RespMsg<T> respDTO, String serviceName) throws ServiceException { if (respDTO.getStatus() != RespStatusEnum.SUCCESS.getStatus()) { logger.error("调用{}服务异常,返回结果,status:{},msg:{}",serviceName, respDTO.getStatus(), respDTO.getMsg()); throw new ServiceException(respDTO.getMsg()); } return respDTO.getData(); } public static <T>T resultWithCheck(RespMsg<T> respDTO, String serviceName, String callMethod) throws ServiceException { if (respDTO.getStatus() != RespStatusEnum.SUCCESS.getStatus()) { logger.error("调用{}服务异常,返回结, callMethod:{}, status:{},msg:{}",serviceName, callMethod, respDTO.getStatus(), respDTO.getMsg()); throw new ServiceException(respDTO.getMsg()); } return respDTO.getData(); } public static <T>T resultWithCheck(com.mljr.car.league.common.dto.RespDTO<T> respDTO, String serviceName) throws ServiceException { if (respDTO.getRc() != RespCodeEnum.SUCCESS.getStatus()) { logger.error("调用{}服务异常,返回结果,status:{},msg:{}",serviceName, respDTO.getRc(), respDTO.getMsg()); throw new ServiceException(respDTO.getMsg()); } return respDTO.getData(); } }
8. 数据库字段设置唯一索引,如果继续插入重复数据,可能导致异常。这种异常需要明确提示“数据重复”。可以通过判断异常名称,来确定返回信息。
catch(Exception e){ String local = e.getMessage(); logger.info("local{}"+local); String localizeMsg = e.getLocalizedMessage(); logger.info("localizeMsg{}"+localizeMsg); if(localizeMsg.contains("MySQLIntegrityConstraintViolationException")){ return RespMsg.fail("车商门店重复,无法添加"); } logger.error(msg,e); }
9. 泛型
// 成员变量泛型在 类上声明? // 方法参数泛型,在方法上声明? @Data public class RespMsg<T,V> { private String msg; private int status; private T data; private V data2; public RespMsg(String msg,int status,T data){ this.msg = msg; this.status = status; this.data = data; } public <T,E> void test(T data,E date){ } }
10. Stream用法: (https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/):
List<String> output = wordList.stream().map(String::toUpperCase).collect(Collectors.toList());
11. Spring自动注入properties文件:http://1358440610-qq-com.iteye.com/blog/2090955
12. Java 8:通过反射获取参数名:在Java 8中,使用javac编译器的时候加上-parameters参数的话,会在生成的.class文件中额外存储参数的元信息。当你输入javac -help的时候,你会看到-parameters这个选项
参见:http://deepinmind.iteye.com/blog/2046050