遇到一个取数的过程业务代码段:1
2
3
4
5
6// ......
List<T> list1 = xx.getListByParam(param1);
map.put("list1",list1);
List<T> list2 = xx.getListByParam(param2);
map.put("list2", list2);
// ......
其中,list1中,某属性的值,在list2获取的过程中,发生改变。出现问题。
解决方法1
2
3
4
5
6
7
8
9
10
11
12
13public static <T> List<T> deepCopy(List<T> list) throws IOException, ClassNotFoundException {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(arrayOutputStream);
objectOutputStream.writeObject(list);
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(arrayInputStream);
"unchecked") (
List<T> reList = (List<T>) objectInputStream.readObject();
return reList;
}
代码表现是:
避免出现此问题,导致数据不准确,则需要考虑Java List的深拷贝的问题。
变更记录
变更项 | 时间 | 更新者 |
---|---|---|
初始化文档,实现war包的处理 | 2020.01 | me |
部署flowable工作流引擎,需要根据所在服务器的IP和MySQL账密信息,修改很多配置项,为方便,这些配置项,采用脚本批处理的方式,将用户在终端输入的信息,进行处理并修改配置项。
脚本做了以下事情:
如图,使用的时候,保证脚本和压缩包在同一目录下:
这段代码是获取路径
1 | CURDIR=$( |
处理用户输入内容1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23cd flowable-admin/WEB-INF/classes
rm -rf application.properties
touch application.properties
提示端口,如果不输入,会有默认值9988
echo -n "[1]please input the server.port = ? (default is 9988)"
read port
if [[ $port == "" ]]; then
port='9988'
fi
echo -n "your server.port is $port"
echo -n "[13]please input the MySQL server host ip = ? (default is 127.0.0.1)"
read MySQLHostIp
if [[ $MySQLHostIp == "" ]]; then
MySQLHostIp='127.0.0.1'
fi
echo -n "[14]please input the MySQL server host port = ? (default is 3306)"
read MySQLHostPort
if [[ $MySQLHostPort == "" ]]; then
MySQLHostPort='3306'
fi
上文说过,如果错了,是可以根据序号修正输入信息的。如果flag不为y,则会一直询问正确输入,直至输入y为止。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28read flag
while [[ $flag == "n" ]]; do
echo 'input number 1 to 26, the numbers are not continuous'
echo 'your number is'
read aNum
case $aNum in
1)
echo 'number is 1, please fix your [port] current!'
read line1
port=$line1
;;
13)
echo 'number is 13, please fix your [the MySQL server host ip] current!'
read line13
MySQLHostIp=$line13
;;
14)
echo 'number is 14, please fix your [the MySQL server host port] current!'
read line14
MySQLHostPort=$line14
;;
*)
echo 'Error'
;;
esac
echo -n "confirm,if wrong, please choice 'n' to fix it !, input y/n:"
read flag
done
最后,使用
1 | cat >application.properties <<EOF |
并配合类似:
1 | spring.datasource.username=$datasourceUsername |
将读取终端input的变量值写入要生成的配置文件中。
https://flowable.oss-cn-beijing.aliyuncs.com/semi_auto_deploy_flowable_sample.sh
]]>日常开发中,虽然代码可用性通过,要求的功能实现了,但是有一些规范需要去遵守;避免不注意犯了一些低级错误;本文笔记梳理一些常忽略的点,以免下次再进坑;
1、公共类中的方法最好不要动;
2、uuid要谨慎使用;
3、公共的方法要放到公共的类中;
4、业务类的方法比较多,取名字要规范一些,长点不要紧,但是一定要读出来方法的作用是什么;如果表达不出来,加上必要的注释;
5、尽量不要捕获类似Exception通用异常,而是捕获特定的异常,具体到哪一种子类型;
6、在复杂的生产系统中,标准出错STERR不是合适的输出选项,尤其是分布式系统,最好使用产品日志;详细的输出到日志系统中;STREE无法找到堆栈轨迹;
不要在catch代码块中直接printStackTrace()
7、[配置文件]在微服务的resources下的配置文件,可能会有许多的配置文件,每个配置文件尽量要求做到名字直观易懂,必要时在配置文件中做出单独的说明;
8、Java的类名开头要写明是什么;
9、Java中的方法要写明作者、邮箱、版本、参数、返回;
如果图片不存在,就会在程序运行时引起异常,图片如果是网络资源,有一种方法,如下,可以处理图片异常的情况。
获取图片的url,记得关闭流。对于异常的情况,则给一个固定的、一定存在的连接。1
2
3
4
5
6
7
8
9
10try {
URL urlTemp = new URL(url + employeeInfo.getEmployeeId() + ".jpg");
URLConnection uc = urlTemp.openConnection();
InputStream in = uc.getInputStream();
in.close();
} catch (Exception e) {
map.put("photoUrl","http://192.168.6.125/static/photo/default/boy.png");
e.printStackTrace();
}