Spring 初试 对象数组,LIST,SET

本文介绍如何在Spring框架中配置含有数组、列表、集合和映射等复杂类型的对象,并通过示例代码展示了具体的实现方法。

如一个类中包含另一个类的数组,LIST,SET,MAP 在Spring中配置会是怎样呢?例子如下

一个Stu类
public class Stu {
private String stuName;
private Integer stuAge;
public Integer getStuAge() {
    return stuAge;
}
public void setStuAge(Integer stuAge) {
    this.stuAge = stuAge;
}
public String getStuName() {
    return stuName;
}
public void setStuName(String stuName) {
    this.stuName = stuName;
}
}
//一个USER类
public class User {
private String userName;
private Integer age;
private Stu[] stus1;
private List stus2;
private Set stus3;
private Map stus4;
public Integer getAge() {
    return age;
}
public void setAge(Integer age) {
    this.age = age;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public Stu[] getStus1() {
    return stus1;
}
public void setStus1(Stu[] stus1) {
    this.stus1 = stus1;
}
public List getStus2() {
    return stus2;
}
public void setStus2(List stus2) {
    this.stus2 = stus2;
}
public Set getStus3() {
    return stus3;
}
public void setStus3(Set stus3) {
    this.stus3 = stus3;
}
public Map getStus4() {
    return stus4;
}
public void setStus4(Map stus4) {
    this.stus4 = stus4;
}

}
//spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="astu" class="org.nieweiguo.Stu" abstract="false"
    singleton="true" lazy-init="default" autowire="constructor"
    dependency-check="default">
    <property name="stuName">
     <value>阿猫</value>
    </property>
    <property name="stuAge">
     <value>18</value>
    </property>
</bean>
<bean id="astu2" class="org.nieweiguo.Stu" abstract="false"
    singleton="true" lazy-init="default" autowire="constructor"
    dependency-check="default">
    <property name="stuName">
     <value>阿狗</value>
    </property>
    <property name="stuAge">
     <value>28</value>
    </property>
</bean>

<bean id="user" class="org.nieweiguo.User" abstract="false"
    singleton="true" lazy-init="default" autowire="constructor"
    dependency-check="default">
    <property name="userName">
     <value>nieweiguo</value>
    </property>
    <property name="age">
     <value>25</value>
    </property>
    <property name="stus1">
     <list>
      <ref bean="astu" />
      <ref bean="astu" />
      <ref bean="astu" />
     </list>
    </property>
    <property name="stus2">
     <list>
      <ref bean="astu2" />
      <ref bean="astu2" />
      <ref bean="astu2" />
     </list>
    </property>
    <property name="stus3">
     <set>
      <ref bean="astu" />
      <ref bean="astu2" />
     </set>
    </property>
    <property name="stus4">
     <map>
      <entry key="stu1">
       <ref bean="astu" />
      </entry>
      <entry key="stu2">
       <ref bean="astu2" />
      </entry>
     </map>
   
    </property>
</bean>
</beans>
//TEST类
public class testSpring {
/**
    * @param args
    */
public static void main(String[] args) {
    ApplicationContext context =new FileSystemXmlApplicationContext("src/applicationContext.xml");
    User user=(User)context.getBean("user");
//对象数组得到显示
    Stu[] stus1=(Stu[])user.getStus1();
    for(int i=0;i<stus1.length;i++)
    {
     System.out.println(stus1.getStuName());
    }
    //LIST得到显示
    List stus2=user.getStus2();
    for(int i=0;i<stus2.size();i++)
    {
     System.out.println(((Stu)stus2.get(i)).getStuName());
    }
    //Set 得到显示
    Set stus3=user.getStus3();
    Object stuso3[]=(Object[])stus3.toArray();
    for(int i=0;i<stuso3.length;i++)
    {
     Stu a=(Stu)stuso3;
     System.out.println(a.getStuName());
    }
    //Map得到显示
    Map stus4=user.getStus4();
    Stu a=(Stu)stus4.get("stu1");
    Stu b=(Stu)stus4.get("stu2");
    System.out.println(a.getStuName()+"aaaa");
    System.out.println(a.getStuName()+"bbb");
  
}
}
输出结果如下:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
阿猫
阿猫
阿猫
阿狗
阿狗
阿狗
阿猫
阿狗
阿猫aaaa
阿猫bbb

还可以定义一个类的 初始化方法。
<bean id="user" class="org.nieweiguo.User" abstract="false"
   singleton="true" lazy-init="default" autowire="constructor"
   dependency-check="default" init-method="init">
</bean>
在类中 可以这样写 
public void init()
{
   this.userName="anie";
   this.age=25;
}

在Vue中传递对象数组Spring MVC可以通过以下步骤实现: ### 前端(Vue) 在Vue项目中,使用`axios`库(一个基于Promise的HTTP客户端)来发送包含对象数组的请求。以下是一个示例: ```vue <template> <div> <button @click="sendData">发送数据</button> </div> </template> <script> import axios from 'axios'; export default { methods: { sendData() { // 定义对象数组 const dataArray = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' } ]; // 发送POST请求 axios.post('http://localhost:8080/api/receiveData', dataArray) .then(response => { console.log('响应数据:', response.data); }) .catch(error => { console.error('请求出错:', error); }); } } }; </script> ``` 在上述代码中,定义了一个包含两个对象数组`dataArray`,并使用`axios.post`方法将其发送到Spring MVC的`/api/receiveData`接口。 ### 后端(Spring MVC) 在Spring MVC项目中,需要创建一个控制器来接收对象数组。以下是一个示例: ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; // 定义一个简单的对象类 class Person { private int id; private String name; // Getters and Setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @RestController public class DataController { @PostMapping("/api/receiveData") public String receiveData(@RequestBody List<Person> persons) { // 处理接收到的对象数组 for (Person person : persons) { System.out.println("ID: " + person.getId() + ", Name: " + person.getName()); } return "数据接收成功"; } } ``` 在上述代码中,定义了一个`Person`类来表示对象的结构,使用`@RestController`注解创建一个RESTful控制器,并使用`@PostMapping`注解处理POST请求。`@RequestBody`注解用于将请求体中的JSON数据映射到`List<Person>`对象。 ### 注意事项 - 确保前端和后端的对象结构一致,即前端发送的对象属性名和后端接收的对象属性名相同。 - 确保Spring MVC项目中已经配置了JSON数据的解析,通常使用Spring Boot时会自动配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值