- 浏览: 502781 次
- 性别:
- 来自: 沈阳
文章分类
- 全部博客 (437)
- Windows设置 (2)
- oracle数据库 (39)
- bug--jsp (4)
- j2se (13)
- js (40)
- bug-tomcat不能启动程序 (1)
- Hibernate (29)
- eclipse (20)
- java (65)
- 设计模式 (6)
- bug (18)
- PL/SQL (11)
- 前台 (5)
- 杂谈 (25)
- UML (1)
- jdbc编程 (2)
- 技术调研 (1)
- 数据通信 (2)
- ios (1)
- servlet自学笔记 (10)
- tomcat (9)
- SQL学习笔记 (6)
- java工具 (1)
- 数据库设计 (4)
- javascript (10)
- jsp (11)
- struts (17)
- ajax (7)
- linix/Unix (6)
- 资源 (3)
- spring (14)
- 算法 (5)
- 计算机网络 (2)
- http (5)
- c++ (2)
- web应用 (3)
- jvm (5)
- java中的字符编码 (14)
- java代码库 (2)
- classloader (1)
- 读书笔记 (1)
- c (1)
- 开源软件 (1)
- svn (1)
- AOP (1)
- java序列化 (1)
- 多线程 (4)
- The legendary programmers (1)
- Apache http Server (1)
- html tag (3)
- struts1.X学习笔记 (5)
- buffalo (1)
- 自己收藏 (0)
- TOEFL(IBT) (1)
- 网络翻墙 (0)
- 编译原理 (1)
- 书籍推荐 (1)
- css (10)
- javaee环境搭建资料 (1)
- 开源工具 (1)
- 美国生活 (1)
- spring自学 (3)
- log4j (3)
- 算法与数据结构 (5)
- 病毒,插件处理大全 (1)
- flex (2)
- webservice (1)
- git (7)
- cs (1)
- html (4)
- javaee (6)
- 开车 (0)
- springmvc (3)
- 互联网架构 (2)
- intellij idea (18)
- maven (15)
- mongodb (2)
- nginx (1)
- react (3)
- java基础例子 (2)
- springboot (2)
- 培训 (5)
- mysql (3)
- 数据库 (3)
- 生活 (2)
- intellij (3)
- linux (2)
- os (3)
最新评论
-
潇洒天涯:
[color=blue][color=cyan] ...
oracle 通过 nvl( )函数sql 查询时为 空值 赋默认值 -
hekai1990:
受教了..
oracle中的varchar2
通过thread.getContextClassloader:
thread.getContextClassloader默认返回AppClassLoader,除非你显式setContextClassloader
来看一下jdbc中如何使用classloader:
一般我们写一个jdbc程序都会这样:
Class.forName("com.mysql.jdbc.Driver");
Stringurl ="jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
Stringuser = "root";
Stringpsw = "yanyan";
Connectioncon = DriverManager.getConnection(url,user, psw);
为什么需要第一句话?
其实第一句话可以用下面这句话替代:
com.mysql.jdbc.Driverdriver = new com.mysql.jdbc.Driver();
其他都不用变化,有人会问,driver对象从来没有用到.对,它的效果就是在调用DriverManager的getConnection方法之前,保证相应的Driver类已经被加载到jvm中,并且完成了类的初始化工作就行了.注意了,如果我们进行如下操作,程序是不能正常运行的,因为这样仅仅使Driver类被装载到jvm中,却没有进行相应的初始化工作。
com.mysql.jdbc.Driverdriver = null;
//or:
ClassLoadercl = new ClassLoader();
cl.loadClass("com.mysql.jdbc.Driver");
我们都知道JDBC是使用Bridge模式进行设计的,DriverManager就是其中的Abstraction,java.sql.Driver是Implementor,com.mysql.jdbc.Driver是Implementor的一个具体实现(请参考GOF的Bridge模式的描述)。大家注意了,前一个Driver是一个接口,后者却是一个类,它实现了前面的Driver接口。
Bridge模式中,Abstraction(DriverManager)是要拥有一个Implementor(Driver)的引用的,但是我们在使用过程中,并没有将Driver对象注册到DriverManager中去啊,这是怎么回事呢?jdk文档对Driver的描述中有这么一句:
Whena Driver class is loaded, it should create an instance of itself andregister it with the DriverManager
哦,原来是com.mysql.jdbc.Driver在装载完后自动帮我们完成了这一步骤。源代码是这样的:
packagecom.mysql.jdbc
//Class.forName("");的作用是要求JVM查找并加载指定的类,如果在类中有静态初始化器的话,JVM会 // 执行该类的静态代码段。而在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,//即任何一个JDBC Driver的 Driver类的代码都必须类似如下:
publicclass Driver extends NonRegisteringDriver implements java.sql.Driver{
static{
try{
java.sql.DriverManager.registerDriver(newDriver());
}catch (SQLException E) {
thrownew RuntimeException("Can't register driver!");
}
}
publicDriver() throws SQLException {
//Required for Class.forName().newInstance()
}
}
再看一下DriverManager.getConnection(url,user, psw);方法:
ClassLoadercallerCL = DriverManager.getCallerClassLoader();
getConnection(url,info, callerCL)
==>
if(callerCL== null){
callerCL= Thread.currentThread().getContextClassLoader();
}
上面的意思是:代码意思是如果DriverManager类的类加载器为空的话,就使用当前线程的类加载器。仔细想想,DriverManager在rt.jar包中,它是由JDK的启动类加载器加载的,而启动类加载器是C编写的,所以取得的都是空,再者,使用当前线程类加载器的话,那么交由程序编写者来保证能够加载驱动类。而不至于驱动器类无法加载。非常高明的手段~!
Class的这种设计引入了一个有趣的模式:
某个框架制定某个API,而这些api的实现是有其他供应商来提供,为了能让框架类(处于较高层次的classloader)使用api的实现(处于较低层次的classloader)
通过thread.getContextClassloader是来传递classloader(有时候需要thread.setContextClassloader设置好api实现的classloader),用此classloader.getResources找出所有的api实现的具体类名,再用classloader加载之,此时框架都不需要知道api的实现类的类名就能加载之,程序显示了良好的动态性和可扩展性。
------------------------------------------
Class.forName("")返回的是类
1. 总是使用当前装载器(也就是装载执行forName()请求类的类装载器)
2. 总是初始化这个被装载类(当然包括:装载,连接,初始化)
Class.forName("").newInstance()返回的是object
-----------------------------------------
Class.forName(String className)
Returns the Class
object associated with the class or interface with the given string name
Class.forName(String name, boolean initialize, ClassLoader loader)
Returns the Class
object associated with the class or interface with the given string name, using the given class loader
Class.forName() 与 new 关键字的不同
new 关键字适合用于对class文件在app ClassLoader 里,直接加载
Class.forName()
1. 经典用法是在工厂模式,
String classname = "Example";
Class c = Class.forName(classname);
factory = (ExampleInterface)c.newInstance();
classname 可以通过改变赋值,使生成的factory可以是任意继承ExampleInterface的类
2. 经典的使用环境
上面提到的JDBC 的例子,为了适应不同厂商提供的JDBC Driver, 所以使用Class.forName 动态加载
3. 为什么需要Class.forName(), 很多时候,如果把所有jar一次性加载到JVM里, JVM寻找加载类的时间越过长, 所以需要类加载器去动态加载所需要的类。
为什么需要ContextClassLoader (上下文类加载器) 动态加载类包和其他资源。 ContextClassLoader不是具体某一种 ClassLoader, 更像是一种链表的根。 如果class A 里加载了class B, A 通过 App ClassLoader加载, ContextClassLoader 会给 提供App ClassLoader 同样去加载 B. 在多线程环境下, 也许一个线程里有几个类,而且并不是都是动态联编, 需要用当前的线程统一设置一个ContextClassLoader 应用的场景 当前类上下文加载 Class.getResource, Class.forName(); Java.util.ResourceBundle Java 序列化API默认调用者 当前线程上下文加载 JNDI JAXP URL protocol handlers specified via java.protocol.handler.pkgs system property are looked up in the bootstrap and system classloaders only
发表评论
-
【转】Spring的DAO异常-你可能忽视的异常
2018-10-11 05:04 539Spring的DAO框架没有抛出与特定技术相关的异常,例如 ... -
【转】java8 Optional
2018-10-05 02:39 467https://my.oschina.net/wangz ... -
java double checked locking broken
2018-09-15 01:56 486// Double-check idiom for lazy ... -
【转】JAVA泛型通配符(PECS)
2018-07-29 10:43 513在JAVA的泛型集合中,默认都可以添加null,除此以外, ... -
Differences between notify() and notifyAll()
2018-07-16 09:01 521Notification to number of th ... -
【转】深度解析Java多线程的内存模型
2018-07-16 09:00 486https://www.jianshu.com/p/a3f ... -
maven项目src源代码下的资源文件不自动复制到classes文件夹的解决方法
2018-07-01 23:34 1187POM文件 <build><resour ... -
java好用的开源库
2018-04-28 23:40 0guava--google第三方数据结构开源包 Vardu ... -
【转】java rmi
2018-01-03 18:57 397此处讲的是Java中的RMI ... -
【转】字符编码笔记:ASCII,Unicode 和 UTF-8
2017-12-12 19:09 449今天中午,我突然想搞清楚 Unicode 和 UTF-8 之 ... -
【转】Java web 学习路线
2017-06-14 15:55 700JSP -> Servlet -> Java ... -
有意思的ASCII程序注释
2017-06-14 10:39 732/** * * create ... -
【转】Java基础知识总结(绝对经典)
2017-06-13 14:13 517本人学习java时,做的java基础知识总结: 因内容较 ... -
java定时任务
2017-03-27 10:08 409import java.util.concurrent.Ex ... -
二分查找(java实现)
2017-03-21 11:02 484二分查找 算法思想:又叫折半查找,要求待查找的序列有序。每 ... -
java环境配置
2017-03-15 15:46 437增加系统变量: JAVA_HOME C:\Program ... -
Java中的Big(Little)-endian问题的一种解决方法
2017-03-08 15:47 1030http://blog.sina.com.cn/s/blo ... -
【转】Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
2016-12-13 15:07 703http://www.cnblogs.com/hoojo ... -
【转】JNDI学习总结(三)——Tomcat下使用Druid配置JNDI数据源
2016-12-13 14:50 1274http://www.cuomi.com/html/co ... -
【转】JNDI学习总结(二)——Tomcat下使用C3P0配置JNDI数据源
2016-12-13 14:48 724http://blog.csdn.net/samjustin ...
相关推荐
12. talk - talked 13. jog - jogged 14. shop - shopped 15. knock - knocked 16. dance - danced 17. take - took 18. come - came 19. close - closed 20. sweep - swept 21. blow - blew 22. sit - sat 23. see ...
6. talked - 说话,交谈 7. train - 火车 8. house - 房子 二、发音归类: 1. /i / - take, big 2. /iə/ - hear, near 3. /Λ / - station, box 4. / ɔ / - hot, hill 5. / ei / - take, summer 三、动词的现在...
4. "Thank you for talking____ me." 固定搭配talk to sb,选B. to。 5. "She ____ football yesterday." 昨天发生的动作,用过去式played,选B. played。 三、翻译题: 这部分要求学生理解并翻译英文句子,比如: ...
Most of us have filled our iPhone or iPod touch with games, and many of us hope to develop the next best-selling, most talked-about game. You’ve probably already read and mastered Beginning iPhone ...
1. 动词过去式:题目要求写出一些动词的过去式,如 live(lived)、can(could)、do(did)、are(were)、watch(watched)、change(changed)、have(had)、is(was)、talk(talked)、cook(cooked)。...
Most of us have filled our iPhone or iPod touch with games, and many of us hope to develop the next best-selling, most talked-about game. You’ve probably already read and mastered Beginning iPhone ...
timeless context, and emphasizes the central theme of the book: adaptability to change. What’s Not In This Book The aim of this book is to arm readers with practical information and a way of think‐ ...
ECBA 企业聊天机器人应用 ...The idea is to use protypes for OO to save memory as talked about here: https://veerasundar.com/blog/2014/02/javascript-prototype-methods-vs-object-methods/ No tabs in
8. **An old lady talked about her life many years ago.** - 一位老妇人谈论了她许多年的生活。 9. **She couldn’t read or write.** - 她不会读也不会写。 ### 四、自然拼读 1. **ai, ay /ei/** - rain, play ...
10. An old lady talked about her life many years ago. - 一个年老的女士谈论了她许多年前的生活。 11. She couldn’t read or write. - 她不能读也不能写。(could 或者 couldn’t后面要加动词原形;否定句里要用...
10. An old lady talked about her life many years ago. - 一个年老的女士谈论了她许多年前的生活。 11. She couldn't read or write. - 她不能读也不能写。(could 或者 couldn't 后面要加动词原形,否认句里要用 ...
Most of us have filled our iPhone or iPod touch with games, and many of us hope to develop the next best-selling, most talked-about game. You’ve probably already read and mastered Beginning iPhone ...
Carver’s groundbreaking Policy Governance model is the best-known, respected, and talked about governance model in the world and has fundamentally influenced the way organizations are governed....
8. talk - talked 9. help - helped 10. go - went 11. run - ran 12. play - played 13. dance - danced 14. listen - listened 15. take - took 这些动词过去式的书写是小学英语学习的基础,通过这个练习,学生能...
- talk -> talked - practice -> practiced - listen -> listened - play -> played - visit -> visited - write -> wrote - do -> did - have -> had - go -> went - learn -> learned/learnt - take ->...
4. "He is often made __ for twelve hours a day by the boss." - 此题考察make的被动语态,需加to,正确答案是C. to work。 5. "He could neither ... nor..." - 此题考察并列结构和动词搭配,正确答案是A. speak...
12)There is a slight/slow/steady/rapid rise/increase demand./income./population./prices./production./decrease/decline/reduction/fall/drop in 13) be on the increase/decrease/rise/decline (……在不断的...
In late 2014, Carlos and Christian talked about extending the article and sharing all their knowledge of and experience in applying Domain-Driven Design in production. They were very excited about the...
So far we’ve talked about some theory and OOP design principles and making our request asynchronous. In this post we’re going to discuss what was going on inside of the AsyncTask to make the network...
- 原句:The students will go to a party that will be held in our class at 7:45. - 转换后:The students will go to a party to be held in our class at 7:45. 当定语从句的谓语动词是被动语态或者动作先于...