您的位置:58编程 > spring 弹簧 Spring教程 - 弹簧集属性

spring 弹簧 Spring教程 - 弹簧集属性

2023-03-20 06:33 Spring教程

spring 弹簧 Spring教程 - 弹簧集属性

spring 弹簧

弹簧是一种机械元件,它可以在受到外力作用时产生弹性变形,并在外力消失后恢复原状。它是一种重要的机械元件,广泛应用于机械设备中。

Spring 框架是 Java 平台上的一个开源的轻量级的 Java 开发框架,它主要用于快速开发企业应用程序。Spring 框架提供了一套基于 POJO 的低耦合、面向切面的容器,使得 Java 开发者能够轻松地使用 IOC、AOP 等高级特性。

public class SpringTest {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

        helloWorld.sayHello();

    }
}

Spring 框架提供了大量的优势,如依赖注入、声明式事务、AOP 等特性,使得 Java 开发者能够快速地开发出高质量的企业应用系统。此外,Spring 框架还能够帮助 Java 开发者快速地整合各种不同的 Java 技术,如 Hibernate、Struts 等。

Spring 框架核心是 IOC 和 AOP 两大部分。IOC 是 Inversion of Control 的简写,即“控制反转”。IOC 采用依赖注入方式来实现对象之间的耦合关系,使得对象之间不再相互依赖,耦合度大大降低。AOP 是 Aspect Oriented Programming 的简写,即“面向切面编程”。AOP 采用切面和连接点来将业务逻辑和补充逻辑进行隔离和封装,使得代码易于理解和修改。

Spring 框架拥有众多优势和特性,因此已成为当前最流行的 Java EE 公共平台之一。它不仅能帮助 Java 工作者快速地开发出优雅、易扩展、易测试的企业应用;而且还能帮助工作者整合不同的 Java 技术以实施真正意义上的 SOA 服务化体系。

Spring教程 - 弹簧集属性

Spring教程 - 弹簧集属性


我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。

我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。...

Java Bean

为了展示如何使用xml配置文件来填充集合属性,我们定义了一个具有四个集合属性的Customer对象。

package com.www..cnmon;

import java.util.HashSet;
import java.util.Set;

public class Customer {
  private Set<Object> sets = new HashSet<Object>();

  public Set<Object> getSets() {
    return sets;
  }

  public void setSets(Set<Object> sets) {
    this.sets = sets;
  }

  public String toString() {
    return sets.toString();
  }
}

Person Java Bean

package com.www..cnmon;

public class Person {
  private String name;
  private int age;
  private String address;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + ", address=" + address
        + "]";
  }
  
}


以下代码显示如何将数据填充到java.util.Set类型化属性。

代码填充三个值。 第一个是硬编码值1.第二个是一个bean参考。 我们必须在某处定义PersonBean,以便在此处使用它。 第三个是bean定义与属性设置。

...
<property name="sets">
    <set>
      <value>1</value>
      <ref bean="PersonBean" />
      <bean class="com.www..cnmon.Person">
        <property name="name" value="java2sSet" />
        <property name="address" value="address" />
        <property name="age" value="28" />
      </bean>
    </set>
</property>
...


例子

Full Spring的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="CustomerBean" class="com.www..cnmon.Customer">
    <!-- java.util.Set -->
    <property name="sets">
      <set>
        <value>1</value>
        <ref bean="PersonBean" />
        <bean class="com.www..cnmon.Person">
          <property name="name" value="java2sSet" />
          <property name="address" value="address" />
          <property name="age" value="28" />
        </bean>
      </set>
    </property>
  </bean>
  <bean id="PersonBean" class="com.www..cnmon.Person">
    <property name="name" value="java2s1" />
    <property name="address" value="address 1" />
    <property name="age" value="28" />
  </bean>
</beans>

下面是加载和运行配置的代码。

package com.www..cnmon;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

输出

Customer [
sets=[
1, 
Person [address=address 1, age=28, name=java2s1], 
Person [address=address, age=28, name=java2sSet]]
]

Download Java2s_Spring_Set_Properties.zip

SetFactoryBean

输出...

以下代码显示了如何使用SetFactoryBean。

这里是Java bean类。

package com.www..cnmon;
//from  w w  w.  j av  a  2  s.  co  m
import java.util.HashSet;
import java.util.Set;

public class Customer {
  private Set<Object> sets = new HashSet<Object>();

  public Set<Object> getSets() {
    return sets;
  }

  public void setSets(Set<Object> sets) {
    this.sets = sets;
  }

  public String toString() {
    return sets.toString();
  }
}

这里是Spring的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="CustomerBean" class="com.www..cnmon.Customer">
    <property name="sets">
      <bean class="org.springframework.beans.factory.config.SetFactoryBean">
        <property name="targetSetClass">
          <value>java.util.HashSet</value>
        </property>
        <property name="sourceSet">
          <list>
            <value>1</value>
            <value>2</value>
            <value>3</value>
          </list>
        </property>
      </bean>
    </property>
  </bean>
</beans>

Download Java2s_Spring_SetFactoryBean.zip

util模式

我们还可以使用util模式和< util:set> 以将数据填充到java.util.Set。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-2.5.xsd">
  <bean id="CustomerBean" class="com.www..cnmon.Customer">
    <property name="sets">
      <util:set set-class="java.util.HashSet">
        <value>1</value>
        <value>2</value>
        <value>3</value>
      </util:set>
    </property>
  </bean>
</beans>

Download Java2s_Spring_set_util.zip

使用以下代码运行应用程序。

package com.www..cnmon;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
 
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
 
    }
}

上面的代码生成以下结果。

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