bean配置

启用注解

<context:annotation-config/>

使用spring的特殊bean

对bean

BeanPostProcessor

spring本身提供的特殊bean

1.实现了BeanPostProcessor的后置处理器

2.PropertyPlaceholderConfigurer.

分散配置(有两种方式引入文件)

使用spring的特殊bean,完成分散配置。

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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- 引入我们的db. -->
<context:property-placeholder location="classpath:com/hsp/dispatch/db.properties,classpath:com/hsp/dispatch/db2.properties"/> <bean id="dbutil" class="com.hsp.dispatch.DBUtil">
<property name="name" value="${name}"/>
<property name="drivername" value="${drivername}"/>
<property name="url" value="${url}"/>
<property name="pwd" value="${pwd}"/>
</bean> <bean id="dbutil2" class="com.hsp.dispatch.DBUtil">
<property name="name" value="${name}"/>
<property name="drivername" value="${drivername}"/>
<property name="url" value="${url}"/>
<property name="pwd" value="${pwd}"/> </bean>
</beans>

说明:当通过context:property-placeholder引入属性文件的时候,有多个需要使用,号间隔。

package com.hsp.dispatch;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App1 {
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/dispatch/beans.xml"); DBUtil dbUtil = (DBUtil)ac.getBean("dbutil2");
System.out.println(dbUtil.getDrivername());
System.out.println(dbUtil.getName());
System.out.println(dbUtil.getUrl());
System.out.println(dbUtil.getPwd()); } }

db2.properties

name=root2
drivername=oracle:jdbc:driverDriver2
url=jdbc:oracle:thin:@127.0.0.1:1521:hsp2
pwd=tiger2

使用占位符变量代替bean装配文件中的硬编码配置。占位符采用${variable}形式。

spring学习(5)的更多相关文章

  1. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  4. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  5. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

  6. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  7. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  8. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  9. Spring学习8-Spring事务管理

      http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html   Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...

  10. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

随机推荐

  1. 第1章列表处理——1.1 Lisp列表

    Lisp是啥? Lots of Isolated Silly Parentheses (大量分离的愚蠢的括号) Lisp指的是"LISt Processing"(列表处理),通过把 ...

  2. 【Mac系统】之破解Navicat Premium for Mac(中文版)

    前期准备工作: 下载Mac中文版本: 下载地址:链接:https://pan.baidu.com/s/1wShOFK9odsSWBSphgqTdEQ  密码:ipsa 下载完成后进行安装,安装步骤省略 ...

  3. 【转】Jenkins+Ant+Jmeter接口自动化集成测试实例

    出处:https://my.oschina.net/MrToStudy/blog/742251 一.Jenkins安装配置 1.安装配置JDK1.6+环境变量: 2.下载jenkins.war,放入C ...

  4. UGUI随记

    <color=#ffef00ff>武器</color>:巨剑 <color=#ffef00ff>种族</color>:人族 <color=#ffe ...

  5. thinkPHP5.0的添加(C操作)

    首先创建表单: 后台表单用的是layui框架(模块化前端框架),有自己的表单验证,推荐大家使用,在这里表单我就不再贴代码了 其次后台处理: //接收数据并入库 $data = $this->re ...

  6. 解决ListView滑动上下出现阴影

    网上大部分说在listview的属性中通过设置android:fadingEdge="none"来解决问题,需要说明的是是在2.3版本之前有效! 方法一. public class ...

  7. JAVA中sleep() 和 wait() 有什么差别?

    (网上的答案:sleep是线程类(Thread)的方法,导致此线程暂停运行指定时间,将运行机会给其它线程.可是监控状态依旧保持,到时后会自己主动恢复.调用sleep不会释放对象锁. wait是Obje ...

  8. HDU-4031-Attack(树状数组)

    Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is ab ...

  9. DevExpress控件显示前弹出到期时间解决办法

    方法是,删除Properties下的license.licx文件,重新生成即可.

  10. D-hdu 1465 不容易系列之一(递推)

    hdu 1465 不容易系列之一(错排) 不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...