首页>>后端>>Spring->java源码解析?

java源码解析?

时间:2023-12-07 本站 点击:0

如何快速读懂项目源码javaWeb

一:学会如何读一个JavaWeb项目源代码 步骤:表结构-web.xml-mvc-db-spring

ioc-log- 代码

1、先卖祥了解项目数据库的表结构,这个方面是最容易忘记 的,有时候我们只顾着看每一个方法是怎么进行的,却没

有去了解数据库之间的主外键关联。其实如果先了解数据 库表结构,再去看一个方法的实现会更加容易。

2、然后需要过一遍web.xml,知道项目中用到了什么拦

截器,监听器,过滤器,拥有哪些配置文件。如果是拦截 器,一般负责过滤请求,进行AOP 等;如果是监 可能是定时任务,初始化任务;配置文件有如使用了 spring

后的读取mvc 相关,db 相关,service 相关,aop 相关的文件。

3、查看拦截器,监听器代码,知道拦截了什么请求,这

个类完成了怎样的工作。有的人就是因为缺少了这一步, 自己写了一个action,配置文件也没有写错,但是却怎么

调试也无法进入这个action,直到别人告诉他,请求被拦

4、接下来,看配置文件,首先一定是mvc相关的,如 springmvc

中,要请求哪些请求是静态资源,使用了哪些 view 策略,侍陵controller 注解放在哪个包下等。 然后是db 相关配置文件,看使用了什么数据库,使用了

什么orm框架,是否开启了二级老配戚缓存,使用哪种产品作 为二级缓存,事务管理的处理,需要扫描的实体类放在什 么位置。最后是spring 核心的ioc

功能相关的配置文件, 知道接口与具体类的注入大致是怎样的。当然还有一些如 apectj 置文件,也是在这个步骤中完成

5、log

相关文件,日志的各个级别是如何处理的,在哪些 地方使用了log 记录日志

6、从上面几点后知道了整个开源项目的整体框架,阅读 每个方法就不再那么难了。

7、当然如果有项目配套的开发文档也是要阅读的。

源码解析-偏向锁撤销流程解读

源码链接:

简单总结下偏向撤销的流程:

细节补充:

如何判断偏向所有者没有正在持有该偏向锁?

分两步,首先判断偏向所有者是否还活着,如果还活着,则遍历它的栈,看是否能找到关联该锁的锁记录,如果找到,则正在持有,如果没找到,则没有持有。(遍历过程在一个安全点执行,此时偏向所有者被阻塞。)

偏向所有者正在持有该偏向锁,如何将其撤销为轻量级锁?

遍历偏向所有者的栈,修改与该锁关联的所有顷橘旅锁记录,让偏向所有者以为它对该对象加的就是轻量级锁。

源码中的 highest_lock,为什么说是最早关联偏向锁的锁记录呢?

首伍喊先,锁记录在栈里是连续存放的。

请求获取锁时,按照从低地址到高地址的顺序,找在已关联该锁的锁记录之前,最后一个空闲的锁记录(没有指向雀凳任何锁对象)。

请求锁的源码如下:

而撤销偏向锁时,遍历偏向所有者的锁记录,也是按照从低地址到高地址的顺序,但它没有 break 的逻辑,因为它要处理所有关联该锁的锁记录。所以退出循环后,highest_lock 指向的是最早关联该锁的锁记录。

这篇: 源码解析-触发批量撤销或批量重偏向的条件 ,介绍了批量撤销的触发条件。

包含批量撤销逻辑的源码:

禁用类的可偏向属性有两点作用:

对于批量撤销时,正在被线程持有的偏向锁,通过在安全点遍历所有 Java 线程的栈,将偏向锁撤销为轻量级锁。

JAVA源码解析的时候 Character.MIN_RADIX为什么=2?

‘’占一个蚂誉位,你想一下,如果值是1,那么你怎么输笑物租入汉字

Character类的使用方法

Character:字符类型

1、属碰兆性。

static int MIN_RADIX :返回最小基数。

java 解析 eml的源代码

// 从EML文件得到MimeMessage对象

MimeMessage message = new MimeMessage(session, new FileInputStream(emlFile));

public static String getMailSubject(Message message) throws 拆察Exception {

return MimeUtility.decodeText(message.getSubject());

}

public static String getMailSender(Message message) throws Exception {

String emailSender = null;

Address[] addresses = message.getFrom();

if (addresses == null || addresses.length  1) {

throw 旅橘茄new IllegalArgumentException("该邮件没有发件人");

}

// 获得发件人

InternetAddress address = (InternetAddress) addresses[0];

String senderName = address.getPersonal();

if (senderName != null) {

senderName = MimeUtility.decodeText(senderName);

emailSender = senderName + "" + address.getAddress() + "";

} else {

senderName = address.getAddress();

}

return emailSender;

}

public static String getMailRecipients(Message message, Message.RecipientType recipientType) throws Exception {

StringBuilder builder = new StringBuilder();

Address[] addresses = null;

if (recipientType == null) {

addresses = message.getAllRecipients();

} else {

addresses = message.getRecipients(recipientType);

}

if (addresses == null || addresses.length  1) {

throw new IllegalArgumentException("该邮件没有收件人");

}

for (Address address : addresses) {

InternetAddress iAddress = (InternetAddress) address;

builder.append(iAddress.toUnicodeString()).append(", ");

}

return builder.deleteCharAt(builder.length() - 1).toString();

}

public static String getMailSendDate(Message message, String pattern) throws Exception {

String sendDateString = null;

if (pattern == null || "".equals(pattern.trim())) {

pattern = "yyyy年MM月dd日 E HH:mm";

}

Date sendDate = message.getSentDate();

sendDateString = new SimpleDateFormat(pattern).format(sendDate);

return 伍凯sendDateString;

}

public static boolean containsAttachment(Part part) throws Exception {

boolean flag = false;

if (part != null) {

if (part.isMimeType("multipart/*")) {

MimeMultipart mp = (MimeMultipart) part.getContent();

for (int i = 0; i  mp.getCount(); i++) {

BodyPart bodyPart = mp.getBodyPart(i);

String disposition = bodyPart.getDisposition();

if (disposition != null  (Part.ATTACHMENT.equalsIgnoreCase(disposition)

|| Part.INLINE.equalsIgnoreCase(disposition))) {

flag = true;

} else if (bodyPart.isMimeType("multipart/*")) {

flag = containsAttachment(bodyPart);

} else {

String contentType = bodyPart.getContentType();

if (contentType.indexOf("application") != -1) {

flag = true;

}

if (contentType.indexOf("name") != -1) {

flag = true;

}

}

if (flag)

break;

}

} else if (part.isMimeType("message/rfc822")) {

flag = containsAttachment((Part) part.getContent());

}

}

return flag;

}

public static boolean isSeen(Message message) throws Exception {

if (message == null) {

throw new MessagingException("Message is empty");

}

return message.getFlags().contains(Flags.Flag.SEEN);

}

public static boolean isReplaySign(Message message) throws Exception {

if (message == null) {

throw new MessagingException("Message is empty");

}

boolean replaySign = false;

String[] headers = message.getHeader("Disposition-Notification-To");

if (headers != null  headers.length  0) {

replaySign = true;

}

return replaySign;

}

public static String getMailPriority(Message message) throws Exception {

if (message == null) {

throw new MessagingException("Message is empty");

}

String priority = "普通";

String[] headers = message.getHeader("X-Priority");

if (headers != null  headers.length  0) {

String mailPriority = headers[0];

if (mailPriority.indexOf("1") != -1 || mailPriority.indexOf("High") != -1) {

priority = "紧急";

} else if (mailPriority.indexOf("5") != -1 || mailPriority.indexOf("Low") != -1) {

priority = "低";

} else {

priority = "普通"; // 3或者Normal;

}

}

return priority;

}

public static void getMailTextContent(Part part, StringBuilder content) throws Exception {

if (part == null) {

throw new MessagingException("Message content is empty");

}

boolean containsTextInAttachment = part.getContentType().indexOf("name")  0;

if (part.isMimeType("text/*")  containsTextInAttachment) {

content.append(part.getContent().toString());

} else if (part.isMimeType("message/rfc822")) {

getMailTextContent((Part) part.getContent(), content);

} else if (part.isMimeType("multipart/*")) {

Multipart mp = (Multipart) part.getContent();

for (int i = 0; i  mp.getCount(); i++) {

BodyPart bodyPart = mp.getBodyPart(i);

getMailTextContent(bodyPart, content);

}

} else if (part.isMimeType("image/*")) {

// TODO part.getInputStream()获得输入流然后输出到指定的目录

} else {

// TODO 其它类型的contentType, 未做处理, 直接输出

content.append(part.getContent().toString());

}

}

public static void saveAttachment(Part part, String destDir) throws Exception {

if (part == null) {

throw new MessagingException("part is empty");

}

// 复杂的邮件包含多个邮件体

if (part.isMimeType("multipart/*")) {

Multipart mp = (Multipart) part.getContent();

// 遍历每一个邮件体

for (int i = 0; i  mp.getCount(); i++) {

BodyPart bodyPart = mp.getBodyPart(i);

// bodyPart也可能有多个邮件体组成

String disposition = bodyPart.getDisposition();

if (disposition == null  (Part.ATTACHMENT.equalsIgnoreCase(disposition)

|| Part.INLINE.equalsIgnoreCase(disposition))) {

InputStream in = bodyPart.getInputStream();

saveFile(in, destDir, decodeText(bodyPart.getFileName()));

} else if (bodyPart.isMimeType("multipart/*")) {

saveAttachment(bodyPart, destDir);

} else {

String contentType = bodyPart.getContentType();

if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {

saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));

}

}

}

} else if (part.isMimeType("message/rfc822")) {

saveAttachment((Part) part.getContent(), destDir);

}

}

public static void saveFile(InputStream in, String destDir, String fileName) throws Exception {

FileOutputStream out = new FileOutputStream(new File(destDir + fileName));

byte[] buffer = new byte[1024];

int length = 0;

while ((length = in.read(buffer)) != -1) {

out.write(buffer, 0, length);

}

out.close();

in.close();

}

public static String decodeText(String encodedText) throws Exception {

if (encodedText == null || "".equals(encodedText.trim())) {

return "";

} else {

return MimeUtility.decodeText(encodedText);

}

}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:/Spring/18204.html