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. 关于如何获取/清除 MAXScript 侦听器内的文本

    关于如何获取/清除 MAXScript 侦听器内的文本 用来保存记录?还没想到实际用处,先记上. macroRecorder as string listener as stringclearList ...

  2. 合并excel-MergeExcel

    MergeExcel 将需要合并的excel放入该目录下,包含xls和xlsx 点击运行按钮: 输入包含多少列: 等待运行,完毕! 基于python编写,源码和exe于 TTyb

  3. Jquery数组操作

    jQuery的数组处理,便捷,功能齐全. 最近的项目中用到的比较多,深感实用,一步到位的封装了很多原生js数组不能企及的功能. 最近时间紧迫,今天抽了些时间回过头来看 jQuery中文文档 中对数组的 ...

  4. js获取当前时间&js 页面时钟

    js获取当前时间 //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = ...

  5. Eclipse *下载

    简单了解,Eclipse是绿色软件,下载下来是个压缩包,只需要解压,加上jdk就可以运行了. 相比MyEclipse而言,它是免费的,后者是收费的.各有侧重吧 有很多人用Eclipse,也有很多人用M ...

  6. CentOS 6.5 安装 Python3

    1.安装环境 yum -y install gcc zlib-devel make 2.下载python版本 wget http://www.python.org/ftp/python/3.5.1/P ...

  7. 使用springMVC实现文件上传和下载之环境配置与上传

    最近的项目中用到了文件的上传和下载功能,任务分配给了其他的同时完成.如今项目结束告一段落,我觉着这个功能比较重要,因此特意把它提取出来自己进行了尝试. 一. 基础配置: maven导包及配置pom.x ...

  8. math.h--------数学函数

    三角函数 1.cos 函数 --- 求x的余弦值,返回弧度x的余弦值.(弧度=角度×PI/180). 函数原型 double cos(double x); 函数参数:弧度值 用例:v = cos (x ...

  9. 安装ss

    用的是centos , shell中执行如下命令 yum install python-setuptools && easy_install pip pip install shado ...

  10. 剑指offer习题集2

    1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...