您的位置:58编程 > spring bean 属性 Spring教程 - Spring Bean属性

spring bean 属性 Spring教程 - Spring Bean属性

2023-05-20 21:33 Spring教程

spring bean 属性 Spring教程 -  Spring Bean属性

spring bean 属性 Spring教程 - Spring Bean属性

spring bean 属性

Spring教程 - Spring Bean属性


我们可以通过几种方式将数据填充到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>

“p"模式

我们甚至可以在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配置文件。

阅读全文
以上是58编程为你收集整理的spring bean 属性 Spring教程 - Spring Bean属性全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58编程 58biancheng.com 版权所有 联系我们
桂ICP备12005667号-32 Powered by CMS