Spring中${}的使用

来源:互联网 发布:excel数据透视表题目 编辑:程序博客网 时间:2024/06/08 03:01

       在spring里面用${}是可以获得配置文件里面的值。比如你的<bean id="xxx" class="xxx.xxx.xxx.${xmlTool}">这个里面xmlTool是在另一个配置文件里面配了的,在spring里面就可以用${xmlTool}引用进来。以后如果你要对xmlTool做修改的话,也只用修改配置文件,不用修改代码。好处就是如果你的程序要修改的时候,就只用改${}里配置文件的内容就行了,不用改spring里的配置文件的内容。

那么问题来了,spring这么知道我的配置文件是哪个呢?这个当然是需要配置啦!

 方法一:使用bean的注入来引入配置文件:


<!-- 引入配置文件 -->    <bean id="propertyConfigurer"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="location" value="classpath*:jdbc.properties" />    </bean>


  方法二:spring3提供更简单的方式,使用自动扫描的方式:


<!-- 配置文件 -->    <context:property-placeholder location="classpath*:jdbc.properties" />


  以上两种方式都可以将指定的配置文件加载进来,然后通过${}符号的引用,即可通过外部对变量的修改,来进行切换,不需要每次改动内部的值!


  jdbc.properties文件的内容格式也很简单,是键值对的方式,#表示注释,比如:  


driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true

username=root

password=root

#定义初始连接数 

initialSize=5

#定义最大连接数  

maxActive=20

#定义最大空闲  

maxIdle=20

#定义最小空闲  

minIdle=1

#定义最长等待时间  

maxWait=60000




参考出处:

https://zhidao.baidu.com/question/2055440848342908507.html

http://www.cnblogs.com/handsomeye/p/6432203.html

原创粉丝点击