java中DriverManager跟getdatasource方法获取getConnection有什么不同

&&&您需要以后才能回答,未注册用户请先。关于java中接口Connection的问题?_百度知道
关于java中接口Connection的问题?
其中createStatement()是接口Connection的方法;
Statement stat = conn,user.getConnection(url.createStatement(),但是为何conn这个对象却可以直接调用createStatement()这个方法呢Connection conn = DriverManager,password)
所以可以直接调用createStatement();记住,而不是具体的实现类,user,而实现类可以是多种变化的,password),所以通常我们在调用时写接口首先DriverManager,可以降低代码的耦合性;这个返回的是Connection接口的一个实例化对象。相当于conn是Connection接口的实例化:接口用来定义申明.getConnection(url
来自团队:
其他类似问题
为您推荐:
其他5条回答
同学,你需要理解的是实现、继承和多态的问题。
class A extends B{}
A为B的子类,A和B假如都有一个 void print()方法
B test=new A();
我们可以看到,test引用的类型是B,但是它的实例是A。
因为A是B的子类,所以这个是可以实现的。
那么test.print()调用的是谁的方法呢?
答案是A的方法。
只有当A没有print方法的时候,才会从上一级(父类)里寻找这个print
接口的性质也是一样的
回到问题:
Connection conn = DriverManager.getConnection(url,user,password);
在这个里面,虽然conn的类型是Connection,但是它的实现是DriverManager.getConnection(url,user,password);的返回值。
既是说,实例是取自getConnnection方法里的。
如果你有源码,可以往里面看看。
当然,你...
这个很正常啊
conn是Connection的对象啊
conn不就是Connection接口的 对象吗 所以能掉
conn这个对象是Connection类的,所以能够调用里面的方法。
Connection是类吗?我怎么在API里看到说是一个接口啊!
conn是connection类型的,另外 接口也是类,不过是个特殊的类。
conn就是Connection
java的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁最近做项目,做测试时,无意中遇到了一个问题:分别用BeanFactory和用ApplicationContext的getBean方法获取dataSource,用ApplicationContext获取成功,而用BeanFactory获取则报错
public class DataSourceTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("resources/spring/applicationContext-hibernate.xml");
DataSource ds = (DataSource) ctx.getBean("dataSource");
System.out.println(ds);
输出结果:
public class JdbcTemplateTest {
static String delimiter = File.
static String classpath = "resources"+delimiter+"spring"+delimiter+"applicationContext-hibernate.xml";
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath));
DataSource ds = (DataSource) factory.getBean("dataSource");
* 以上方法获得DataSource的Bean会报错:说找不到数据库的驱动类
System.out.println(ds);
运行报错:
Error creating bean with name 'dataSource' defined in class path resource [resources/spring/applicationContext-hibernate.xml]:
Error set nested exception is org.springframework.beans.PropertyBatchUpdateE
nested PropertyAccessExceptions (1) are:PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName'
nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not load JDBC driver class [${jdbc.driverClassName}]; nested exception is java.lang.ClassNotFoundException: ${jdbc.driverClassName}
我的配置文件位置如下所示:
resouces包-----
config子包:放数据库配置信息:jdbc.properties文件
spring子包:放spring的配置文件:applicationContext.xml
两个文件的内容如下:
jdbc.properties文件内容:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@localhost:1521:LOCALDEVjdbc.username=ccicjdbc.password=ccic
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName"&
&!-- 属性文件读入 --&
&bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&
&property name="locations"&
&value&classpath:resources/config/jdbc.properties&/value&
&/property&
&bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&
&property name="driverClassName" value="${jdbc.driverClassName}"/&
&property name="url" value="${jdbc.url}" /&
&property name="username" value="${jdbc.username}" /&
&property name="password" value="${jdbc.password}" /&
问题:同样是读取同一个配置文件,Spring的BeanFactory与ApplicationContext的getBean方法有什么区别?为什么一个行一个不行呢?
我只知道它们两个在延迟加载方面有区别:BeanFactory的getBean是延迟加载,ApplicationContext的getBean是在容器启动时就创建
采纳的答案
特性BeanFactoryApplicationContextBean实例化/装配YesYes自动BeanPostProcessor注册NoYes自动BeanFactoryPostProcessor注册NoYes便捷的MessageSource访问(i18n)NoYesApplicationEvent发送NoYes
ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean
如果要在BeanFactory中使用,bean factory post-processor必须手动运行
BeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new ClassPathResource("resources/config/jdbc.properties"));
cfg.postProcessBeanFactory(factory);
DataSource ds = (DataSource) factory.getBean("dataSource");
解释清楚明了
那个,不会用表格标签,表格错位了
特性 &&&&&&&&&&&&&&&&&&&&&&&&&& BeanFactory& ApplicationContext
Bean实例化/装配&&&&&&&&&&&&&&&&&&&&& Yes&&&&&&&&& Yes
自动BeanPostProcessor注册&&&&&&&&& No&&&&&&&&&& Yes
自动BeanFactoryPostProcessor注册& No&&&&&&&&&& Yes
便捷的 MessageSource访问(i18n)&&& No&&&&&&&&&& Yes
ApplicationEvent发送&&&&&&&&&&&&&& No&&&&&&&&&& Yes
已解决问题
未解决问题java中DriverManager调用getConnection()方法后为什么能赋给一个Connection对象?_百度知道
java中DriverManager调用getConnection()方法后为什么能赋给一个Connection对象?
DriverManager类和Connection类怎么建立 联系,使之DriverManager类的对象能赋给Connection类的对象呢?
这是通过舍呢么实现的。换句话说就是有一个方方,返回的是一个整型数据,但是很重要我这个问题可能有点傻,麻烦网友解答下,某个对象调用这个方法可以赋给一个整型变量
String user, password); be null.getCallerClassLoader(), user),不知你能读懂;
return (getConnection(
if (user .put(&!= null) {
info, info.util.util? public static Connection getConnection(Spassword&quot.Properties()!= null) { Gets the classloader of the code tha.
ClassLoader callerCL = DriverManager.Properties info =&#47, String password) throws SQLException {/
/user&quot, callerCL)), may
&#47.put(&quot以下是DriverManager类里的getConnection的源码
其他类似问题
为您推荐:
drivermanager的相关知识
其他2条回答
本来就是返回connection
这个和声明变量并赋值一样的啊,这你都不懂吗?
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 getdatasource方法 的文章

 

随机推荐