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. Apache配置代理服务器的方法(2)

    Proxy指令后面的*号表示客户端使用代理服务器访问的目的地址.在上面两个例子中,*号表示所有地址,即禁止使用代理服务器访问所有地址.而如果想仅禁止某一些地址时,可以参考下面的例子: Order de ...

  2. Updatepanel 注册javascript 方法

    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "test", "alert ...

  3. 设计模式-UML类图的各符号含义(转)

    UML类图的各符号含义 类图基本符号可拆分为虚线,箭头,实线,空心右三角,实心右三角,空心菱形和实心菱形.由这些基本的图形进行组合构成了类图的基本符号.这里要注意这几个符号的顺序,代表了类与类之间关系 ...

  4. Apache Shiro 使用手册(五)Shiro 配置说明

    Apache Shiro的配置主要分为四部分:  对象和属性的定义与配置 URL的过滤器配置 静态用户配置 静态角色配置 其中,由于用户.角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包含 ...

  5. [转载] 【Shiro】Apache Shiro架构之实际运用(整合到Spring中)

    写在前面:前面陆陆续续对Shiro的使用做了一些总结,如题,这篇博文主要是总结一下如何将Shiro运用到实际项目中,也就是将Shiro整到Spring中进行开发.后来想想既然要整,就索性把Spring ...

  6. 鸟哥的linux私房菜学习

    cat /etc/shells 系统拥有的shellcat /etc/passwd 记录用户使用的shell按两次 tab 键可显示所有可执行的指令alias 查看所有命令的别名alias lm='l ...

  7. 【WCF】无废话WCF入门教程

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  8. 47. Largest Rectangle in Histogram && Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  9. java web须知细节

    1.${pageContext.request.contextPath}是从这个请求路径(URL)上截取你的项目应用名的,比如你的项目名是hello,截取的结果应该就是/hello,/代表http// ...

  10. Android test---monkey

    一.在使用monkey之前,需要用到模拟器,那么怎么启动模拟器呢,先看一下电脑有什么模拟器,通过命令行查看一下 android list avd 二.看到了模拟器列表了,下来就是启动模拟器了.在命令行 ...