第一次在spring的xml配置文件中遇到${}的时候,真的感觉很奇怪,因为以前都是用value="#{beanId.bean属性}"来做属性注入的,就没有见过这样(以下是demo)
<bean id="testSpring" class="testSpring.TestSpring">
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="URL" value="${url}"></property>
</bean>
后来经过研究,发现配置文件中还要写一段这样的配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:cfg.properties"></property>
</bean>
经过研究,上面这一段代码使用 这个类PropertyPlaceholderCofiguerer(由Spring提供的)来加载properties文件,然后用了这个配置之后,配置bean的value就可以使用value="${Properties文中的key}",这样Properties文中的key所对应的value值就会注入到bean的value值里面。
具体原理是什么,还没有弄懂...
第一次在这里写博客,哈哈