我们可以通过几种方式将数据填充到Spring配置XML中定义的Java Bean中。
以下部分显示将值注入定义的 name
和 type
属性在MyClass。
package com.www..cnmon public class MyClass { private String name; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
下面的代码显示了如何在“值"标记中注入值,并用“属性"标记括起来。
<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.5.xsd"> <bean id="myClass" class="com.www..cnmon.MyClass"> <property name="name"> <value>java2s</value> </property> <property name="type"> <value>txt</value> </property> </bean> </beans>
从Spring配置xml文件加载myClass之后的名称和类型属性分别设置为java2s和txt。
我们可以使用快捷方式属性标记来将值填充到Java bean属性中以下方式。
属性标记可以具有值属性。 我们把我们的价值。
<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.5.xsd"> <bean id="MyClass" class="com.www..cnmon.MyClass"> <property name="name" value="java2s" /> <property name="type" value="txt" /> </bean> </beans>
我们甚至可以在bean标签中声明Java Bean时填充属性。
<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-2.5.xsd"> <bean id="MyClass" class="com.www..cnmon.MyClass" p:name="java2s" p:type="txt" /> </beans>
为了使用p模式,我们必须声明 xmlns:p =“http://www.springframework.org/schema/p
Spring XML bean配置文件。
Spring教程 - 弹簧依赖检查Spring可以做依赖检查以确保已设置或注入所需的属性。Spring可以做依赖检查以确保已设置或注入所需的...
JSP教程 -JSP发送电子邮件要使用JSP发送电子邮件,请在CLASSPATH中安装JavaMail API和Java Activation Framework(JAF)。发送电子...
sql:setDataSource标签用来配置数据源或者将数据源信息存储在某作用域的变量中,用来作为其它JSTL数据库操作的数据源。语法格式s...
ASP.NET CheckBox TextAlign 属性 CheckBox 控件定义和用法 TextAlign 属性用于设置或返回 CheckBox 文本的对齐方式。 默认的对...
ASP.NET BorderWidth 属性 Style 控件定义和用法 BorderWidth 属性用于设置或返回控件的边框宽度。 语法asp:webcontrol id="id" ...