调用xml文件的bean
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&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的更多相关文章
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- 01_1_jdom调用xml文件
01_1_jdom调用xml文件 1. 导入jdom.jar包 2. xml文件内容 test.xml <?xml version="1.0" encoding=" ...
- 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean
在 Spring 容器内拼凑 bean 叫做装配.装配 bean 的时候,你是在告诉容器,需要哪些 bean ,以及容器如何使用依赖注入将它们配合在一起. 理论上,bean 装配的信息可以从任何资源获 ...
- Struts2 学习笔记——struts.xml文件之Bean的配置
Struts2的大部分核心组件不是以硬编码的形式写在代码中,而是通过自身的IoC容器来管理的. Struts2以可配置的形式来管理核心组件,所以开发者可以很容易的扩展框架的核心组件.当开发者需要扩展或 ...
- tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程
writedby 张艳涛,上一篇写了springmvc对<mvc:annoXXXX/>标签的解析过程,其实是遗漏重要的细节,因为理解的不深入吧 今天接着解析<bean>标签 & ...
- 又一本springmvc学习指南 之---第22篇 springmvc 加载.xml文件的bean标签的过程
writedby 张艳涛,今天看spring mvc 学习指南的第2章,特意提下这个作者是how tomcat works 俩个作者之一, 喜欢上一本书的风格,使用案例来讲述原理, 在做第一个案例的时 ...
- 使用配置类而不使用XML文件(代替bean.xml)对spring进行配置
以下类是一个配置类,它的作用和bean.xml是一样的注解: @Configuration 作用: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解. 获取容器时需要使用Anno ...
- tomcat与springmvc 结合 之---第19篇 springmvc 加载.xml文件的bean 过程
writedby 张艳涛,看springmvc 的源码太难了,怎么办, 这篇文章主要分析了看透springmvc的第9章结尾的 如何解析xml 命名空间标签 <?xml version=&quo ...
- Java JSON、XML文件/字符串与Bean对象互转解析
前言 在做web或者其他项目中,JSON与XML格式的数据是大家经常会碰见的2种.在与各种平台做数据对接的时候,JSON与XML格式也是基本的数据传递格式,本文主要简单的介绍JSON/XML ...
随机推荐
- (转) Deep learning architecture diagrams
FastML Machine learning made easy RSS Home Contents Popular Links Backgrounds About Deep learning ar ...
- Docker SSH+NGHINX+MYSQL
1.添加创建镜像的配置文件 vim Dockerfile 输入以下内容: FROM jdeathe/centos-sshMAINTAINER baxk"xxx@hongh ...
- SSIS 目录
微软 BI 系列随笔 - SSIS 2012 基础 - SSIS 目录 上一篇讲解了使用SSIS参数与环境,由于涉及到了SSIS目录的相关知识和概念,本篇将对其进行讲解. 注:在之前的版本中,是使用整 ...
- angular+bootstrap+MVC 之三,分页控件初级版
今天实现一个分页控件,效果如下: 1.HTML: <!doctype html> <!--suppress ALL --> <html ng-app="appT ...
- Linux下删除空文件,删除指定大小的文件
Linux下批量删除空文件(大小等于0的文件)的方法: find . -name "*" -type f -size 0c | xargs -n 1 rm -f 用这个还可以删除指 ...
- ZooKeeper程序员指南(转)
译自http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html 1 简介 本文是为想要创建使用ZooKeeper协调服务优势的分布式 ...
- ADF_ManagedBean的概念和管理(概念)
20150623 Created By BaoXinjian
- 鸟哥的linux私房菜学习
cat /etc/shells 系统拥有的shellcat /etc/passwd 记录用户使用的shell按两次 tab 键可显示所有可执行的指令alias 查看所有命令的别名alias lm='l ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- apache2 tomcat https配置-被忽悠进了CentOS 6
因为需要让ios应用可以绕过appstore,要找个https的地方放ipa,决定使用已有http server. 首先需要说明的是,apache可以通过反向代理方式将用户的https分发到tomca ...