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" />
分享到:
相关推荐
例如,在Spring框架的配置文件中,使用xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"来定义schemaLocation。 四、案例...
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans ...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate...
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 配置细节在这里 --> </beans> ``` `<beans>`元素是配置文件的根节点,用于定义bean的声明和其他配置。此外,Spring还提供了多个命名...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop ...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop ...
xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ...
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!-- 自动扫描包含@Repository、@Service注解的bean,并自动注入@Required、@Autowired的属性 --> <context:component-scan base-package=...
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop ...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ...
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ...
<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-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ...
http://www.springframework.org/schema/security/spring-security-2.0.xsd"> ... </beans> 3. 命名空间的设计 命名空间被用来设计成,处理框架内最常见的功能,提供一个简化和简洁的语法,使他们在一个应用程序...
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloWorld" class="com.example.HelloWorld"> 你好,世界!...
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="Struts2Action.xml"/> <import resource="database.xml"/> </beans> ``` **4. database.xml** `database.xml` 文件...