spring 的xml配置使用p标签简化

时间:2022-11-02 08:52:40
1、常见配置 比如配置数据源 读取properties
<!-- 配置阿里巴巴数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<property name="filters" value="wall,stat,log4j" />

<property name="maxActive" value="300" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />

<property name="timeBetweenEvictionRunsMillis" value="3000" />
<property name="minEvictableIdleTimeMillis" value="300000" />

<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />

<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
</bean>

 

可以配置如下
<
bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.gjt.mm.MySQL.Driver"
p:url
="jdbc:mysql://127.0.0.1:3306/easyjf-bbs"

p:username="root" p:password="mysql" /> < /beans>
p表示对该bean里面的属性进行注入,格式为p:属性名=注入的对象
效果与在bean里面使用<property>标签一样