AcTest.class

package com.zyz.db;

import com.zyz.dao.PersonDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource; /**
* Created by zyz on 2016-8-5.
*/
public class AcTest {
public static void main(String[] args){ // 方法1:使用FileSystemXmlApplicationContext
ApplicationContext ac1=new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext-jdbc.xml");
DriverManagerDataSource dataSource=(DriverManagerDataSource) ac1.getBean("dataSource");
System.out.println(dataSource.getUsername()); // 方法2:使用ClassPathXmlApplicationContext
ApplicationContext ac2=new ClassPathXmlApplicationContext("beans.xml");
PersonDao personDao=(PersonDao)ac2.getBean("personDao");
     System.out.println(personDao.getVersion());
     System.out.println(personDao.getPersons().get(0).getName()); } }

方法1:只要指定xml文件的在项目中的相对位置

方法2:从classpath位置读xml文件,即编译后class文件存放的位置.
可以先将xml文件放入resources文件夹,然后将resources文件夹加入到classpath,在idea中:
"File"->"Project Structure",出现对话框,添加resources文件夹,编译运行后,系统会将xml文件复制到classes文件夹中

applicationContext-jdbc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
"> <!--配置mysql数据库-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/school?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean> <!--jdbcTemplate和数据库关联-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSource"/>
</bean>
</beans>

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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.xsd">
<bean id="personDao" class="com.zyz.dao.PersonDao">
<property name="version">
<value>3.85</value>
</property>
</bean>
</beans>

调用xml文件的bean的更多相关文章

  1. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  2. 01_1_jdom调用xml文件

    01_1_jdom调用xml文件 1. 导入jdom.jar包 2. xml文件内容 test.xml <?xml version="1.0" encoding=" ...

  3. 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean

    在 Spring 容器内拼凑 bean 叫做装配.装配 bean 的时候,你是在告诉容器,需要哪些 bean ,以及容器如何使用依赖注入将它们配合在一起. 理论上,bean 装配的信息可以从任何资源获 ...

  4. Struts2 学习笔记——struts.xml文件之Bean的配置

    Struts2的大部分核心组件不是以硬编码的形式写在代码中,而是通过自身的IoC容器来管理的. Struts2以可配置的形式来管理核心组件,所以开发者可以很容易的扩展框架的核心组件.当开发者需要扩展或 ...

  5. tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

    writedby 张艳涛,上一篇写了springmvc对<mvc:annoXXXX/>标签的解析过程,其实是遗漏重要的细节,因为理解的不深入吧 今天接着解析<bean>标签 & ...

  6. 又一本springmvc学习指南 之---第22篇 springmvc 加载.xml文件的bean标签的过程

    writedby 张艳涛,今天看spring mvc 学习指南的第2章,特意提下这个作者是how tomcat works 俩个作者之一, 喜欢上一本书的风格,使用案例来讲述原理, 在做第一个案例的时 ...

  7. 使用配置类而不使用XML文件(代替bean.xml)对spring进行配置

    以下类是一个配置类,它的作用和bean.xml是一样的注解: @Configuration 作用: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解. 获取容器时需要使用Anno ...

  8. tomcat与springmvc 结合 之---第19篇 springmvc 加载.xml文件的bean 过程

    writedby 张艳涛,看springmvc 的源码太难了,怎么办, 这篇文章主要分析了看透springmvc的第9章结尾的 如何解析xml 命名空间标签 <?xml version=&quo ...

  9. Java JSON、XML文件/字符串与Bean对象互转解析

    前言      在做web或者其他项目中,JSON与XML格式的数据是大家经常会碰见的2种.在与各种平台做数据对接的时候,JSON与XML格式也是基本的数据传递格式,本文主要简单的介绍JSON/XML ...

随机推荐

  1. DWORD类型的IP地址转换为CString字符串

    从ip地址控件获得的ip地址是DWORD类型的 用MessageBox怎样将ip地址显示出来呢? DWORD类型32位,每4位为一组代表常见的IP地址,即***.***.***.***. 采用HIWO ...

  2. Event List

    Created by John Boteler on 2015.01.16 Go to start of metadata   About The current up-to-date list of ...

  3. .NET垃圾回收:非托管资源,IDispose和析构函数的结合

    http://blog.jobbole.com/85436/ 原文出处: 田小计划   欢迎分享原创到伯乐头条 前面一篇文章介绍了垃圾回收的基本工作原理,垃圾回收器并不是可以管理内存中的所有资源.对于 ...

  4. intelij idea 2016.2注册码

    激活码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1l ...

  5. C# 多线程的等待所有线程结束 用 ManualResetEvent 控制

    using System; using System.Collections.Generic; using System.Threading; namespace ConsoleApplication ...

  6. OpenStack nova VM migration (live and cold) call flow

    OpenStack nova compute supports two flavors of Virtual Machine (VM) migration: Cold migration -- mig ...

  7. activiti自定义流程之整合(七):完成我的申请任务

    在上一篇的获得我的申请中,可以看到js代码中还包含了预览和完成任务的代码,既然上一篇已经罗列了相关代码,这里也就不重复. 那么需要补充的是,在上边的完成任务的js代码中,我们还调用了getTaskFo ...

  8. JavaScript 判断一个对象的数据类型。

    1.isString var isString1 = function (obj){ return Object.prototype.toString.call(obj)==="[objec ...

  9. TEST设置

  10. jquery是如何架构的.

    心里一直有个疑问. jquery是如何做到一个jQuery即可以当方法用比如$();又可以当对象用$.extend(); 现在总结一下吧 function method(){} var m=new m ...