Spring2.0中http://www.springframework.org/schema/p

本文介绍了Spring 2.0中SimplePropertyNamespaceHandle解析器的作用及使用方法,通过XML配置文件实例展示了如何使用该解析器进行Bean的简单属性配置注入,包括直接注入属性值和引用其他Bean的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring2.0中还有一个非常实用的解析器,SimplePropertyNamespaceHandle,若配置文件中引用http://www.springframework.org/schema/p命令空间,则将会使用SimplePropertyNamespaceHandle来处理这个Bean的定义,可以在Spring2.0中的Bean中以更简单的方式配置设值方法注入,如下所示: 
< ?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:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

< 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:url则可以直接注入BasicDataSource的url属性值,可以使用p:url-ref属性来引用另外一个Bean。 
    如,Spring2.0以前的一个DAO配置: 
< bean id="userDao" class="com.easyjf.bbs.dbo.springjdbc.UserDaoSpringJdbc"> 
       < property name="dataSource">< ref bean="dataSource"/>< /property> 
   < /bean>  
  使用简短属性方式,则改成如下: 
< bean id="userDao" class="com.easyjf.bbs.dbo.springjdbc.UserDaoSpringJdbc" p:dataSource-ref="dataSource" /> 

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- START SNIPPET: example --> <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 http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- Allows us to use system properties as variables in this configuration file --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>file:${activemq.conf}/credentials.properties</value> </property> </bean> <!-- Allows accessing the server log --> <bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery" lazy-init="false" scope="singleton" init-method="start" destroy-method="stop"> </bean> <!-- The <broker> element is used to configure the ActiveMQ broker. --> <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}"> <!-- dataDirectory="/mnt/mqcache" --> <desti
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值